我有三个具有不同数据的表,我需要插入一个临时表并在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.发布于 2014-12-12 06:45:59
您可以检查它是否已经存在。
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发布于 2014-12-12 06:40:44
创建临时表一次,然后为其他两个SELECT语句插入该表:
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;发布于 2014-12-12 06:40:36
还可以使用tablename表中不存在的另一个select * into tablename from ..的模式,使用SELECT INTO语句创建一个新的空表。
将插入的内容更改如下:
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 https://stackoverflow.com/questions/27438169
复制相似问题