Home > Database > Mysql Tutorial > sql 判断数据库,表,存储过程等是否存在的代码

sql 判断数据库,表,存储过程等是否存在的代码

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 17:59:35
Original
1440 people have browsed it

sql下用了判断各种资源是否存在的代码,很实用。需要的朋友可以参考下。

sql判断是否存在
--判断数据库是否存在
if exists(select * from master..sysdatabases where name=N'库名')
print 'exists'
else
print 'not exists'

---------------
-- 判断要创建的表名是否存在
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[表名]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
-- 删除表
drop table [dbo].[表名]
GO

---------------
--判断要创建临时表是否存在
If Object_Id('Tempdb.dbo.#Test') Is Not Null
Begin
print '存在'
End
Else
Begin
print '不存在'
End

---------------
-- 判断要创建的存储过程名是否存在
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[存储过程名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
-- 删除存储过程
drop procedure [dbo].[存储过程名]
GO

---------------
-- 判断要创建的视图名是否存在
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[视图名]') and OBJECTPROPERTY(id, N'IsView') = 1)
-- 删除视图
drop view [dbo].[视图名]
GO

---------------
-- 判断要创建的函数名是否存在
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[函数名]') and xtype in (N'FN', N'IF', N'TF'))
-- 删除函数
drop function [dbo].[函数名]
GO

if col_length('表名', '列名') is null
print '不存在'

select 1 from sysobjects where id in (select id from syscolumns where name='列名') and name='表名'
Related labels:
sql
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
sql file
From 1970-01-01 08:00:00
0
0
0
php - Overhead of prepare vs sql?
From 1970-01-01 08:00:00
0
0
0
Print sql statement
From 1970-01-01 08:00:00
0
0
0
Pass array to SQL insert query using PHP
From 1970-01-01 08:00:00
0
0
0
sql optimization or
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template