我正在使用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语句中创建表之前,我无法删除表。