存储过程中的临时表是否未访问数据?

时间:2013-08-22 13:26:54

标签: sql tsql stored-procedures sql-server-2008-r2 temp-tables

这是我的代码

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

如果我们使用GLOBALTEMPORARY TABLES,我们是否会在未来遇到任何问题?

2 个答案:

答案 0 :(得分:0)

一旦exec完成执行#temp表被删除 也许如果你尝试使用更广泛的范围,它可能会成功,我不知道。

尝试这样的事情。

create proc TEMP
AS
BEGIN
    DECLARE @now datetime;
    DECLARE @TempTableTableSuffix sysname
    SET @now = GETDATE()

    select @TempTableTableSuffix = CONVERT(VARCHAR, CONVERT(int,RAND(DATEPART(MILLISECOND,@now)+1000*(DATEPART(SECOND,@now)+60*(DATEPART(MINUTE,@now)+60*DATEPART(HOUR,@now)))) * 100000000))

    DECLARE @SQL nvarchar(4000)
    SET @SQL ='SELECT CUSTOMERS,AREA,HOUSEHOLDS'+CHAR(10)
    SET @SQL = @SQL +'INTO ##TEMP1' + @TempTableTableSuffix + CHAR(10)
    SET @SQL = @SQL +'FROM NEW'+CHAR(10)
    PRINT(@SQL)
    EXEC (@SQL)
    SET @SQL ='SELECT CUSTOMERS,AREA,VEHICELS'+CHAR(10)
    SET @SQL = @SQL +'INTO ##TEMP2' + @TempTableTableSuffix + CHAR(10)
    SET @SQL = @SQL +'FROM OLD'+CHAR(10)
    PRINT(@SQL)
    EXEC (@SQL)
    SET @SQL ='SELECT 0.VEHICELS,C.HOUSEHOLDS'+CHAR(10)
    SET @SQL = @SQL +'INTO ##TEMP3'+CHAR(10)
    SET @SQL = @SQL +'FROM ##TEMP1' + + @TempTableTableSuffix + '  C'+CHAR(10)
    SET @SQL = @SQL +'INNER JOIN #TEMP2'+ @TempTableTableSuffix  + ' 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

无论如何,我认为你不需要dynamic SQL

答案 1 :(得分:0)

使用单个EXEC -

DECLARE @SQL nvarchar(MAX)
IF OBJECT_ID(N'tempdb..#TEMP1') IS NOT NULL
    DROP TABLE #TEMP1;
IF OBJECT_ID(N'tempdb..#TEMP2') IS NOT NULL
    DROP TABLE #TEMP2;    
IF OBJECT_ID(N'tempdb..#TEMP3') IS NOT NULL
    DROP TABLE #TEMP3;        


SET @SQL = ' SELECT CUSTOMERS,AREA,HOUSEHOLDS'+CHAR(10)
SET @SQL = @SQL + ' INTO #TEMP1'+CHAR(10)
SET @SQL = @SQL + ' FROM NEW'+CHAR(10)

SET @SQL = @SQL + ' SELECT CUSTOMERS,AREA,VEHICELS'+CHAR(10)
SET @SQL = @SQL + ' INTO #TEMP2'+CHAR(10)
SET @SQL = @SQL + ' FROM OLD'+CHAR(10)
SET @SQL = @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)