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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SQL If Exists

Author  Topic 

besadmin
Posting Yak Master

116 Posts

Posted - 2010-05-27 : 15:50:16
Hey. I bet this is an easy solution, but I am stuck.

I am trying to do if Exists(Select * from Whatever)
ELSE Insert whatever.

The only part I am having touble with is how do i return the row, if the IF EXISTS is true. Will it just return, or do i need to do something like 'return' or if exsists (select) then Select.

Thanks a ton for any help with this!

Later

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-27 : 16:06:07
if Exists(Select * from Whatever)
begin
Select * from Whatever
end
else
begin
insert whatever
end


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2010-05-27 : 16:06:38
select *
into #tmp
from mytable

if @@RowCount > 0
begin
select * from #TMP
end
else
begin
insert into ...
end


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

besadmin
Posting Yak Master

116 Posts

Posted - 2010-05-27 : 16:47:14
OK, got it. Pretty much what I was thinking. just seems like there should be a better way to do if select = something then select... but it works.
Thanks a ton for the replys!!
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-27 : 16:51:31
Yes in exists() no rows are returned - only a boolean.
So if true you have to do the select again to return rows.



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -