Rajee Kasthuri
Rajee Kasthuri

Reputation: 159

Error In Stored Procedure with error message as There is already an object named

Please help me to debug the error for below stored Procedure. Whenevere i tried to execute the sp i am getting error as

(142 row(s) affected) Msg 2714, Level 16, State 6, Line 16 There is already an object named 'AOP_Monthly_GrowthRate_Acquisition' in the database.

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

Thanks a ton

Upvotes: 1

Views: 488

Answers (3)

AdrianChodkowski
AdrianChodkowski

Reputation: 114

Maybe you have different default schema for your user. Try to put schema name here

 INTO **dbo**.AOP_Monthly_GrowthRate_Acquisition

Upvotes: 0

Devart
Devart

Reputation: 121932

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

Upvotes: 1

David Rushton
David Rushton

Reputation: 5030

SELECT FROM INTO

Creates a table, which it cannot do if it already exits (perhaps from a previous execution).

You could swap to a temp table (prefix table name with #). Or you could use INSERT INTO instead. With the second method you may need to clear the table before populating.

Upvotes: 0

Related Questions