Passing case statements into a variable possible???

  • Greetings SQL Gurus,

    I have a quick question. Anyone know how, or if it is possible, to pass a case statement into a variable? lets say I have the following:

    declare @myvariable

    set = "case statement"

    Select a, b, @myvariable as c

    from mytable

    anyway to set the variable = to a query with a case statement??? I know this can be done inside SSIS using the execute sql task, but how about using SSMS???

    HELP!??!

    Al

  • No, you can't do that. Not that way. A variable is just a place holder and is not resolved into TSQL on the fly. What you can do, and I don't recommend it, is use variables to create a string and then execute the string as dynamic SQL.

    ----------------------------------------------------The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood... Theodore RooseveltThe Scary DBAAuthor of: SQL Server 2017 Query Performance Tuning, 5th Edition and SQL Server Execution Plans, 3rd EditionProduct Evangelist for Red Gate Software

  • This is duplicate post to one posted on T-SQL(SS2K8).

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Thanks! I was hoping to avoid Dynamic SQL, but I guess it is something I am just going to have to deal with 😀 Thanks Again.

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

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