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)
 SQL Select with IF exists condition
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mbbrennan
Starting Member

USA
4 Posts

Posted - 12/09/2012 :  21:22:00  Show Profile  Reply with Quote
it is possible to do a select code from table where (if not exist (select value from table) else select value from other table)

I am getting an error on the "IF" part.

The if exists select statement works fine on it's own but not when I add it to the where.

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/09/2012 :  21:30:08  Show Profile  Reply with Quote
IF NOT EXISTS (Select * from Table Where Value = @Value)
 BEGIN
  Select Code from Table
 END
ELSE
BEGIN
Select Value from OtherTable
END
Go to Top of Page

mbbrennan
Starting Member

USA
4 Posts

Posted - 12/09/2012 :  21:44:05  Show Profile  Reply with Quote
Thanks. I have that and this select hypathetically returns a code that I want to then say select value from table where (if not not exist .. etc) -< this returns a code I want to use in a where.
This does not work.
Go to Top of Page

mbbrennan
Starting Member

USA
4 Posts

Posted - 12/09/2012 :  22:39:39  Show Profile  Reply with Quote
This is the need:

A value I was to use in a where exists in 1 of two places (table). I need to check if it exists in table 1 use it otherwise check place 2 where it's guaranteed to be. The value returned from this I need to pass to a where clause. Has to be within a select statement and not transact sql.
Go to Top of Page

mbbrennan
Starting Member

USA
4 Posts

Posted - 12/10/2012 :  00:24:48  Show Profile  Reply with Quote
I changed my code to a union but I still have 1 problem.

select col1 from tablea
union
select col1 from tableb

I need to apply precedence where if result is found in first select, I don't care about results from secomd select

I tried - select top 1 * from (select col1 from tablea union select col1 from tableb) but top 1 returned the highest value from col1 which is an int return type
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2168 Posts

Posted - 12/10/2012 :  01:35:02  Show Profile  Reply with Quote
[quoteI need to apply precedence where if result is found in first select, I don't care about results from secomd select
[/quote]

SELECT col1, 1 const from tablea
union
select col1, 2 from tableb
ORDER BY const


--
Chandu
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/10/2012 :  07:50:50  Show Profile  Reply with Quote
The following is probably not the most efficient, but logically guaranteed to produce what you described:
select col1 from Tablea
union all
(
	select col1 from Tableb
	except
	select col1 from Tablea
);
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.06 seconds. Powered By: Snitz Forums 2000