【问题标题】:How to create same Temporary table in ELSE IF Statement in SQL Server?如何在 SQL Server 的 ELSE IF 语句中创建相同的临时表?
【发布时间】: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


    【解决方案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         
      

    【讨论】:

      【解决方案2】:

      您需要先创建临时表。

      然后在任何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 条件中使用 INSERT..INTO 而不是 SELECT..INTO

      【讨论】:

        【解决方案3】:

        你应该试试

        IF OBJECT_ID('tempdb..#tempQuantity') IS NULL
            SELECT * INTO #tempQuantity...
        ELSE 
            INSERT INTO  #tempQuantity
        

        如果您不想要临时表中的数据,您可以从临时表中删除现有数据。

        【讨论】:

        • 请正确阅读我的问题。我在if else statement 中插入记录,它会引发编译时错误数据库中已经有对象。 if else 语句只执行一次记录,而不是多次
        • 我不确定 sql 如何让您首先创建 SP。使用北风;开始创建过程 SP1 选择 * INTO #TempOrders from Orders;从订单中选择 * INTO #TempOrders; END 我试过上面的脚本,SQL 不允许我创建 SP。
        猜你喜欢
        • 2017-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-06
        • 2019-10-03
        • 1970-01-01
        相关资源
        最近更新 更多