Hi,

I'm using node-tds in order to connect to Microsoft SQL database.

I have table A and B and a n to n relationship called C.
In order to insert something in table C I must check if the primary key of 
A and B exist in their respective tables before with a SELECT statement.
Is there any sample code that does that?
I'm thinking something like this:

Select from table A
Check if values where returned.
If so, select from table B
Otherwise, insert into A
If B returned something, insert into C
Otherwise, insert into B

But if I get this last line I'll not insert into C. How to code 
asynchronously? Any example on the website?

I wrote this code below, but it seems it's not working properly. It calls 
only addA() and gives me error in tds.js (140) - if 
(this._currentStatement) throw new Error('Statement currently running'):

function addA (a)
{
    var stmtA = conn.createStatement('INSERT INTO A (id) VALUES (' + a + 
')');
    stmtA.execute();
}

function addB (space)
{
    var stmtB = conn.createStatement('INSERT INTO B (id) VALUES (' + b + 
')');
    stmtB.execute();
}

function addC (a, b)
{
    var stmtC = conn.createStatement('INSERT INTO C (a, b, date) VALUES (' 
+ a + ', ' + b + ', GETDATE())');
    stmtC.execute();
}

exports.linkAandB = function(a, b) {
    var stmtA = conn.createStatement('SELECT * FROM A WHERE id = ' + a);
    stmtRadio.on('done', function(done) {
        if (!done.hasRowCount || done.rowCount <= 0) {
            addA(a);
        }
        
        var stmtB = conn.createStatement('SELECT * FROM B WHERE id = ' + b);
        stmtB.on('done', function(done) {
            if (!done.hasRowCount || done.rowCount <= 0) {
                addB(b);
            }

            addC(a, b);
        });
        stmtB.execute();
        
    });
    stmtA.execute();
}

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to