0

Here's the original question.

Okay, I'm using Node.js and Edge.js to insert values to an SQL database. First attempt was a straightforward insertion of hardcoded values:

var insertRow = edge.func('sql', function () {/*
    INSERT INTO dbo.table (column0, column1, column2)
    VALUES (value0, value1, value2)
*/});

insertRow();

This hardcoded insertion works as expected, of course. And as seen in the answer of the question before me, passing the function an object allows the SQL statement to recognize a name/value pair of the object via an @, allowing dynamic value assignment:

var rowObj = {
    v0: 'value0',
    v1: 'value1',
    v2: 'value2'
}

var insertRow = edge.func('sql', function () {/*
    INSERT INTO dbo.table (column0, column1, column2)
    VALUES (@v0, @v1, @v2)
*/});

insertRow(rowObj);

Works as expected. What I would like to do is have the table and columns be variable as well, through properties provided by the same rowObj. I tried:

var rowObj = {
    t: 'dbo.table',
    c0: 'column0',
    c1: 'column1',
    c2: 'column2',
    v0: 'value0',
    v1: 'value1',
    v2: 'value2'
}

var insertRow = edge.func('sql', function () {/*
    INSERT INTO @t (@c0, @c1, @c2)
    VALUES (@v0, @v1, @v2)
*/});

insertRow(rowObj);

But this doesn't work. I tried making the table dynamic on its own, and the columns on their own, and that didn't work either.

note: I don't know SQL, I don't know the limitations of Node.js/Edge.js, and I'm very new to programming in general (so if my nomenclature is inconsistent/wrong, let me know but please don't tear me apart) I was just wondering if what I'm trying to do can be done.

0

1 Answer 1

0

I suggest you call edge.func as many times as you need different selection criteria. If you don't know the column names a priori, you can always construct the T-SQL string you pass to edge.func dynamically. With this approach you would normally loose some optimizations around precompiled SQL statements, but given how MS SQL optimizes query execution on the server the savings are minimal.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm mostly just curious how much of a 'master' statement I could make. The one TSQL statement to rule them all. Turns out I'm going to need to write my statement(s) in .NET so I won't have as many constraints on syntax. Regardless, thanks! Edge.js is pretty rad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.