case Statement with Cte

  • Hi

    I am getting data from Cte

    I want if @ICode parameter is not null then select statement should execute with parameter else all data

    SELECT * from cteCab where icode = @ICode

    else select * from ctetab



  • I answered this question yesterday. Why are you asking it again?

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • @jagjitsingh,

    When I run into problems like the one's you're having, I go back to the basics.

    The first step is to get out of the dynamic SQL world and get some regular code to work properly first.  Then, you can work on making it dynamic and you'll be much more successful.

    In fact... I lied a bit.  I don't run into problems like the one's you're having because I always start with working code first.  Then I convert it to dynamic SQL one parameter at a time.

    --Jeff Moden

    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Seems like this user is posting exam questions on the forum.


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

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