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 ');