Creating and deleting temp tables consecutively in a stored procedure

左心房为你撑大大i 提交于 2020-01-16 22:00:12

问题


This code doesn't work when I run it and I don't understand why it shouldn't, the error is that a table already exists so it doesn't seem so much as an actual bug but a bug in the verification process, perhaps I am wrong. it's something from a procedure so doesn't work to use go's everywhere.

in my first question I grossly oversimplified my problem

these were my constraints :

I use temporary tables to communicate between procedures, in this case I needed to use the same procedure twice but with different parameters so i needed the table "#a" to be informed twice. Because the way of working is to create a temporary table before executing the procedure and in that form the procedure will know if it needs to do a select or inform the table. If it informs the table it is also responsible to create column (this is done to avoid dependencies between procedures). so I can't reuse the same table because the columns have been create already.

The solution I found was to create another stored procedure which will do the work for the second part, I could have changed the procedure sp_a but I don't have actual control of it.

CREATE PROCEDURE zzzzz_testtodelete
AS
BEGIN
    CREATE TABLE #a(idtmp bit)
    Exec sp_a
        @a = 1
    .....
    DROP TABLE #a

    CREATE TABLE #a(idtmp bit)
    Exec sp_a
        @a = 2
    .....
    DROP TABLE #a
END

Is this normal or is it a bug from sql?


回答1:


This is normal. What happens here is a compile error, not a run-time error. This is because when parsing the batch, SQL Server sees that you already have created #a, so creating it again will produce an error. Again, this error is not a run-time error but compile error. To test:

PRINT 'TESTING'

CREATE TABLE #a(idtmp bit)
DROP TABLE #a

CREATE TABLE #a(idtmp bit)
DROP TABLE #a

Note that TESTING is not printed, since the batch is not even executed due to the compile error.

Putting a GO in between the first DROP and second CREATE will fix the problem:

PRINT 'TESTING'

CREATE TABLE #a(idtmp bit)
DROP TABLE #a

GO

CREATE TABLE #a(idtmp bit)
DROP TABLE #a

This is because you have two separate batches now. SQL Server parses and validates sql queries by batch.


Only after the OP's edit that he/she states that this is used in stored procedure. In that case, there is no real solution. Still, this is a compile error. The code is not executed.

Additionally, according to BOL:

If more than one temporary table is created inside a single stored procedure or batch, they must have different names.

The workaround is to use different names for the tables. Or just truncate the table after the first use.




回答2:


try this way

IF Object_id('tempdb..#temp') is not null                
    Drop table #temp  
create table #temp (table columns as desired)



回答3:


Temp tables are less efficient in stored procedures, try using table types, you don't need to drop them.

DECLARE @a TABLE (idtmp bit)


来源:https://stackoverflow.com/questions/32112865/creating-and-deleting-temp-tables-consecutively-in-a-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!