If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > t-sql: returning a row with check

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-11-04, 21:40
ranyc ranyc is offline
Registered User
 
Join Date: Apr 2004
Posts: 2
Question t-sql: returning a row with check

Hello all,

i am writing a SP and want to do the next thing:

1. check whether a row exists by key field.
2. if not - raiserror
3. if does exist - return it.

the thing is that i dont want to scan the table twice with 2 select statements, and dont know how to do it....

any sugestions ??

thanks a lot, Ran.
Reply With Quote
  #2 (permalink)  
Old 04-11-04, 21:42
ranyc ranyc is offline
Registered User
 
Join Date: Apr 2004
Posts: 2
Re: t-sql: returning a row with check

sorry, did a mistake:
in step 3, i might want to return only one field of the row (in some cases).

thanks again, Ran.

Quote:
Originally posted by ranyc
Hello all,

i am writing a SP and want to do the next thing:

1. check whether a row exists by key field.
2. if not - raiserror
3. if does exist - return it.

the thing is that i dont want to scan the table twice with 2 select statements, and dont know how to do it....

any sugestions ??

thanks a lot, Ran.
Reply With Quote
  #3 (permalink)  
Old 04-11-04, 23:24
derrickleggett derrickleggett is offline
Registered User
 
Join Date: Apr 2004
Location: Kansas City, MO
Posts: 734
SELECT col1, col2
FROM table1
WHERE col1 = '1'

IF @@ROWCOUNT = 0
BEGIN
RAISERROR ('Exercise in futility.',16,1)
RETURN 0
END
__________________
MeanOldDBA
derrickleggett@hotmail.com
When life gives you a lemon, fire the DBA.
Reply With Quote
  #4 (permalink)  
Old 04-12-04, 01:11
ms_sql_dba ms_sql_dba is offline
Registered User
 
Join Date: Sep 2003
Posts: 522
declare @field_key your_data_type (..)
set @field_key = (select field_key from your_table
where col1 = '1' --don't know what this is
if @field_key is null raiserror ('your_error, 16, 1)
return @field_key
Reply With Quote
  #5 (permalink)  
Old 04-12-04, 01:31
derrickleggett derrickleggett is offline
Registered User
 
Join Date: Apr 2004
Location: Kansas City, MO
Posts: 734
If you do it that way, then:

<i>in step 3, i might want to return only one field of the row (in some cases).</I>

you will only be able to return one row.
__________________
MeanOldDBA
derrickleggett@hotmail.com
When life gives you a lemon, fire the DBA.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On