sql-to determine if a table exists before creating a table

Source: Internet
Author: User

1. SQL script
/*sql-determine if a table exists before creating a table LDH @ 2018-5-16*/ UseTestDBGO--Method 1IF object_id(N'Books','U') is  not NULLBEGIN    DROP TABLEBooks;END;--Method 2/*IF EXISTS (SELECT * from sys.objects WHERE name = ' Books ') BEGIN DROP TABLE Books; END;*/--Method 3/*IF EXISTS (SELECT COUNT (1) from sys.tables WHERE name = ' Books ') BEGIN DROP TABLE Books; END;*/--Create a new table.CREATE TABLEBooks (BookIdINT  not NULL, BookNameCHAR( -) not NULL, Price Money );--Insert data into the table.INSERT  intoBooks (Bookid,bookname,price)VALUES('1001'N'If life is just like the beginning',' the');INSERT  intoBooks (Bookid,bookname,price)VALUES('1002'N'SQL Server',' the');INSERT  intoBooks (Bookid,bookname,price)VALUES('1003'N'SQL must know',' -');INSERT  intoBooks (Bookid,bookname,price)VALUES('1004'N'The past of Lek Chuan',' the');INSERT  intoBooks (Bookid,bookname,price)VALUES('1005'N'Red Mansions',' -');INSERT  intoBooks (Bookid,bookname,price)VALUES('1006'N'Kingdoms',' -');INSERT  intoBooks (Bookid,bookname,price)VALUES('1007'N'Journey to the',' -');INSERT  intoBooks (Bookid,bookname,price)VALUES('1008'N'Outlaws',' -');--Select The table created just now.SELECTBookId, BookName, price fromDbo. Books
2. Running results

sql-to determine if a table exists before creating a table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.