2017-04-09 373 views
1

我使用類似下面如何在SQL Server中的ELSE IF語句中創建相同的臨時表?

IF(@GroupKey = 1) 
BEGIN 
    SELECT 
     ItemID, 
     StoreID, 
     sum(Qty) Quantity, 
     sum(ExtendedPrice) ExtendedPrice, 
     sum(ExtendedCost) ExtendedCost 
    into #tempQuantity 
    FROM 
     dbo.F_ItemDailySalesParent 

    WHERE 
     ((@DateFrom is null) or (Time>[email protected])) and ((@DateTo is null) or (Time<[email protected])) 
    GROUP BY ItemID,StoreID 
END 
ELSE IF(@GroupKey = 2) 
BEGIN 
    SELECT 
     Year(Time), 
     ItemID, 
     StoreID, 
     sum(Qty) Quantity, 
     sum(ExtendedPrice) ExtendedPrice, 
     sum(ExtendedCost) ExtendedCost 
    into #tempQuantity 
    FROM 
     dbo.F_ItemDailySalesParent 

    WHERE 
     ((@DateFrom is null) or (Time>[email protected])) and ((@DateTo is null) or (Time<[email protected])) 
    GROUP BY Year(Time),ItemID,StoreID 
END 
ELSE 
BEGIN 
    SELECT 
     Year(Time), 
     DATEPART(WEEK,Time), 
     ItemID, 
     StoreID, 
     sum(Qty) Quantity, 
     sum(ExtendedPrice) ExtendedPrice, 
     sum(ExtendedCost) ExtendedCost 
    into #tempQuantity 
    FROM 
     dbo.F_ItemDailySalesParent 

    WHERE 
     ((@DateFrom is null) or (Time>[email protected])) and ((@DateTo is null) or (Time<[email protected])) 
    GROUP BY Year(Time),DATEPART(WEEK,Time),ItemID,StoreID 
END 

else if聲明儘管執行該Alter stored procedure存儲在不同的條件數據爲「#tempQuantity」臨時表,它會拋出錯誤「目前已經在名爲‘#tempQuantity’對象數據庫。」

我明白錯誤。但它不會同時創建2個臨時表。那爲什麼會拋出。然後,我怎樣才能創建這樣

注意臨時表

我不能降得,纔在第二ELSE創建表IF語句

回答

3

您需要創建首先是臨時表。

然後在任何IF..ELSE聲明中使用INSERT..INTO

使用表變量不是一個好主意,因爲它會有性能問題。

輕鬆創建臨時表,使用下面的代碼在腳本

-- check if table exists 
IF OBJECT_ID('tempdb..#tempQuantity') IS NULL 
    DROP TABLE #tempQuantity 

-- simply create the temp table using 1=2 in where clause 
SELECT 
    Year(Time), 
    ItemID, 
    StoreID, 
    sum(Qty) Quantity, 
    sum(ExtendedPrice) ExtendedPrice, 
    sum(ExtendedCost) ExtendedCost 
into #tempQuantity 
FROM 
    dbo.F_ItemDailySalesParent 
where 1=2 

年初然後在你的所有的IF條件

0

你應該嘗試

IF OBJECT_ID('tempdb..#tempQuantity') IS NULL 
    SELECT * INTO #tempQuantity... 
ELSE 
    INSERT INTO #tempQuantity 

如果你不想要臨時表的數據,你可以de從臨時表中提取現有數據。

+0

使用INSERT..INTO代替SELECT..INTO請妥善閱讀我的問題。我在'if else語句'中插入記錄,它會引發編譯時錯誤**數據庫**中已經有對象。如果else語句只執行一次記錄,而不是多次執行 –

+0

我不確定sql是如何讓你首先創建SP的。 使用Northwind; go CREATE PROCEDURE SP1 AS BEGIN SELECT * INTO #TempOrders from Orders; SELECT * INTO #TempOrders from Orders; END我嘗試了上面的腳本,SQL不允許我創建SP。 – Vidyadhar

1
  1. 你可以聲明本地表和insert into ... select...

    DECLARE @TempTb AS TABLE (Id int) 
    IF(@GroupId = 1) 
    BEGIN 
    
        INSERT INTO @TempTb 
        SELECT 1 
    END 
    ELSE 
    BEGIN 
    
        INSERT INTO @TempTb 
        SELECT 1 
    END 
    
  2. 插入數據,或者你可以創建#temp表並插入數據

    IF OBJECT_ID('tempdb..##temptb') IS NOT NULL 
        BEGIN 
         DROP TABLE #temptb 
        END 
    
    CREATE TABLE #temptb (Id int) 
    
    IF(@GroupId = 1) 
    BEGIN 
    
        INSERT INTO #temptb 
        SELECT 1 
    END 
    ELSE 
    BEGIN 
    
        INSERT INTO #temptb 
        SELECT 1 
    END