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 |
|
Chester
Starting Member
27 Posts |
Posted - 2004-09-10 : 13:56:34
|
| I'm using SQLserver2000.My query looks like this:select niin, field1, field2, ... etcfrom tablewhere niin in ('aa','bb','cc'...etc)My problem:'cc' is not in the table but I want 'cc' to be returned in the recordset with NULL's in the rest of the columns like this --niin field1 field2 ... etcaa 1234 5678 ... etcbb 123 456 ... etccc NULL NULL ... etcRight now, cc does not appear in the output. How do I do this?TIA |
|
|
DustinMichaels
Constraint Violating Yak Guru
464 Posts |
Posted - 2004-09-10 : 13:59:52
|
| You may need to use a function to convert the csv to a table. You can find the function on this site.SELECT table.niin, table.field1, table.field2from dbo.CvsToTable(@niinItems) a LEFT OUTER JOIN table ON (table.niin = a.Item) |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-09-10 : 14:06:36
|
| select niin, b.*from tableleft outer join (select nin, field1, field2, .... from table where niin in ('aa','bb','cc', ... )) bon table.niin = b.niinassuming niin is the primary key. Without table structures or sample data, it is hard to know exactly what you are looking for.- Jeff |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-09-10 : 14:08:43
|
| [code]USE NorthwindGOSET NOCOUNT ONCREATE TABLE myTable99_Master (niin char(2) PRIMARY KEY, niin_desc varchar(255))CREATE TABLE myTable99_Detail (niin char(2), Col1 int, Col2 int)GOINSERT INTO myTable99_Master (niin, niin_desc)SELECT 'aa', 'aa description' UNION ALLSELECT 'bb', 'bb description' UNION ALLSELECT 'cc', 'cc description'INSERT INTO myTable99_Detail (niin, Col1, Col2)SELECT 'aa', 1,2 UNION ALLSELECT 'aa', 1,3 UNION ALLSELECT 'aa', 1,4 UNION ALLSELECT 'aa', 2,1 UNION ALLSELECT 'aa', 2,2 UNION ALLSELECT 'bb', 1,2 UNION ALLSELECT 'bb', 1,3 UNION ALLSELECT 'bb', 1,4 UNION ALLSELECT 'bb', 2,1 UNION ALLSELECT 'bb', 2,2GO SELECT m.niin, d.Col1, d.Col2 FROM myTable99_Master m LEFT JOIN myTable99_Detail d ON m.niin = d.niinGOSET NOCOUNT OFFDROP TABLE myTable99_MasterDROP TABLE myTable99_DetailGO[/code]Brett8-) |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-09-10 : 14:23:12
|
| good point, Brett -- it probably is a Master/Detail kind of thing going on here; that the classic case of where these techniques come in handy.Again, without tables we don't know for sure.One thing you missed which is key on a LEFT join like that is the criteria (which he did indicate he would need) : it needs to be specified in the JOIN expression, or as a subquery within the join. if you just add it to the end as a WHERE , it will negate the LEFT OUTER join and make it an INNER join.- Jeff |
 |
|
|
Chester
Starting Member
27 Posts |
Posted - 2004-09-10 : 15:02:15
|
| Thanks everyone.Okay this is what I'm doing.I have a web page where people will be typing in the niin's.I have to return the niin's they typed in along with the infofrom the table. The table has thousands of niin's but I onlyneed to return the ones that the user types in. The query thatJeff provided returned all the niin's from the table and thatis not what the user wants to see. There will be multiple peopleon this web page at the same time typing in their niin's. Is therea way I can create a unique temp table for each user that I canstore the niin's they type in and run Brett's left join statement?Again, many thanks. |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-09-10 : 15:37:33
|
| Just create a permanent table with userId, niin, and entered. Delete after some period of time (24 hours, 48 hours...)Create Table userNiins(UserId int,Niin varchar(100),dateEntered datetime default(getdate()))Select * From userNiins ALeft Join NiinTable Bon A.niin = B.niinWhere A.userId = @UserIdCorey |
 |
|
|
|
|
|
|
|