SQL to determine if "library, table, column, view, stored procedure" exists

Source: Internet
Author: User

-- determine if the database exists

IF EXISTS (SELECT * from MASTER.sys.sysdatabases WHERE name = ' library name ')

PRINT ' exists '

Else

PRINT ' NOT EXISTS '

-- determine if the name of the table to be created exists

IF EXISTS (Select * from sysObjects Where name = ' table name ' and Type in (' S ', ' U '))

PRINT ' exists '

ELSE

PRINT ' NOT EXISTS '

GO

-- determine if you want to create a temporary table exists

If object_id (' tempdb.dbo. #Test ') is a temporary table name #Test not null--

Begin

print ' exists '

End

Else

Begin

print ' does not exist '

End

-- determine if the stored procedure name to be created exists

IF EXISTS (Select * from sysObjects Where name = ' stored procedure name ' and Type in (' S ', ' P '))

PRINT ' exists '

ELSE

PRINT ' NOT EXISTS '

GO

-- Determine if the column name exists

IF EXISTS (SELECT o.name as table name , c.name as column name from SYSOBJECTS O INNER JOIN syscolumns C on O.id=c . ID WHERE o.name= ' table name ' and c.name = ' column name ')

SELECT ' EXISTS '

ELSE

SELECT ' Not EXISTS '

--whether the table A column C exists

IF not EXISTS (SELECT 1 from SYSOBJECTS T1 INNER joins syscolumns T2 on t1.id=t2.id WHERE t1.name= ' A ' and t2.name= ' C ')

-- a function to judge the existence of a table name

IF col_length (' table name ', ' column name ' ) is NULL

PRINT ' NOT EXISTS '

ELSE

PRINT ' exists '

--New Modified view sp use first DROP,&NBSP, create .  Span style= "font-family: the song Body;" If you have * view using Span style= "Font-family:times New Roman;" >sp_refreshview refresh exec

if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Rpt_2033300001_v] ') and OBJECTPROPERTY (ID, N ' isview ') = 1)

Drop view [dbo]. [Rpt_2033300001_v]

GO

CREATE VIEW Dbo.rpt_2033300001_v

As

*********

if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Stored Procedure name ] ') and OBJECTPROPERTY (ID, N ' isprocedure ') = 1)

-- Delete stored procedures

drop procedure [dbo]. [ stored procedure name ]

GO

SQL to determine if "library, table, column, view, stored procedure" 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.