Solved

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

Posted on 2004-10-08
3
1,298 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
3 Comments
 
LVL 6

Accepted Solution

by:
acampoma earned 75 total points
Comment Utility
you can set nocount off at the beginningof your statement
or  return @@RowCount
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
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
Comment Utility
perfect...  @@RowCount was what I was looking for.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now