SQL Server/T-SQL/Table/Drop Table

Материал из SQL эксперт
Перейти к: навигация, поиск

A statement that qualifies the table to be deleted

 
DROP TABLE New_AP.dbo.Bankers1



Drop a table if necessary

1>
2> CREATE TABLE department(dept_no   CHAR(4) NOT NULL,
3>                         dept_name CHAR(25) NOT NULL,
4>                         location  CHAR(30) NULL)
5> GO
1>
2> insert into department values ("d1", "developer",   "Dallas")
3> insert into department values ("d2", "tester",      "Seattle")
4> insert into department values ("d3", "marketing",  "Dallas")
5> GO
(1 rows affected)
(1 rows affected)
(1 rows affected)
1>
2> select * from department
3> GO
dept_no dept_name                 location
------- ------------------------- ------------------------------
d1      developer                 Dallas
d2      tester                    Seattle
d3      marketing                 Dallas
(3 rows affected)
1>
2> drop table department
3> GO
1>



If table exists, rename and create new one

12>
13> IF EXISTS(SELECT name FROM sys.tables
14>     WHERE name = "T")
15>     BEGIN
16>         PRINT "T already."
17>         DROP TABLE T_old
18>         EXEC sp_rename "T", "T_old"
19>     END
20> ELSE PRINT "No T already."
21>
22> CREATE TABLE T (
23>     c1 bigint,
24>     c2 nvarchar(max)
25> )
26>
27> drop table t
28> GO
No T already.
1>