Description of if not exists in mssql and sqlite

Source: Internet
Author: User

In the SQL language name, if not exists indicates that it does not exist, and if exists indicates that it exists.

Next we will learn how to use the two.

A. When the database does not exist

Copy codeThe Code is as follows:
If not exists (select * from sys. databases where name = 'database _ name ')

B. When the table does not exist

Copy codeThe Code is as follows:
If not exists (select * from sysobjects where id = object_id ('table _ name') and OBJECTPROPERTY (id, 'isusertable') = 1)

C. Determine if the column does not exist.

Copy codeThe Code is as follows:
If not exists (select * from syscolumns where id = object_id ('table _ name') and name = 'column _ name ')

When the identified table does not exist, I can create a database, create a table, add columns, and execute corresponding SQL statements;

If exists determines the existence of the query result in the same way. if the query result exists, execute the following statements, query databases, tables, and columns in the same way;

Mssql Syntax:

Copy codeThe Code is as follows:
If not exists (SELECT 1 FROM [t_Table] where [fName] = 'zhang san ')
Insert into [t_Table] ([fName]) values ('zhang san ');

Sqlite Syntax:

Copy codeThe Code is as follows:
Insert into [t_Table] ([fName]) select 'zhang san'
Where not exists (SELECT 1 FROM [t_Table] where [fName] = 'zhang san ');

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.