Search code examples
sqltemporary

Temporary Table


Can I create temporary table within case statement..?

if yes please explain me with example.

actually I want to do something like this..

declare @a int = 1

select case when @a = 1 then
'yes'
else
'no'
end

if first case execute then I would like to create temp table with that condition which is require to manipulate some business data.

in else case I would like to create another table with other condition to manipulate business data.


Solution

  • Two interpretations of what I believe you can mean:

    Have columns with different data:

    SELECT CASE WHEN @a = 1 THEN
             'yes'
           ELSE
             'no'
           END AS answer
    INTO tempTable
    

    Create a table with different queries:

    IF @a = 1
      SELECT 'yes' AS answer
      INTO tempTable2
    ELSE
      SELECT 'no' AS answer, 'other field' as other
      INTO tempTable2
    

    SQLFiddle.