SQL Server determine whether the relationship of the various objects exist and sysobjects

A, it is determined whether there is a table

object_id ():. acquires the ID table, where N represents the name of the object may be supported in Unicode of different languages

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[表名]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[表名]

Second, the name of the stored procedure to determine whether there is to be created

IF  EXISTS ( SELECT  *  from dbo.sysobjects WHERE ID =  object_id (N ' . [the dbo] [stored procedure name] ' ) and  the OBJECTPROPERTY (ID, N ' IsProcedure ' ) =  . 1 )
 - delete the stored procedure 
drop  Procedure  [ the dbo ] . [ stored procedure name ]

Third, the view name to determine whether there is to be created

IF  EXISTS ( SELECT  *  from dbo.sysobjects WHERE ID =  object_id (N ' . [the dbo] [View name] ' ) and  the OBJECTPROPERTY (ID, N ' IsView ' ) =  . 1 )
 - Delete View 
drop  View  [ the dbo ] . [ view name ]

Fourth, the name of the function to determine whether there is to be created

IF  EXISTS ( SELECT  *  from dbo.sysobjects WHERE ID =  object_id (N ' . [the dbo] [function name] ' ) and xtype in (N ' FN ' , N ' the IF ' , N ' TF ' ))
 - Delete Function 
drop  function  [ dbo ] . [ function name ]

Five, it is determined whether there is listed

if col_length('表名', '列名') is not null
select * from sysobjects where id in (select id from syscolumns where name='列名') and name='表名'

六、dbo.sysobjects

  sysobjects within each database sql server has this system tables, which store all objects created within the database

  xtype object type:
    C: the CHECK constraints, D: DEFAULT constraints, F: FOREIGN KEY constraint, L: log, FN: scalar function, IF: inline table function, P: Stored Procedures, RF: Replication filter stored procedure, TR: flip-flop, U: user table, X: extended stored procedure, S: system tables, TF: table function, UQ: UNIQUE constraint (type K), V: view, PK: PRIMARY KEY constraints (type K)

Guess you like

Origin www.cnblogs.com/haosit/p/10733954.html