Solved

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

Posted on 2004-10-08
3
1,300 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 75 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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…

730 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