Best way to skip code if certain condition not met in stored procedure

  • I have a stored procedure with several insert into statements. On occasion one of the insert into queries doesn't return any data. What is the best way to test for no records then, skip that query?

  • Well the way you test for a query not returning/returning records is by running the query..... so what exactly is the problem with running the insert into if no records are returned?

  • There are several queries that produce a result. The result gets messed up if this query is not skipped.

  • Well without knowing more about what you're trying to do it's hard to say but have you tried checking @@ROWCOUNT after your insert into

    INSERT INTO MY_TABLE(....) SELECT ......

    IF @@ROWCOUNT > 0

    BEGIN

    /*Do your stuff here*/

    END

  • Thanx. I'll give it a try.

  • Another option, according to what I understood.

    IF EXISTS(SELECT 1 FROM MyQuery)

    INSERT INTO MyDestination( mycolumns)

    SELECT mycolumns FROM MyQuery

    The exists will stop at the first returned row, so it should be fast.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

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

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