Faster way to check if a record needs updating

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abev
    New Member
    • Jan 2008
    • 22

    #1

    Faster way to check if a record needs updating

    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_GetTeamA bbr]
    (@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
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    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

    Comment

    Working...