Unix Technical Forum

stored procedure and if exists

This is a discussion on stored procedure and if exists within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, In my stored procedure I would like to do something like : IF EXISTS ( SELECT WebId FROM ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 08:44 AM
Sam
 
Posts: n/a
Default stored procedure and if exists

Hi,
In my stored procedure I would like to do something like :

IF EXISTS
(
SELECT WebId
FROM Webs
WHERE FormatDateId = @FormatDateId
)
BEGIN
SELECT WebId
FROM Webs
WHERE FormatDateId = @FormatDateId
SET @RetVal = -1
END

Is there a nicer way to do this, so that we don't have to do the
request twice ?

Regards

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 08:44 AM
Simon Hayes
 
Posts: n/a
Default Re: stored procedure and if exists

You could use @@ROWCOUNT to see if the query returns a row:

SELECT WebId
FROM Webs
WHERE FormatDateId = @FormatDateId

set @rows = @@ROWCOUNT

if @rows = 1
set @RetVal = -1 -- we found one row
else
.... -- do something else here

Simon

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 08:44 AM
Sam
 
Posts: n/a
Default Re: stored procedure and if exists

thx, I like that !

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:03 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com