sql server if exists用法

if exists Usage

 
 
SUMMARY judgment table if exists whether there
 
 
IF EXISTS(SELECT 1 FROM proprice_sheet WHERE vndcode = @vndcode AND matcode = @matcode)  


if exists(select 1 fromwhere 列=@ 参数)

 

 
 
 
Other Uses
 
Copy the code
Determining whether a database exists 
if exists (select * from sys.databases where name = ' database name')   
  drop Database [database name] 
2 determines whether there is a table if exists (select * from sysobjects where id = object_id (N '[ Table Name ] ') and the OBJECTPROPERTY (ID, N'IsUserTable') =. 1) drop table [table]
3 determines whether there is a stored procedure if exists (select * from sysobjects where id = object_id (N '[ stored procedure name]') and the OBJECTPROPERTY (ID, N'IsProcedure ') =. 1) drop procedure [stored procedure name]
4 determines whether there is a temporary table if object_id (' tempdb .. # temporary table name ') IS Not null drop the temporary table table # 5 Analyzing view whether there is - SQL Server 2000 iF eXISTS (the SELECT * the FROM the sysviews the WHERE object_id = '[dbo]. [view name]' - SQL Server 2005 the iF eXISTS (the FROM sys.views the SELECT * the WHERE object_id = '[the dbo] [view name]' - -SQL Server 2000 IF EXISTS (SELECT * FROM sysviews WHERE object_id = '[dbo]. [ View name]' - SQL Server 2005 the IF EXISTS (the FROM sys.views the SELECT * the WHERE object_id = '[the dbo]. [View name]'

6 judging function whether there is - the function name to be created is determined whether or not there '. [dbo] [function name]' if exists (select * from dbo.sysobjects where id = object_id (N) and xtype in (N'FN ', N'IF ', N'TF')) drop function [the dbo] [function name] - function name to be created is determined whether there is
an object created by the user information acquired 7 SELECT [name], [id] , crdate FROM sysobjects where xtype = 'the U-' / * xtype parameter indicating the type, typically comprising the C = CHECK constraint D = default or dEFAULT constraint F = FOREIGN KEY constraint L = log PK = PRIMARY KEY constraints ( type K) the RF = replication filter stored procedure FN = scalar function IF = inline table function P = stored procedure 11 to view objects in a database S = system table TF = Table function TR = Trigger U = user table UQ = UNIQUE constraint (type K) V = view X = Extended stored procedure
* /
8 determines whether there is a row if exists (select * from syscolumns where id = object_id ( 'table') and name = 'column name') ALTER table drop column column name table

9 determines whether auto-increment column if columnproperty (object_id ( 'table' ), 'col', 'IsIdentity') = 1 print 'auto-increment " the else print' auto-increment is not ' whether there is an index table 10 determines if exists (select * from sysindexes where id = object_id (' table ') and name =' index name ') print' presence ' the else Print 'does not exist

object name SELECT * FROM sys.sysobjects WHERE name =' '
Copy the code
Copy the code
Determining whether a database exists 
if exists (select * from sys.databases where name = ' database name')   
  drop Database [database name] 
2 determines whether there is a table if exists (select * from sysobjects where id = object_id (N '[ Table Name ] ') and the OBJECTPROPERTY (ID, N'IsUserTable') =. 1) drop table [table]
3 determines whether there is a stored procedure if exists (select * from sysobjects where id = object_id (N '[ stored procedure name]') and the OBJECTPROPERTY (ID, N'IsProcedure ') =. 1) drop procedure [stored procedure name]
4 determines whether there is a temporary table if object_id (' tempdb .. # temporary table name ') IS Not null drop the temporary table table # 5 Analyzing view whether there is - SQL Server 2000 iF eXISTS (the SELECT * the FROM the sysviews the WHERE object_id = '[dbo]. [view name]' - SQL Server 2005 IF EXISTS (SELECT * FROM sys.views WHERE object_id = '[dbo]. [ View name]' --SQL Server 2000 the IF EXISTS (the FROM the sysviews the SELECT * the WHERE object_id = '[the dbo]. [View name]' - SQL 2005 Server the iF eXISTS (the fROM sys.views the SELECT * object_id = the WHERE '. [the dbo] [view name]'

6 determines whether there is the function - the name of the function determines whether there is to be created if exists (select * from dbo.sysobjects where id = object_id (N '. [dbo ] [ function name]') and in xtype (N'FN ', N'IF', N'TF ')) drop function [the dbo] [function name] - Analyzing to create function name exists
object information 7 acquires the user created the SELECT [name], [ID], the crdate the FROM the sysobjects WHERE xtype = 'the U-' / * xtype parameter indicating the type, typically comprising the C = CHECK constraint D = default or DEFAULT constraint F = FOREIGN KEY constraint L = log FN = scalar function IF = inline table function P = stored procedure PK = PRIMARY KEY constraints (type K) the RF filter stored procedure replication = S = system table TF = Table function TR = Trigger U = user table UQ = UNIQUE constraint (type K ) V = view X = extended stored procedure
* /
8 determines columns exist if exists (select * from syscolumns where id = object_id ( ' table') and name = 'column name') ALTER table table drop column column name

9 determining whether the column from additional iF the COLUMNPROPERTY (object_id ( 'table'), 'COL', 'IsIdentity') =. 1 Print "auto-increment" the else Print 'auto-increment is not' whether there is an index table 10 determines if exists ( select * from sysindexes where id = object_id ( ' table') and name = 'index name') Print 'presence' the else Print 'does not exist

11 View objects in the database SELECT * FROM sys.sysobjects WHERE name = 'object name'
Copy the code

Guess you like

Origin www.cnblogs.com/baili-luoyun/p/11138281.html