If table exists, rename and create new one : Drop Table « Table « SQL Server / T-SQL






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>
           
       








Related examples in the same category

1.A statement that qualifies the table to be deleted
2.Drop a table if necessary