Determining # of rows

  • Is there a way to determine how many rows are being returned in TSQL? I need to throw an error if it returns 0 rows.

    Matt

  • 
    
    IF @@ROWCOUNT = 0 BEGIN
    -- RAISE AN ERROR
    END

    Watch out though, @@ROWCOUNT is reset by each statement - so checking it with an IF statement resets it. If you want to return the value you need to capture it straight away.

    ie.

    
    
    -- do something
    SELECT @rc = @@ROWCOUNT
    IF @rc > 0 BEGIN
    RETURN @RC
    END
  • Use the @@rowcount Global Variable...

    declare @iCount int

    select * from sysobjects where type = 'u'

    select @iCount = @@rowcount

    if @iCount = 0

    begin

    RAISERROR('Error: 0 Rows returned.',16,1)

    return

    end

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply