2015-12-01 27 views
1

請幫我調試下面的存儲過程的錯誤。 Whenevere我試圖執行我收到錯誤的SP作爲錯誤信息存儲過程中的錯誤已經有一個對象名爲

(受影響的142行(S)) 消息2714,級別16,狀態6,第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 

由於一噸

+0

我剛剛在工作時遇到了這個問題。請檢查是否已經有一個名爲'AOP_Monthly_GrowthRate_Acquisition'的表,但具有**不同的模式**。 – t1t1an0

回答

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 
0

選擇 FROM INTO

創建一個表,如果它已經存在它不能做(也許是從先前的執行)。

您可以切換到臨時表(帶#的前綴表名)。或者你也可以使用INSERT INTO。使用第二種方法,您可能需要在填充之前清除表格。

+1

謝謝你每一個。 –

0

也許你的用戶有不同的默認模式。嘗試在這裏放置模式名稱

INTO **dbo**.AOP_Monthly_GrowthRate_Acquisition