On Feb 25, 3:19 am, Emerson Espínola <[email protected]> wrote: > 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?
For your particular logic I think you're missing the use of "else" in your callbacks - since you only want to proceed to the next step if you found something but currently you proceed to the next step even if you found nothing. As someone pointed out there could be some issues with concurrent inserts taking place between the SELECT and the INSERT if there is a chance that there are multiple web servers hitting the database. To get around this people will often just attempt the INSERT and if it fails assume it was already inserted. However, it's likely this is some contrived example and you don't care about such real-world issues yet. Another best practice to be aware of: avoid constructing SQL strings using '+'. Even when you know it is safe someone might follow your example later in an unsafe manner. node-tds does support Parameterized Statements for this purpose I believe. SQL statements contructed as strings need to be eradicated like a disease. Cheers, Dobes -- 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
