?
Solved

how to determine the records affected in an update sql statement in a stored procdure?

Posted on 2004-10-08
3
Medium Priority
?
1,302 Views
Last Modified: 2012-06-27
If I have a stored procedure like this:
CREATE PROCEDURE spTest
@UserID int
AS
set NOCOUNT ON
begin

UPDATE pwreset SET UpdateDate = getdate()
             WHERE UserID = @UserID

--get # of records updated (0 or 1)
--and then do something based on that info
--if #recsAffected > 0
--   select 'true' as result
--else
--   select 'false' as result

end
SET NOCOUNT OFF
GO


how would I know how many records were affected?  I guess I could alter the statement a bit and then do some sort of query to find out if the record is found, but if I can just tell if there was a record updated, it'd save a step...

Thanks.
0
Comment
Question by:davexxxx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Accepted Solution

by:
acampoma earned 300 total points
ID: 12262523
you can set nocount off at the beginningof your statement
or  return @@RowCount
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 12262919
Capture the value of @@ROWCOUNT *immediately after* the UPDATE statement - otherwise the @@ROWCOUNT value gets overwritten by another statement.  For example:

begin

UPDATE pwreset SET UpdateDate = getdate()
             WHERE UserID = @UserID
-- *don't add any statements here!* (or save @@ROWCOUNT in a local var. first)
IF @@ROWCOUNT > 0
   select 'true' as result
else
   select 'false' as result
-- can add statements here if needed

end
SET NOCOUNT OFF
GO

0
 

Author Comment

by:davexxxx
ID: 12278247
perfect...  @@RowCount was what I was looking for.
0

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question