Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Jnetik
Starting Member
5 Posts |
Posted - 2011-11-03 : 10:12:17
|
| Hello,This is my example line of code:select NAME_ID from NAMES where NAME_ID IN ('Bob','Joe','Sarah','Dave')Let's say I run the above and inside my database 'NAMES' I have the entries 'Bob' and 'Sarah'.The returned output will look like thisNAME_IDBobSarahMy question is as follows, is it possible to return my entire input list for comparison with the output result, without creating a new table (I only have read-only access).For example, could I get something like this:NAME_ID INPUTBob BobSarah Sarah Joe DaveOr even just this would be helpfulINPUTJoeDaveNOTE: There will be hundreds of other names in the database so I can't just ask it to return anything that is not 'Bob' or 'Sarah'Thanks in advanceJ |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-11-03 : 10:51:42
|
Try this:select distinct dt.Name_Id as Searched, n.Name_Id as inTablefrom(select 'Bob' as Name_Id union all select 'Joe' as Name_Id union all select 'Sarah' as Name_Id union all select 'Dave' as Name_Id) dtleft join Names n on n.Name_Id = dt.Name_Id No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Jnetik
Starting Member
5 Posts |
Posted - 2011-11-03 : 11:52:58
|
| That's great thanks. One more thing if that's OK...Is it possible to put a second column input into the SQL to be returned back out with the search results. If I have a list of peoples names and ages, and I want to see if these peoples names are in the database, can I return a list of matches and non-matches this additional age information.For example,The output from your code above yields the following,Searched inTableJoe (null)Dave (null)Bob BobSarah SarahIf I know that Bob has age 20, Joe, Sarah, Dave have ages 19, 18, 17 respectively. (these ages are supplied by me and are not present in the database)Can I have something like this output:Searched inTable Input_ageJoe (null) 19Dave (null) 18Bob Bob 20Sarah Sarah 17Thanks again so much, I've just played around for an hour trying do the above.J |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-11-03 : 12:10:16
|
[code]select distinct dt.Name_Id as Searched, n.Name_Id as inTable, dt.Input_agefrom(select 'Bob' as Name_Id, 20 as Input_age union all select 'Joe' as Name_Id, 19 as Input_age union all select 'Sarah' as Name_Id, 17 as Input_age union all select 'Dave' as Name_Id, 18 as Input_age) dtleft join Names n on n.Name_Id = dt.Name_Id[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Jnetik
Starting Member
5 Posts |
Posted - 2011-11-03 : 13:19:46
|
| Thank-you very much, that's awesome :) |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-11-03 : 13:21:24
|
welcome  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|
|