我使用類似下面如何在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語句
使用
INSERT..INTO
代替SELECT..INTO
請妥善閱讀我的問題。我在'if else語句'中插入記錄,它會引發編譯時錯誤**數據庫**中已經有對象。如果else語句只執行一次記錄,而不是多次執行 –我不確定sql是如何讓你首先創建SP的。 使用Northwind; go CREATE PROCEDURE SP1 AS BEGIN SELECT * INTO #TempOrders from Orders; SELECT * INTO #TempOrders from Orders; END我嘗試了上面的腳本,SQL不允許我創建SP。 – Vidyadhar