SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 displaying records not present in the condition
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

dildileep
Starting Member

3 Posts

Posted - 06/09/2012 :  15:10:12  Show Profile  Reply with Quote
I have list of student ids of around 2000 students o be searched in a table. I would write the query as

select student_id from student_db where student_id in('12346','1245',45612',..........)

this query displays the ids which are present, but i need the student ids which are not present in the given set of ids. Can that be accomplished by a sql query ?

Edited by - dildileep on 06/09/2012 15:10:38

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 06/09/2012 :  15:11:51  Show Profile  Reply with Quote

select student_id from student_db where student_id not in('12346','1245',45612',..........)


another way is to dump id values to a temporary table and use left join or not exists

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

dildileep
Starting Member

3 Posts

Posted - 06/09/2012 :  15:22:00  Show Profile  Reply with Quote
I dont want to create another temporary table for it. we get return code whether a condition is success or not. Is there any way to write sql such that we get return code for every id that is searched ?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 06/09/2012 :  15:37:44  Show Profile  Reply with Quote
quote:
Originally posted by dildileep

I dont want to create another temporary table for it. we get return code whether a condition is success or not. Is there any way to write sql such that we get return code for every id that is searched ?


do you mean you've to return code even if id doesnt exist in table?


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sshelper
Posting Yak Master

213 Posts

Posted - 06/10/2012 :  23:38:36  Show Profile  Visit sshelper's Homepage  Reply with Quote
If I understood your question correctly, one way to do this is to first load your list of student IDs in a table variable (not a temporary table). Then given this table variable, you can return a flag for each student ID displaying whether it exists in your table or not. Your query will look like the following, assuming the name of your table variable is @StudentIDs.

SELECT A.[Student_ID], CASE WHEN B.[Student_ID] IS NOT NULL THEN 1 ELSE 0 END AS [StudentIDExists]
FROM @StudentIDs A LEFT OUTER JOIN [dbo].[Student_DB] B
ON A.[Student_ID] = B.[Student_ID]

Hope this helps.

SQL Server Helper.
http://www.sql-server-helper.com/error-messages/msg-1-500.aspx
Go to Top of Page

dildileep
Starting Member

3 Posts

Posted - 06/11/2012 :  23:27:24  Show Profile  Reply with Quote
@visakh16.. yes even returning code if not present would also be fine. So that I can process using condition later or else displaying the record not present

@sshelper: your solution looks good, i have never tried using temporary variables will try this one once.

I found a query like the below

select 1234,234,456 from dual not exists in (Select student_id from
studentdb where student_id in(1234,234,456)

but this query is wrking when only one id is given in the first part.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 06/12/2012 :  15:50:19  Show Profile  Reply with Quote
you can do like below for that


SELECT t.id,
CASE WHEN s.student_id IS NOT NULL THEN 1 ELSE 0 END AS Is_Present
FROM
(
VALUES ('12346'),('1245'),(45612')
) AS t (id)
LEFT JOIN student_db s
ON s.student_id = t.id


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 06/12/2012 :  15:51:42  Show Profile  Reply with Quote
See below link to understand some of the applications of VALUES table constructor in sql 2008

http://visakhm.blogspot.com/2012/05/multifacet-values-clause.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000