SQL Common Judgment statement

Source: Internet
Author: User
Tags create index

When we do the SQL update, in order to prevent the SQL to repeat the error, we need to judge the existence of the object to be executed;

Common judgment scripts are as follows:

To determine whether a view exists

IF object_id ('viewname'is notNULLbegin-- operation  --drop view viewnameEnd
determine if a table exists
IF object_id ('tablename'isNULLBEGIN-- operation END 
determine if a column exists
IF  not EXISTS(SELECT 1  fromDbo.syscolumnsWHERE [name]='ColumnName'  andId=object_id('TableName'))begin --OperationEnd
determine if a function exists 
IF exists (Select1from where xtype='fn'and name='funcname')BEGIN-- drop function FuncName End
  determine if a stored procedure exists
IF exists (Select1from where xtype='P'and name='procname')BEGIN-- drop proc ProcName End
  determine if a trigger is present
IF exists(Select *  fromsysobjectswhereId=object_id(N'tr_es_order_upd') and ObjectProperty(Id,n'Istrigger')=1) begin--DROP TRIGGER tr_es_order_upd;End
To determine if an index exists CREATE index
IF  not EXISTS (Select1fromwhere name='index_cb_warehouseinoutdtl_ Materialsguid')begin-- operation END

SQL Common Judgment statement

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.