| Author |
Topic |
|
xrum
Yak Posting Veteran
87 Posts |
Posted - 2010-05-13 : 10:17:30
|
| hi,I have two tables, users and transactions. when i try to add the foreign key and save i get an error.i am trying to see if there are any user ids in the transaction table, that are not in the user table.i tried the following:SELECT tblUser.id FROM tblUser WHERE NOT EXIST (SELECT user_id FROM tblTransaction WHERE tblUser.id = tblTransaction.user_idbut i'm getting errors, am i doing it wrong?please help. could there be another reason i can't add a foreign key? To my best understanding, the foreign key (user_id) has to match an id from the user table, but the id from the user table (primary key), does not have to be in the transactions table, right?Thanks! |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-05-13 : 10:19:04
|
| SELECT tblUser.id FROM tblUser as tWHERE NOT EXIST (SELECT user_idFROM tblTransactionWHERE t.id = user_id)MadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-13 : 10:21:56
|
| can you post error?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
xrum
Yak Posting Veteran
87 Posts |
Posted - 2010-05-13 : 10:23:15
|
| Msg 156, Level 15, State 1, Line 3Incorrect syntax near the keyword 'SELECT'.Msg 102, Level 15, State 1, Line 5Incorrect syntax near ')'.same errorsam i missing something? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-13 : 10:24:26
|
| the posted query is missing a end braces------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-05-13 : 10:25:55
|
quote: Originally posted by xrum Msg 156, Level 15, State 1, Line 3Incorrect syntax near the keyword 'SELECT'.Msg 102, Level 15, State 1, Line 5Incorrect syntax near ')'.same errorsam i missing something?
See my first replyMadhivananFailing to plan is Planning to fail |
 |
|
|
xrum
Yak Posting Veteran
87 Posts |
Posted - 2010-05-13 : 10:26:00
|
| thanks,but that's the error i'm getting using this one:SELECT tblUser.id FROM tblUser as tWHERE NOT EXIST (SELECT user_idFROM tblTransactionWHERE t.id = user_id) |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-05-13 : 10:28:07
|
| WHERE NOT EXISTS |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-05-13 : 10:29:33
|
quote: Originally posted by xrum thanks,but that's the error i'm getting using this one:SELECT tblUser.id FROM tblUser as tWHERE NOT EXIST (SELECT user_idFROM tblTransactionWHERE t.id = user_id)
SELECT tblUser.id FROM tblUser as tWHERE NOT EXISTS (SELECT user_idFROM tblTransactionWHERE t.id = user_id)EXIST should be EXISTSMadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-13 : 10:29:39
|
quote: Originally posted by xrum thanks,but that's the error i'm getting using this one:SELECT tblUser.id FROM tblUser as tWHERE NOT EXIST (SELECT user_idFROM tblTransactionWHERE t.id = user_id)
there's a typoits EXISTS------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-13 : 10:29:58
|
ah double ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
xrum
Yak Posting Veteran
87 Posts |
Posted - 2010-05-13 : 10:47:39
|
| now i'm getting this: The multi-part identifier "tblUser.id" could not be bound.:( |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-13 : 10:49:42
|
| [code]SELECT t.id FROM tblUser as tWHERE NOT EXISTS (SELECT 1FROM tblTransactionWHERE t.id = user_id)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
xrum
Yak Posting Veteran
87 Posts |
Posted - 2010-05-13 : 10:53:40
|
| this worked, thanks for your helpSELECT USER_ID FROM tblTransaction WHERE USER_ID NOT IN (SELECT ID FROM TBLUSER) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-13 : 10:57:06
|
quote: Originally posted by xrum this worked, thanks for your helpSELECT USER_ID FROM tblTransaction WHERE USER_ID NOT IN (SELECT ID FROM TBLUSER)
have you tested performance of both? I have seen cases where NOT EXISTS perform better------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-05-13 : 10:59:14
|
quote: Originally posted by xrum now i'm getting this: The multi-part identifier "tblUser.id" could not be bound.:(
Why didn't you use the exact code posted?You must have modified it and ranHave you run this?SELECT t.id FROM tblUser as tWHERE NOT EXISTS (SELECT 1FROM tblTransactionWHERE t.id = user_id)MadhivananFailing to plan is Planning to fail |
 |
|
|
|