我使用类似下面如何在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