Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i have the following SQL codes which is to display the output inside a table created. But every time i execute it, it will give me the following message:

There is already an object named '#OutPutTBL' in the database.

Below are my codes:
SQL
DROP TABLE #OutPutTBL

CREATE TABLE #OutPutTBL(TARGETDATE INT, ACTUALDATE INT, PERCENTAGE INT, MONTH VARCHAR(5))
SELECT SUM(CONVERT(INT, TargetDate)) AS TargetDate, SUM(CONVERT(INT, ActualDate)) AS ActualDate, SUM(CONVERT(INT, CompleteRate))/SUM(CONVERT(INT, TargetDate)) AS CompletePercentage, Months  
into #OutPutTBL
FROM #FORMULA
GROUP BY Months

SELECT* FROM #OutPutTBL


Any help would be appreciated.
Posted
Comments
syed shanu 1-Jul-14 21:15pm    
Chk like this :

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

CREATE TABLE #OutPutTBL(TARGETDATE INT, ACTUALDATE INT, PERCENTAGE INT, MONTH VARCHAR(5))
SELECT SUM(CONVERT(INT, TargetDate)) AS TargetDate, SUM(CONVERT(INT, ActualDate)) AS ActualDate, SUM(CONVERT(INT, CompleteRate))/SUM(CONVERT(INT, TargetDate)) AS CompletePercentage, Months
into #OutPutTBL
FROM #FORMULA
GROUP BY Months

SELECT * FROM #OutPutTBL
ArunRajendra 1-Jul-14 23:53pm    
Try to convert to Table variable instead of temp table. So that you need worry about dropping the table explicitly.

The select into will auto generate your temp table.

either you remove the create table of the temp table or you remove the into part of the select and add an insert into

SQL
DROP TABLE #OutPutTBL
 
--CREATE TABLE #OutPutTBL(TARGETDATE INT, ACTUALDATE INT, PERCENTAGE INT, MONTH VARCHAR(5))
SELECT SUM(CONVERT(INT, TargetDate)) AS TargetDate, SUM(CONVERT(INT, ActualDate)) AS ActualDate, SUM(CONVERT(INT, CompleteRate))/SUM(CONVERT(INT, TargetDate)) AS CompletePercentage, Months  
into #OutPutTBL
FROM #FORMULA
GROUP BY Months
 
SELECT* FROM #OutPutTBL


or remove the select into

SQL
DROP TABLE #OutPutTBL

CREATE TABLE #OutPutTBL(TARGETDATE INT, ACTUALDATE INT, PERCENTAGE INT, MONTH VARCHAR(5))

insert into #OutPutTBL
SELECT SUM(CONVERT(INT, TargetDate)) AS TargetDate, SUM(CONVERT(INT, ActualDate)) AS ActualDate, SUM(CONVERT(INT, CompleteRate))/SUM(CONVERT(INT, TargetDate)) AS CompletePercentage, Months
FROM #FORMULA
GROUP BY Months

SELECT* FROM #OutPutTBL
 
Share this answer
 
Comments
Jamie888 1-Jul-14 23:11pm    
currently i am writing the codes in a stored procedure. How can i make the select statement for #OutPutTBL? i tried SELECT @SQL = 'SELECT * FROM #OutPutTBL' and then EXEC sp_executesql @SQL but it does not come out.
Jose Pla 4-Jul-14 10:14am    
Change
SELECT @SQL = 'SELECT * FROM #OutPutTBL'
for
SET @SQL = 'SELECT * FROM #OutPutTBL'

if you still get nothing it could be temp table, you might try using a global temp table
##OutPutTBL instead of just #OutPutTBL

http://msdn.microsoft.com/en-us/library/ms187330.aspx
http://msdn.microsoft.com/en-us/library/ms189484.aspx

Also make sure you have already declared the variable @SQL
DECLARE @SQL varchar(500)
Why don't you try TABLE variables? They are much better then temp tables.

SQL
DECLARE @OutputTBL TABLE
(TARGETDATE INT, ACTUALDATE INT, PERCENTAGE INT, MONTH VARCHAR(5))

INSERT INTO @OutputTBL(TARGETDATE, ACTUALDATE, PERCENTAGE, MONTH)
SELECT SUM(CONVERT(INT, TargetDate)) AS TargetDate, SUM(CONVERT(INT, ActualDate)) AS ActualDate, SUM(CONVERT(INT, CompleteRate))/SUM(CONVERT(INT, TargetDate)) AS CompletePercentage, Months
FROM #FORMULA
GROUP BY Months

SELECT* FROM @OutPutTBL
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900