在存储过程中使用 (insert and select) temp table

Use (insert and select) temp table in the store procedure

我使用下面的代码创建使用临时文件的过程table

Go
create procedure testTempTable
as
    INSERT INTO #resultTbl (code,userName) SELECT code,userName FROM Customer
    select * from #resultTbl
Go

当我想 运行 程序 exec testTempTable

Invalid object name '#resultTbl'.

如何在程序中使用临时 table?

因为您的临时 table 可能未创建,所以您无法从 #resultTbl 获取结果集。您可以尝试使用 SELECT ... INTO temp table 或在使用前创建一个临时 table。

create procedure testTempTable
as
BEGIN
    SELECT code,userName 
    INTO #resultTbl
    FROM Customer
    
    SELECT * 
    FROM #resultTbl
    
END
Go