SQL-创建表之前判断表是否存在
Posted Love In Winter
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL-创建表之前判断表是否存在相关的知识,希望对你有一定的参考价值。
1.SQL脚本
/* SQL-创建表之前判断表是否存在 LDH @ 2018-5-16 */ USE TestDB GO -- 方法 1 IF OBJECT_ID(N\'Books\', \'U\') IS NOT NULL BEGIN DROP TABLE Books; END; -- 方法 2 /* IF EXISTS (SELECT * FROM sys.objects WHERE name = \'Books\') BEGIN DROP TABLE Books; END; */ -- 方法 3 /* IF EXISTS (SELECT COUNT(1) FROM sys.tables WHERE name = \'Books\') BEGIN DROP TABLE Books; END; */ -- Create a new table. CREATE TABLE Books ( BookId INT NOT NULL, BookName CHAR(50) NOT NULL, Price MONEY ); -- Insert data into the table. INSERT INTO Books(BookId,BookName,Price)VALUES(\'1001\',N\'人生若只如初见\',\'88\'); INSERT INTO Books(BookId,BookName,Price)VALUES(\'1002\',N\'SQl Server 2017\',\'88\'); INSERT INTO Books(BookId,BookName,Price)VALUES(\'1003\',N\'SQL必知必会\',\'50\'); INSERT INTO Books(BookId,BookName,Price)VALUES(\'1004\',N\'沥川往事\',\'80\'); INSERT INTO Books(BookId,BookName,Price)VALUES(\'1005\',N\'红楼梦\',\'100\'); INSERT INTO Books(BookId,BookName,Price)VALUES(\'1006\',N\'三国演义\',\'100\'); INSERT INTO Books(BookId,BookName,Price)VALUES(\'1007\',N\'西游记\',\'120\'); INSERT INTO Books(BookId,BookName,Price)VALUES(\'1008\',N\'水浒传\',\'100\'); -- Select the table you created just now. SELECT BookId , BookName , Price FROM dbo.Books
2.运行结果
以上是关于SQL-创建表之前判断表是否存在的主要内容,如果未能解决你的问题,请参考以下文章