Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I use below code to create procedure to using temp table

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

When I want to run the procedure with exec testTempTable says

Invalid object name ‘#resultTbl’.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I use temp table in the procedure?

>Solution :

Because your temp table might not be created, so you can’t get result set from #resultTbl. you can try to use SELECT ... INTO temp table or create a temp table before you use it.

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading