How can I delete the table that I declared in my SP

as i declared the table ...

declare @tempTable TABLE (Id bigint identity(1,1), bKey int, DateT DateTime, Addres nvarchar(max), unit bigint) 

and I want to abandon it ... but I am stuck in coz drop table n truncate not working.

ani ideaaa ... ???

+4
source share
6 answers

Variable tables only live if they are in scope, in your case, during the stored procedure. He will remove himself after completion of the procedure.

Perhaps you are thinking of a temporary table:

 CREATE TABLE #MyTempTable 

There are differences between temporary tables and table variables:

http://sqlnerd.blogspot.com/2005/09/temp-tables-vs-table-variables.html

The best link:

http://www.sql-server-performance.com/articles/per/temp_tables_vs_variables_p1.aspx

+7
source

You do not need to drop table variables (i.e. those starting with @)

+6
source

why do you want to delete a table variable? the scope of the table variable is specified only in the stored procedure ...

check this article ... Table Variables in T-SQL

UPDATE

Use #temptable as suggested by @ Adam ... Then TRUNCATE TABLE #temptable instead of DROP TABLE; thus, there is no need to recreate it in a loop.

+2
source

You have declared the table as a table variable. It will exist only for the duration of the stored procedure. When the procedure ends, it is automatically โ€œdiscardedโ€ (it ceases to exist more precisely, since it never existed in the way other tables do it). You cannot delete them manually.

+2
source

A temporary table has been created and stored in a variable. It exists only as long as the repository procedure is running. When the SP completes, the temporary table ceases to exist and is automatically deleted.

edit:

you can try testing it by running the stored procedure and then try running the select query on @tempTable

+2
source

In your SP, this will remove all values โ€‹โ€‹from the declared table

 DELETE FROM @tempTable 
0
source

Source: https://habr.com/ru/post/1315652/


All Articles