Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i drop a table which i have declared in my SP

Tags:

sql

like i declared a table...

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

and i want to drop it...but im stucked coz drop table n truncate are not working.

ani ideaaa...???

like image 765
Tami Avatar asked Jul 14 '10 12:07

Tami


People also ask

Can we DROP TABLE from stored procedure?

You can use these scripts to drop database, table, or other elements In the beginning of your stored procedure, and then create them freely during the SP. While recreating the table you have full control over the data types of the table columns (fields is a wrong word by the way in this context).

Can we DROP a table which is in reference with another table?

Multiple tables can be dropped in any database. If a table being dropped references the primary key of another table that is also being dropped, the referencing table with the foreign key must be listed before the table holding the primary key that is being referenced.

How do I DROP an entire table?

Syntax: DROP object object_name Examples: DROP TABLE table_name; table_name: Name of the table to be deleted. DROP DATABASE database_name; database_name: Name of the database to be deleted.

How do I DROP a #tmp table in SQL?

Using the DROP TABLE command on a temporary table, as with any table, will delete the table and remove all data. In an SQL server, when you create a temporary table, you need to use the # in front of the name of the table when dropping it, as this indicates the temporary table.


3 Answers

Table variables only live while they are in scope, in your case for the duration of the stored procedure. It will delete itself once the procedure completes.

You might be 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

A better link:

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

like image 149
Adam Houldsworth Avatar answered Oct 13 '22 12:10

Adam Houldsworth


You don't need to drop table variables (i.e. those that start with @)

like image 39
Mitch Wheat Avatar answered Oct 13 '22 10:10

Mitch Wheat


You've declared the table as a table variable. It will only exist for the duration of the stored procedure. When the procedure ends, it is automatically "dropped" (ceases to exist is more accurate, as it never really existed in the way other tables do). You cannot drop them manually.

like image 28
Ant Avatar answered Oct 13 '22 11:10

Ant