Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Faster way to check if a record needs updating

Question posted by: abev (Newbie) on July 2nd, 2008 06:53 PM
First I check if the ABBR exists, then I update it. It seems like I am making 2 requests - 99% of the time I would never send the wrong parameters so it's just a check.

I was wondering if there was a way to ask AND update in one step?

[code]
ALTER PROCEDURE [dbo].[League_GetTeamAbbr]
(@LeagueID int, @TeamName nvarchar(50), @Abbr nvarchar(10) OUTPUT)
As
IF Exists(SELECT ABBR from League_TeamData WHERE TeamName = @TeamName AND LeagueID = @LeagueID)
BEGIN
SET @Abbr = (SELECT ABBR from League_TeamData WHERE TeamName = @TeamName AND LeagueID = @LeagueID)
RETURN 1
End
ELSE
RETURN 0
[code]
Delerna's Avatar
Delerna
Expert
562 Posts
July 2nd, 2008
11:09 PM
#2

Re: Faster way to check if a record needs updating
The system variable @@rowcount returns the number of rows affected by the previous statement.

So you could just do the update and return 0 or 1 depending on the value of @@rowcount. @@rowcount will be 0 if the record didn't exist

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,872 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Microsoft SQL Server Contributors