If Table Empty

  • Hello, I am new to SQl so sorry for the simple question. I have a stored procedure that is running every 5 minutes and throws out a XML file. It works fine except that it throws the file out even if the table is empty. How do I put an if statement into the SQL script to check to see if the table is empty before it proceeds? Thanks.

  • Do like so in your SP

    if (select count(*) from tblname) != 0

    begin

    --do your xml stuff here

    end

  • Worked Perfectly. Thanks alot for your help.

  • This is a small modification that is designed to boost performance:

    if exists(select * from tblname)

    begin

    --do your xml stuff here

    end

    The EXISTS will terminate the select after the first record is found while the COUNT(*) will perform the entire select.

    HTH

    Steve Hendricks

    MCSD, MCDBA

    AFS Consulting Group

    shendricks@afsconsulting.com

    (949) 588-9800 x15


    Steve Hendricks
    MCSD, MCDBA
    Data Matrix

    shendricks@afsconsulting.com
    (949) 588-9800 x15

  • Oooooh, aaaaaw.

    Cool, hadn't thought of that before.

  • Thanks to both of you for helping me out.

  • That seems to be the case... but we have found that the internals seem to be more efficient with the COUNT than EXISTS (at least in tables with relatively small quantities of data...< 1000). You might want to run some tests is efficiency is an issue. Not a big deal, though. Even in our tests, the difference was only about 7%.

    Guarddata-

Viewing 7 posts - 1 through 6 (of 6 total)

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