如何在SQL Server的ELSE IF语句中创建相同的临时表?

4

我正在使用else if语句根据不同的条件将数据存储在'#tempQuantity'临时表中,如下所示:

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>=@datefrom)) and ((@DateTo is null) or (Time<=@dateTo))
    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>=@datefrom)) and ((@DateTo is null) or (Time<=@dateTo))
    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>=@datefrom)) and ((@DateTo is null) or (Time<=@dateTo))
    GROUP BY Year(Time),DATEPART(WEEK,Time),ItemID,StoreID
END

执行这个“Alter stored procedure”时,会抛出错误“There is already an object named '#tempQuantity' in the database.” 我理解这个错误,但它不会同时创建两个临时表。那么为什么会抛出这个错误呢?那么我该怎样创建这样的临时表呢?
注意:在第二个ELSE IF语句中创建表之前,我无法删除表。
3个回答

5

您需要先创建临时表。

然后在任何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条件语句中,请使用INSERT..INTO代替SELECT..INTO


3
  1. You could declare a local table and insert data by 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. Or you could create #temp table and insert data

    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         
    

-1

你应该尝试一下

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

如果您不想从临时表中获取数据,可以删除临时表中的现有数据。


1
请仔细阅读我的问题。我正在“if else语句”中插入记录,它会抛出编译时错误“数据库中已经有对象”。if else语句只执行一次记录,而不是多次。 - Liam neesan
我不确定SQL是如何允许你首先创建SP的。 USE Northwind; go CREATE PROCEDURE SP1 AS BEGIN SELECT * INTO #TempOrders from Orders; SELECT * INTO #TempOrders from Orders; END 我尝试了上面的脚本,但SQL不允许我创建SP。 - Vidyadhar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接