sql 判斷數(shù)據(jù)庫,表,存儲過程等是否存在的代碼
更新時間:2009年12月13日 01:03:19 投稿:mdxy-dxy
sql下用了判斷各種資源是否存在的代碼,很實用。需要的朋友可以參考下。
代碼:
--庫是否存在 if exists(select * from master..sysdatabases where name=N'庫名') print 'exists' else print 'not exists' --------------- -- 判斷要創(chuàng)建的表名是否存在 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[表名]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) -- 刪除表 drop table [dbo].[表名] GO --------------- -----列是否存在 IF COL_LENGTH( '表名','列名') IS NULL PRINT 'not exists' ELSE PRINT 'exists' alter table 表名 drop constraint 默認值名稱 go alter table 表名 drop column 列名 go ----- --判斷要創(chuàng)建臨時表是否存在 If Object_Id('Tempdb.dbo.#Test') Is Not Null Begin print '存在' End Else Begin print '不存在' End --------------- -- 判斷要創(chuàng)建的存儲過程名是否存在 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[存儲過程名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) -- 刪除存儲過程 drop procedure [dbo].[存儲過程名] GO --------------- -- 判斷要創(chuàng)建的視圖名是否存在 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[視圖名]') and OBJECTPROPERTY(id, N'IsView') = 1) -- 刪除視圖 drop view [dbo].[視圖名] GO --------------- -- 判斷要創(chuàng)建的函數(shù)名是否存在 if exists (select * from sysobjects where xtype='fn' and name='函數(shù)名') if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[函數(shù)名]') and xtype in (N'FN', N'IF', N'TF')) -- 刪除函數(shù) drop function [dbo].[函數(shù)名] GO if col_length('表名', '列名') is null print '不存在' select 1 from sysobjects where id in (select id from syscolumns where name='列名') and name='表名'
sql判斷是否存在
--判斷數(shù)據(jù)庫是否存在 if exists(select * from master..sysdatabases where name=N'庫名') print 'exists' else print 'not exists' --------------- -- 判斷要創(chuàng)建的表名是否存在 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[表名]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) -- 刪除表 drop table [dbo].[表名] GO --------------- --判斷要創(chuàng)建臨時表是否存在 If Object_Id('Tempdb.dbo.#Test') Is Not Null Begin print '存在' End Else Begin print '不存在' End --------------- -- 判斷要創(chuàng)建的存儲過程名是否存在 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[存儲過程名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) -- 刪除存儲過程 drop procedure [dbo].[存儲過程名] GO --------------- -- 判斷要創(chuàng)建的視圖名是否存在 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[視圖名]') and OBJECTPROPERTY(id, N'IsView') = 1) -- 刪除視圖 drop view [dbo].[視圖名] GO --------------- -- 判斷要創(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')) -- 刪除函數(shù) drop function [dbo].[函數(shù)名] GO if col_length('表名', '列名') is null print '不存在' select 1 from sysobjects where id in (select id from syscolumns where name='列名') and name='表名'
相關(guān)文章
SqlServer數(shù)據(jù)庫全角轉(zhuǎn)換成半角
SqlServer數(shù)據(jù)庫全角轉(zhuǎn)換成半角,需要的朋友可以參考一下2013-03-03SQLServer導出數(shù)據(jù)到MySQL實例介紹
本文為大家詳細介紹下從SQLServer導出數(shù)據(jù)并將數(shù)據(jù)導入到MySQL,具體的實現(xiàn)如下,感興趣的朋友可以參考下哈2013-07-07SQL Server 性能調(diào)優(yōu)之查詢從20秒至2秒的處理方法
這篇文章主要介紹了SQL Server 性能調(diào)優(yōu)之查詢從20秒至2秒的處理方法,需要的朋友可以參考下2017-07-07SQL Server存儲過程中編寫事務處理的方法小結(jié)
這篇文章主要介紹了SQL Server存儲過程中編寫事務處理的方法,結(jié)合實例形式總結(jié)分析了三種存儲過程中編寫事務處理的方法,具有一定參考借鑒價值,需要的朋友可以參考下2016-03-03用SQL語句實現(xiàn)隨機查詢數(shù)據(jù)并不顯示錯誤數(shù)據(jù)的方法
用SQL語句實現(xiàn)隨機查詢數(shù)據(jù)并不顯示錯誤數(shù)據(jù)的方法...2007-11-11SqlServer備份數(shù)據(jù)庫的4種方式介紹
這篇文章主要介紹了SqlServer備份數(shù)據(jù)庫的4種方式介紹,本文講解了用sqlserver的維護計劃、通過腳本+作業(yè)的方式備份數(shù)據(jù)庫(非xp_cmdshell和xp_cmdshell)、用powershell調(diào)用sqlcmd來執(zhí)行備份命令幾種方式,需要的朋友可以參考下2015-02-02