2017-04-09 365 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