The wording of the if not exists in MSSQL and SQLite

Source: Internet
Author: User
Tags mssql sqlite

In the SQL name, if not exists is if not present, if exists is if present.

Below you learn the use of both.

A to determine if the database does not exist

Copy the code code as follows:
If not EXISTS (SELECT * from sys.databases WHERE name = ' database_name ')

b, when the judgment table does not exist

Copy the code code as follows:
If not EXISTS (SELECT * from sysobjects where id = object_id (' table_name ') and OBJECTPROPERTY (ID, ' isusertable ') = 1)

C, the Judgment column does not exist

Copy the code code as follows:
If not EXISTS (SELECT * from syscolumns where id=object_id (' table_name ') and Name= ' column_name ')

When the table is not saved, I can do the creation of the database, create the table, add columns, you can execute the corresponding SQL statements;

And if exists the same judgment, first of all to determine whether the query results exist, if there is execution judgment after the statement, query database, table, column method is the same;

MSSQL Syntax:

Copy the code code as follows:
If not exists (SELECT 1 from [t_table] where [fName] = ' Zhang San ')
INSERT into t_table values (' Zhang San ');

SQLite syntax:

Copy the code code as follows:
Insert INTO t_table select ' Zhang San '
Where NOT EXISTS (SELECT 1 from [t_table] where [fName] = ' Zhang San ');

The wording of the if not exists in MSSQL and SQLite

Related Article

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.