count in table

  • I know this is probably a dumb question, but I can't figure out how to do this.

    I want to set up a job step and with that step I want to count the rows in a table, if there are more than 0 then I want the step to error out. If there are 0 rows in the table I want the job step to finish successfully. Can someone help? This is where I can't figure out how to make it generate an error:

    declare @count int 

    select @count = count(*) from test_tbl

    IF @count > 0 

    begin

    print 'This is bad'

    end

    else

    begin

    print 'Good to go'

    END

    Thanks!

  • You should make use of the RAISERROR method. Read about it in Books Online.


    N 56°04'39.16"
    E 12°55'05.25"

  • Use Raiserror.  Use with log if you want the error to appear in the log file.

    select @count = count(*) from test_tbl

    IF @count > 0 

        raiserror('Your Error Message Here',16,1) with log

    else

        print 'Good to go'

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • probably obvious to everyone that you can avoid the variable and just us an if exists:

    IF exists(select * from test_tbl)

        raiserror('Your Error Message Here',16,1) with log

    else

        print 'Good to go'

     

    this is my 501st. post on SSC. woot!

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Viewing 4 posts - 1 through 4 (of 4 total)

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