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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Query WHERE statement

Author  Topic 

Billmiles77
Starting Member

7 Posts

Posted - 2012-10-17 : 17:05:28
I am executing a SQL query, via Toad, that is searching a table based on accounts that are part an 'IN" list within the WHERE statement.
Can I flag accounts that are not found in the DB based on the WHERE 'IN' list accounts?

SELECT account_no
FROM master_table
WHERE (account_no IN ( 01, 02, 03, 04, 05, 06));

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-10-17 : 17:48:41
I'm not sure I understand. Are you looking for NOT IN?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-10-17 : 17:48:54
A small data example might make the post more clear. Thank you.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-17 : 22:45:44
sounds like this:-


SELECT t.account_no
FROM (SELECT '01' AS account_no
UNION ALL
SELECT '02'
UNION ALL
SELECT '03'
UNION ALL
SELECT '04'
UNION ALL
SELECT '05'
UNION ALL
SELECT '06'
)t
LEFT JOIN master_table m
ON m.account_no = t.account_no
WHERE m.ccount_no IS NULL


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

Go to Top of Page

Billmiles77
Starting Member

7 Posts

Posted - 2012-10-18 : 10:30:27
Firstly, Thank you for the reply and time. What I am really looking for is when using the 'WHERE IN' list if an account is not found in the query based on the list can that account, not found, somehow be flag and reported on the output? example "WHERE (account_no IN ( 01, 02, 03, 04, 05, 06));". If during the query excution account 01, 02, 03, 05 and 06 are found in the table however 04 is not, can I somehow still report that the account was not found in the table on the output? (without doing some manual compares of the accounts included in the "WHERE IN " statement to those not reported(not found)?
quote:
Originally posted by tkizer

A small data example might make the post more clear. Thank you.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-18 : 12:56:42
quote:
Originally posted by Billmiles77

Firstly, Thank you for the reply and time. What I am really looking for is when using the 'WHERE IN' list if an account is not found in the query based on the list can that account, not found, somehow be flag and reported on the output? example "WHERE (account_no IN ( 01, 02, 03, 04, 05, 06));". If during the query excution account 01, 02, 03, 05 and 06 are found in the table however 04 is not, can I somehow still report that the account was not found in the table on the output? (without doing some manual compares of the accounts included in the "WHERE IN " statement to those not reported(not found)?
quote:
Originally posted by tkizer

A small data example might make the post more clear. Thank you.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog




Reading this I think you're good to go with my suggestion

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

Go to Top of Page

Billmiles77
Starting Member

7 Posts

Posted - 2012-10-18 : 17:16:19
Thanks again...I will give this a shot and let you know the results..
quote:
Originally posted by visakh16

quote:
Originally posted by Billmiles77

Firstly, Thank you for the reply and time. What I am really looking for is when using the 'WHERE IN' list if an account is not found in the query based on the list can that account, not found, somehow be flag and reported on the output? example "WHERE (account_no IN ( 01, 02, 03, 04, 05, 06));". If during the query excution account 01, 02, 03, 05 and 06 are found in the table however 04 is not, can I somehow still report that the account was not found in the table on the output? (without doing some manual compares of the accounts included in the "WHERE IN " statement to those not reported(not found)?
quote:
Originally posted by tkizer

A small data example might make the post more clear. Thank you.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog




Reading this I think you're good to go with my suggestion

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



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-18 : 21:44:35
welcome

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

Go to Top of Page
   

- Advertisement -