我有三个包含不同数据的表,我需要插入一个 TEMP 表并在 StoredProcedure 中返回该表。
我试过:
-- 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
将错误显示为
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.
最佳答案
您可以检查它是否已经存在
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
关于sql - 如何在临时表中插入多个选择语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27438169/