存储过程中出错,并显示错误消息,因为已存在名为的对象

时间:2015-12-01 08:38:59

标签: sql sql-server

请帮我调试以下存储过程的错误。 Whenevere我试图执行sp我正在收到错误

(142对(s)受影响) Msg 2714,Level 16,State 6,Line 16 已有一个名为“AOP_Monthly_GrowthRate_Acquisition”的对象'在数据库中。

if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 
  'AOP_Monthly_GrowthRate_Acquisition' AND TABLE_SCHEMA = 'dbo')
    drop table BudgetWorld_temp.dbo.AOP_Monthly_GrowthRate_Acquisition

GO

SELECT               dbo.LTM_ACQUISITION_MONTHLY.Year, 
                     dbo.LTM_ACQUISITION_MONTHLY.Month, 
                     dbo.LTM_ACQUISITION_MONTHLY.SALES_MANAGER_CODE, 
                     dbo.LTM_ACQUISITION_MONTHLY.SALES_GROUP, 
                    dbo.LTM_ACQUISITION_MONTHLY.NetProductSales, 
                     dbo.LTM_ACQUISITION_MONTHLY.AverageCostBasedProfit, 
                     dbo.LTM_ACQUISITION_MONTHLY.ReplacementCostBasedProfit, 
            isnull(dbo.[Provided 2016 AOP Values_Acquisition].[AOP Sales],0) as 'AOP Sales', 
                     isnull(dbo.[Provided 2016 AOP Values_Acquisition].[AOP   
Profit Based On Average Cost],0) as 'AOP Profit Based On Average Cost'
INTO #TEMP3
FROM            dbo.LTM_ACQUISITION_MONTHLY LEFT OUTER JOIN
                         dbo.[Provided 2016 AOP Values_Acquisition] ON   
dbo.LTM_ACQUISITION_MONTHLY.Month = dbo.[Provided 2016 AOP   
Values_Acquisition].Month AND 
                         dbo.LTM_ACQUISITION_MONTHLY.SALES_MANAGER_CODE = 
dbo.[Provided 2016 AOP Values_Acquisition].[Sales Manager] AND 
                         dbo.LTM_ACQUISITION_MONTHLY.SALES_GROUP = dbo.  
[Provided 2016 AOP Values_Acquisition].[Sales Group]

SELECT *,
SALES_GROWTH = CASE  
  WHEN ([NetProductSales] + [AOP Sales]) = 0 THEN 0
  WHEN ([AOP Sales] ) > 0 AND ([NetProductSales]) = 0 THEN 1
  ELSE
  (([AOP Sales] -[NetProductSales])/[NetProductSales]) END
    ,
  Margin_Growth = 

  CASE  
  WHEN ([AverageCostBasedProfit] + [AOP Profit Based On Average Cost]) = 0   
THEN 0
  WHEN ([AOP Profit Based On Average Cost] ) > 0 AND   
([AverageCostBasedProfit]) = 0 THEN 1

  else
 (([AOP Profit Based On Average Cost] -  
[AverageCostBasedProfit])/[AverageCostBasedProfit]) END

 INTO AOP_Monthly_GrowthRate_Acquisition

FROM #TEMP3
DROP TABLE #TEMP3

非常感谢

3 个答案:

答案 0 :(得分:1)

USE BudgetWorld_temp
GO

IF OBJECT_ID('dbo.AOP_Monthly_GrowthRate_Acquisition', 'U') IS NOT NULL
    DROP TABLE dbo.AOP_Monthly_GrowthRate_Acquisition
GO

IF OBJECT_ID('tempdb.dbo.#TEMP3') IS NOT NULL
    DROP TABLE #TEMP3
GO

SELECT
    a.[Year], 
    a.[Month], 
    a.SALES_MANAGER_CODE, 
    a.SALES_GROUP, 
    a.NetProductSales, 
    a.AverageCostBasedProfit, 
    a.ReplacementCostBasedProfit, 
    ISNULL(b.[AOP Sales], 0) AS [AOP Sales], 
    ISNULL(b.[AOP Profit Based On Average Cost], 0) AS [AOP Profit Based On Average Cost]
INTO #TEMP3
FROM dbo.LTM_ACQUISITION_MONTHLY a
LEFT JOIN dbo.[Provided 2016 AOP Values_Acquisition] b ON a.[Month] = b.[Month]
    AND a.SALES_MANAGER_CODE = b.[Sales Manager]
    AND a.SALES_GROUP = b.[Sales Group]

SELECT *
    , SALES_GROWTH =
        CASE  
            WHEN [NetProductSales] + [AOP Sales] = 0 THEN 0
            WHEN [AOP Sales] > 0 AND [NetProductSales] = 0 THEN 1
            ELSE (([AOP Sales] -[NetProductSales])/[NetProductSales])
        END
    , Margin_Growth = 
        CASE  
            WHEN ([AverageCostBasedProfit] + [AOP Profit Based On Average Cost]) = 0 THEN 0
            WHEN ([AOP Profit Based On Average Cost] ) > 0 AND ([AverageCostBasedProfit]) = 0 THEN 1
            ELSE (([AOP Profit Based On Average Cost] - [AverageCostBasedProfit])/[AverageCostBasedProfit])
        END
INTO dbo.AOP_Monthly_GrowthRate_Acquisition
FROM #TEMP3

IF OBJECT_ID('tempdb.dbo.#TEMP3') IS NOT NULL
    DROP TABLE #TEMP3

答案 1 :(得分:0)

选择 从 INTO

创建一个表,如果它已经退出,它就无法执行(可能来自之前的执行)。

您可以交换到临时表(前缀表名称带#)。或者您可以使用INSERT INTO代替。使用第二种方法,您可能需要在填充之前清除表。

答案 2 :(得分:0)

也许您的用户有不同的默认架构。尝试将模式名称放在此处

 INTO **dbo**.AOP_Monthly_GrowthRate_Acquisition