determining @count > 0

  • I'm trying to detect if a SELECT statement will be returning at least one row

    before executing subsequent work.

    Rather than use the following slow code:

       set @Count = (select count(*) uid from a_big_table inner join a_bigger_table on uid)

       if @Count > 0 ...

    I came up with the faster:

       set @AtLeastOne = (

       select count(*) from one_row_dummy_table

       where (select  top 1 uid from a_big_table inner join a_bigger_table on uid)

       is not null

       )

       if @AtLeastOne > 0 ...

    But it still feels ungainly.  Any good suggestions?

      

  • What about

    IF EXISTS(select uid from a_big_table inner join a_bigger_table on uid)

    BEGIN

    ...

    END

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Thanks, that seems to work.  (Sorry for the "newbie" question.)

  • No problem and I would not class it as a 'newbie' question. Like all things you either know the answer or not. The art is in knowing who to ask or where to look.

    I only really understood the use of and workings of EXISTS through this forum.

     

    Far away is close at hand in the images of elsewhere.
    Anon.

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

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