这是我的代码
create proc TEMP
AS
BEGIN
DECLARE @SQL nvarchar(4000)
IF OBJECT_ID(N'tempdb..#TEMP1') IS NOT NULL
DROP TABLE #TEMP1;
SET @SQL ='SELECT CUSTOMERS,AREA,HOUSEHOLDS'+CHAR(10)
SET @SQL = @SQL +'INTO #TEMP1'+CHAR(10)
SET @SQL = @SQL +'FROM NEW'+CHAR(10)
PRINT(@SQL)
EXEC (@SQL)
IF OBJECT_ID(N'tempdb..#TEMP2') IS NOT NULL
DROP TABLE #TEMP2;
SET @SQL ='SELECT CUSTOMERS,AREA,VEHICELS'+CHAR(10)
SET @SQL = @SQL +'INTO #TEMP2'+CHAR(10)
SET @SQL = @SQL +'FROM OLD'+CHAR(10)
PRINT(@SQL)
EXEC (@SQL)
IF OBJECT_ID(N'tempdb..#TEMP3') IS NOT NULL
DROP TABLE #TEMP3;
SET @SQL ='SELECT 0.VEHICELS,C.HOUSEHOLDS'+CHAR(10)
SET @SQL = @SQL +'INTO #TEMP3'+CHAR(10)
SET @SQL = @SQL +'FROM #TEMP1 C'+CHAR(10)
SET @SQL = @SQL +'INNER JOIN #TEMP2 O '+CHAR(10)
SET @SQL = @SQL +'on C.CUSTOMERS=O.CUSTOMERS'+CHAR(10)
SET @SQL = @SQL +'AND C.AREA=O.AREA'+CHAR(10)
PRINT(@SQL)
EXEC (@SQL)
END
我的错误:
Msg 208, Level 16, State 0, Line 1
Invalid object name '#TEMP1'.
首先 TEMP1 和 TEMP2 运行良好,但是当涉及到 TEMP3 时,它不会访问存在数据的 TEMP1
你们能检查一下这个令人困惑的问题吗???
DECLARE @sql varchar(max)
SET @sql = 'CREATE TABLE ##T1 (Col1 varchar(20))'
EXEC(@sql)
INSERT INTO ##T1 (Col1) VALUES ('This will work.')
SELECT * FROM ##T1
如果我们使用全球临时表,我们将来会遇到任何问题吗???