Common SQL judgment and detection statements

Source: Internet
Author: User
SQL
-- ----------------------------------------------------------
-- Determine whether the database exists
If   Exists (
Select   *
From Master.. sysdatabases
Where Name = N ' Dbname '
)
Print ' Exists '
Else  
Print ' Not exists '
-- ----------------------------------------------------------
-- Determine whether the table name to be created exists
If   Exists (
Select   *
From DBO. sysobjects
Where ID =   Object_id (N ' [DBO]. [tablename] ' )
And Objectproperty (ID, n ' Isusertable ' ) =   1
)
Print ' Exists '
Else  
Print ' Not exists '
-- ----------------------------------------------------------
-- Determine whether the temporary table to be created exists
If   Object_id ( ' Tempdb. DBO. # tempdbname ' ) Is Not Null
Print ' Exists '
Else  
Print ' Not exists '
-- ----------------------------------------------------------
-- Determine whether the stored procedure name to be created exists
If   Exists (
Select   *
From DBO. sysobjects
Where ID =   Object_id (N ' [DBO]. [spname] ' )
And Objectproperty (ID, n ' Isprocedure ' ) =   1
)
Print ' Exists '
Else  
Print ' Not exists '
-- ----------------------------------------------------------
-- Determine whether the view name to be created exists
If   Exists (
Select   *
From DBO. sysobjects
Where ID =   Object_id (N ' [DBO]. [viewname] ' )
And Objectproperty (ID, n ' Isview ' ) =   1
)
Print ' Exists '
Else  
Print ' Not exists '
-- ----------------------------------------------------------
-- Determine whether the name of the function to be created exists
If   Exists (
Select   *
From DBO. sysobjects
Where ID =   Object_id (N ' [DBO]. [funname] ' )
And Xtype In (N ' FN ' , N ' If ' , N ' TF ' )
)
Print ' Exists '
Else  
Print ' Not exists '
-- ----------------------------------------------------------
-- Determine whether the column name to be created exists
If   Exists (
Select O. Name As Tablename,
C. Name As Columnname
From Sysobjects o Inner
Join Syscolumns C
On O. ID = C. ID
Where O. Name =   ' Tablename '
And C. Name =   ' Columnname '
)
Print ' Exists '
Else  
Print ' Not exists '
-- ----------------------------------------------------------
-- Or
If   Exists (
Select   1
From Sysobjects
Where ID In ( Select ID
From Syscolumns
Where Name =   ' Columnname ' )
And Name =   ' Tablename '
)
Print ' Exists '
Else  
Print ' Not exists '
-- ----------------------------------------------------------
-- Or
If   Col_length ( ' Tablename ' , ' Columnname ' ) Is Null
Print ' Not exists '
Else  
Print ' Exists '  

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.