|
-
May 11th, 2008, 03:16 PM
#1
How to output what is not in list
Select * from employee where empnum in ('1','2','3','4')
I don't have emp 3 and 4 in the table and so i want to output that.
Basically, instead of outputing what is in the table i want to output what is in the list but not in the table . Is it possible ?
-
May 11th, 2008, 03:32 PM
#2
Re: How to output what is not in list
Create a temporary table (or table variable depending on the flavor of SQL), then do a left join, looing for RHS = null.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
May 11th, 2008, 03:46 PM
#3
Re: How to output what is not in list
hmm..interesting. Not a sql expert. But, can this all be done in query analyzer statements? I am using toad.
create a temp table like #temp
Fill in temp with list items
left or right join with the table i am querying
-
May 11th, 2008, 03:50 PM
#4
Re: How to output what is not in list
Yes it can be done in T-SQL too. After all a stored procedure or a function is also T-SQL. You will need to execute the whole block at once, and not line by line.
-
May 11th, 2008, 04:07 PM
#5
Re: How to output what is not in list
Something like this??
CREATE TABLE #temp (empnum int )
INSERT INTO #temp(empnum) values ('1','2','3','4')
select * from #temp left outer join employee e on
e.empnum=#temp.empnum
drop table #temp
But for this don't need to have table create permissions. If i have read only permissions, is there a way to do it?
-
May 13th, 2008, 03:50 AM
#6
Re: How to output what is not in list
Don't use a temp table, just declare a temporary table variable.
Code:
DECLARE @Temp TABLE { EmpNumber int }
INSERT INTO @Temp VALUES (1)
INSERT INTO @Temp VALUES (2)
INSERT INTO @Temp VALUES (3)
INSERT INTO @Temp VALUES (4)
SELECT T.EmpNumber
FROM @Temp
LEFT JOIN Employee E ON E.EmpNum = T.EmpNumber
WHERE E.EmpNum IS NULL
On Error Kill(User)
-
May 29th, 2008, 06:50 AM
#7
Re: How to output what is not in list
What database are you using?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|