14

I am having three tables with different data and i need to insert into one TEMP table and return that table in StoredProcedure.

I tried as:

-- To get last 10 Days Letters count
SELECT col1,col2,1 AS Type, LettersCount
INTO #temp FROM tblData

-- To get last 4 weeks Letters count
SELECT col1,col2,2 AS Type, LettersCount
INTO #temp FROM tblData

-- To get month wise Letters count
SELECT col1,col2,3 AS Type, LettersCount
INTO #temp FROM tblData

Showing Error as

Msg 2714, Level 16, State 1, Line 16
There is already an object named '#temp ' in the database.
Msg 102, Level 15, State 1, Line 24
Incorrect syntax near 'T'.
Msg 2714, Level 16, State 1, Line 32
There is already an object named '#temp ' in the database.
Dinesh Reddy Alla
  • 1,677
  • 9
  • 23
  • 47

5 Answers5

30

You can Check it Already Exists or NOT

IF OBJECT_ID ('tempdb..#TempLetters') is not null
drop table #TempLetters


SELECT col1,col2,1 AS Type, LettersCount
INTO #TempLetters FROM tblData

-- To get last 4 weeks Letters count
INSERT INTO #TempLetters
SELECT col1,col2,2 AS Type, LettersCount
FROM tblData

-- To get month wise Letters count
INSERT INTO #TempLetters
SELECT col1,col2,3 AS Type, LettersCount
FROM tblData
tjeloep
  • 318
  • 1
  • 4
  • 19
Dgan
  • 10,077
  • 1
  • 29
  • 51
8

Create the temporary table once, then insert into it for the other two SELECT statements:

SELECT col1, col2, 1 AS Type, LettersCount
  INTO #temp
  FROM tblData;

INSERT INTO #temp
    SELECT col1, col2, 2 AS Type, LettersCount
      FROM tblData;

INSERT INTO #temp
    SELECT col1, col2, 3 AS Type, LettersCount
      FROM tblData;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
5

The SELECT INTO statement can also be used to create a new, empty table using the schema of another select * into tablename from .. here tablename table should not exist.

Change your insert like this:

SELECT col1,
       col2,
       1 AS Type,
       LettersCount
INTO   #temp
FROM   tblData

-- To get last 4 weeks Letters count
INSERT INTO #temp
SELECT col1,col2,2 AS Type,LettersCount
FROM   tblData

-- To get month wise Letters count
INSERT INTO #temp
SELECT col1,col2,3 AS Type,LettersCount
FROM   tblData 
Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
2

Why not write just a single insert statement and union the tables before insert

with A as
(
    -- To get last 10 Days Letters count
    SELECT col1,col2,1 AS Type, LettersCount
    FROM tblData
    union all
    -- To get last 4 weeks Letters count
    SELECT col1,col2,2 AS Type, LettersCount
    FROM tblData
    union all
    -- To get month wise Letters count
    SELECT col1,col2,3 AS Type, LettersCount
    FROM tblData
)
select col1, col2, Type, LettersCount
INTO #temp 
FROM A

This will help you add more tables in the select easily if you need as you wont need any more insert statements for them

Hitesh
  • 3,449
  • 8
  • 39
  • 57
1

The error occurs because the first select into statement creates the table and the second and third tries to recreate it again.

Change the second and third queries into:

insert into #temp
select..
Eduard Uta
  • 2,477
  • 5
  • 26
  • 36