【发布时间】:2017-04-09 08:20:52
【问题描述】:
我在不同条件下使用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时,它会抛出错误“数据库中已经有一个名为'#tempQuantity'的对象。”
我理解错误。但它不会同时创建 2 个临时表。那为什么它会抛出。那么我怎样才能创建这样的临时表
注意
在第二个 ELSE IF 语句中创建表之前,我也不能放弃
【问题讨论】:
标签: sql sql-server stored-procedures temp-tables