IF ELSE Query

  • I have written an IF ELSE statement into a sproc but I'd like to display the results (as in counts) from the statement in the "Results" pane after the sproc runs. How do I accomplish this?

    IF EXISTS ({SELECT Query})

    BEGIN

    {INSERT Version 1}

    END

    ELSE

    BEGIN

    {INSERT version 2}

    END

    Thx.

    John

  • Do you just want to know how many rows were inserted? You could consider @@ROWCOUNT or the OUTPUT clause.

    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
  • To obtain data from a stored procedure you need to have a select. A function can 'return' data if that's what you're looking for-- If you're looking to return something based on some condition.

    Below is an extremely basic example of selecting data from a temp table in a SProc

    declare @Table_A Table (--Table Column Names and cast types)

    --if conditions

    insert into @Table_A

    --else conditions

    insert into @Table_A

    select columnNameWithResults from @Table_A

  • I am not clear with your requirement..can you please some more detailed information to us.......

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Correct, Luis.

    I wanted to see how many records were inserted.

  • latingntlman (7/22/2013)


    Correct, Luis.

    I wanted to see how many records were inserted.

    Then you could simply use

    SELECT @@ROWCOUNT

    Beware of the possible resets of the @@ROWCOUNT value.

    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
  • Voide (7/19/2013)


    To obtain data from a stored procedure you need to have a select. ...[/code]

    The OUTPUT clause of a DELETE will do just fine ๐Ÿ˜‰

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

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

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