SQL Insert Into Temp Table in both If and Else Blocks

后端 未结 8 1930
自闭症患者
自闭症患者 2020-12-10 12:52

I\'m trying to populate a temp table based on the result of a condition in SQL 2005. The temp table will have the same structure either way, but will be populated using a d

相关标签:
8条回答
  • 2020-12-10 13:26

    In the scenario you provide you could do this

    DECLARE @Id int
    SET @Id = 1
    
    IF OBJECT_ID('tempdb..#MyTestTable') IS NOT NULL DROP TABLE #MyTestTable
    
    SELECT 
      CASE WHEN (@Id = 2) 
        THEN 'ABC' 
        ELSE 'XYZ' 
      END AS Letters
    INTO #MyTestTable;
    

    But otherwise you will need to create the table before the if statement like this

    Create Table #MyTestTable (
      MyValue varchar(3)
    )
    IF (@Id = 2) BEGIN 
      Insert Into (MyValue)
      SELECT 'ABC' AS Letters;
    END ELSE BEGIN
      Insert Into (MyValue)
      SELECT 'XYZ' AS Letters;
    END
    
    0 讨论(0)
  • 2020-12-10 13:27

    You could drop the table before SELECTing INTO it in both cases., e.g.:

    DECLARE @Id int 
    SET @Id = 1  
    
    IF (@Id = 2) BEGIN  
        IF OBJECT_ID('tempdb..#MyTestTable') IS NOT NULL DROP TABLE #MyTestTable
        SELECT 'ABC' AS Letters 
        INTO #MyTestTable; 
    END ELSE BEGIN 
        IF OBJECT_ID('tempdb..#MyTestTable') IS NOT NULL DROP TABLE #MyTestTable 
        SELECT 'XYZ' AS Letters 
        INTO #MyTestTable; 
    END 
    

    Update After Comment:

    That's annoying.

    How about two separate temp tables? Then after the If/Else login, check for the existence of each one and if it exists, select into a third temp table? That may not perform great, but whether that matters or not depends on what you need this for.

    0 讨论(0)
  • 2020-12-10 13:35

    I tried this:

    SELECT S1.* INTO #MytestTable
    FROM 
    (   SELECT 'ABC' AS Letters WHERE 1 = CASE @Id=2 THEN 1 ELSE 2 END
        UNION
        SELECT 'XYZ' AS Letters WHERE 1 = CASE @Id=1 THEN 1 ELSE 2 END
    ) AS S1
    

    This solution is better if later you need to add columns to #MyTestTable, because otherwise you must physically drop it before you re-run your script, which is annoying iin test conditions.

    0 讨论(0)
  • 2020-12-10 13:36

    This is an old issue, but for anyone else coming here:

    The dynamic SQL answer given by user Philip Kelley does not work for local temp tables (#Mytemp). What you can do is create dynamic SQL to insert it into a global temp table (##MyTemp) which can later be dropped.

    DECLARE @Command  varchar(500)
    
    DECLARE @Id int 
    SET @Id = 2
    
    IF OBJECT_ID('tempdb..#MyTestTable') IS NOT NULL DROP TABLE ##MyTestTable 
    
    IF (@Id = 2) BEGIN  
        SET @Command = 'SELECT ''ABC'' AS Letters INTO ##MyTestTable'
    END ELSE BEGIN 
        SET @Command = 'SELECT ''XYZ'' AS Letters INTO ##MyTestTable'
    END 
    
    EXECUTE (@Command)
    
    select * from ##MyTestTable
    
    DROP ##MyTestTable
    
    0 讨论(0)
  • 2020-12-10 13:38

    Here is a solution which I use if temp table can't be created upfront and don't want to put core logic in dynamic SQL.

    IF 1 = 1 -- Replace with actual condition
    BEGIN
        SELECT * INTO #tmp1 FROM dbo.Table1
    END
    ELSE
    BEGIN
        SELECT * INTO #tmp2 FROM dbo.Table2
    END
    
    -- Inserting data into global temp table so sql server can't complain on not recognizing in a context
    DECLARE @Command VARCHAR(MAX)
    IF OBJECT_ID('tempdb..#tmp1') IS NOT NULL
    BEGIN
        SET @Command = 'SELECT * INTO ##tmp FROM #tmp1'
    END
    ELSE
    BEGIN
        SET @Command = 'SELECT * INTO ##tmp FROM #tmp2'
    END
    
    EXECUTE(@Command)
    SELECT * INTO #tmpFinal FROM ##tmp -- Again passing data back to local temp table from global temp table to avoid seeing red mark
    
    IF OBJECT_ID('tempdb..##tmp') IS NOT NULL DROP TABLE ##tmp
    IF OBJECT_ID('tempdb..#tmp1') IS NOT NULL DROP TABLE #tmp1
    IF OBJECT_ID('tempdb..#tmp2') IS NOT NULL DROP TABLE #tmp2
    
    SELECT * FROM #tmpFinal
    
    IF OBJECT_ID('tempdb..#tmpFinal') IS NOT NULL DROP TABLE #tmpFinal
    
    0 讨论(0)
  • 2020-12-10 13:39

    The problem you’re having is not that you are populating the temp table, but that you’re trying to create the table. SQL parses your script and finds that you are attempting to create it in two different places, and so raises an error. It is not clever enough to realize that the “execution path” cannot possibly hit both of the create statemements. Using dynamic SQL will not work; I tried

    DECLARE @Command  varchar(500)
    
    DECLARE @Id int 
    SET @Id = 2
    
    IF OBJECT_ID('tempdb..#MyTestTable') IS NOT NULL DROP TABLE #MyTestTable 
    
    IF (@Id = 2) BEGIN  
        SET @Command = 'SELECT ''ABC'' AS Letters INTO #MyTestTable'
    END ELSE BEGIN 
        SET @Command = 'SELECT ''XYZ'' AS Letters INTO #MyTestTable'
    END 
    
    EXECUTE (@Command)
    
    select * from #MyTestTable
    

    but the temp table only lasts as long as the dynamic session. So, alas, it looks like you’ll have to first declare the table and then populate it. Awkward code to write and support, perhaps, but it will perform efficiently enough.

    0 讨论(0)
提交回复
热议问题