sql server判斷數(shù)據(jù)庫、表、列、視圖是否存在
1 判斷數(shù)據(jù)庫是否存在
if exists (select * from sys.databases where name = '數(shù)據(jù)庫名')
drop database [數(shù)據(jù)庫名]
2 判斷表是否存在
if exists (select * from sysobjects where id = object_id(N'[表名]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [表名]
3 判斷存儲(chǔ)過程是否存在
if exists (select * from sysobjects where id = object_id(N'[存儲(chǔ)過程名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [存儲(chǔ)過程名]
4 判斷臨時(shí)表是否存在
if object_id('tempdb..#臨時(shí)表名') is not null
drop table #臨時(shí)表名
5 判斷視圖是否存在
--判斷是否存在'MyView52'這個(gè)試圖
IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = N'MyView52')
PRINT '存在'
else
PRINT '不存在'
6 判斷函數(shù)是否存在
-- 判斷要?jiǎng)?chuàng)建的函數(shù)名是否存在
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[函數(shù)名]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[函數(shù)名]
7 獲取用戶創(chuàng)建的對象信息
SELECT [name],[id],crdate FROM sysobjects where xtype='U'
8 判斷列是否存在
if exists(select * from syscolumns where id=object_id('表名') and name='列名')
alter table 表名 drop column 列名
9 判斷列是否自增列
if columnproperty(object_id('table'),'col','IsIdentity')=1
print '自增列'
else
print '不是自增列'
SELECT * FROM sys.columns WHERE object_id=OBJECT_ID('表名') AND is_identity=1
10 判斷表中是否存在索引
if exists(select * from sysindexes where id=object_id('表名') and name='索引名')
print '存在'
else
print '不存在'
刪除索引 drop index 表名.索引名
或: drop index 索引名 on 表名(貌似2000不行)
11 查看數(shù)據(jù)庫中對象
SELECT * FROM sys.sysobjects WHERE name='對象名' SELECT * FROM sys.sysobjects WHERE name='對象名'
相關(guān)文章
SQL?Server創(chuàng)建用戶定義函數(shù)
這篇文章介紹了SQL?Server創(chuàng)建用戶定義函數(shù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
SQL Server 2000“設(shè)備激活錯(cuò)誤”的解決方法
數(shù)據(jù)庫恢復(fù)時(shí)出現(xiàn)諸如“設(shè)備激活錯(cuò)誤,請使用with move選項(xiàng)來標(biāo)志該文件的有效位置”報(bào)錯(cuò)的解決方法2013-11-11
SQLserver中字符串查找功能patindex和charindex的區(qū)別
CHARINDEX 和 PATINDEX 函數(shù)都返回指定模式的開始位置,PATINDEX 可使用通配符,而 CHARINDEX 不可以2012-05-05
如何在SQLSERVER中快速有條件刪除海量數(shù)據(jù)
如何在SQLSERVER中快速有條件刪除海量數(shù)據(jù)...2006-09-09
使用SQL Server數(shù)據(jù)庫嵌套子查詢的方法
使用SQL Server數(shù)據(jù)庫嵌套子查詢的方法...2007-03-03
SQL?Server?DATEDIFF()?函數(shù)用法
這篇文章主要介紹了SQL?Server?DATEDIFF()?函數(shù)的定義和用法,通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
sql server2012附加數(shù)據(jù)庫問題解決方法
這篇文章主要介紹了sql server2012附加數(shù)據(jù)庫問題解決方法,需要的朋友可以參考下2014-05-05

