sql 判斷數(shù)據(jù)庫,表,存儲(chǔ)過程等是否存在的代碼
代碼:
--庫是否存在 if exists(select * from master..sysdatabases where name=N'庫名') print 'exists' else print 'not exists' --------------- -- 判斷要?jiǎng)?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 默認(rèn)值名稱 go alter table 表名 drop column 列名 go ----- --判斷要?jiǎng)?chuàng)建臨時(shí)表是否存在 If Object_Id('Tempdb.dbo.#Test') Is Not Null Begin print '存在' End Else Begin print '不存在' End --------------- -- 判斷要?jiǎng)?chuàng)建的存儲(chǔ)過程名是否存在 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[存儲(chǔ)過程名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) -- 刪除存儲(chǔ)過程 drop procedure [dbo].[存儲(chǔ)過程名] GO --------------- -- 判斷要?jiǎng)?chuàng)建的視圖名是否存在 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[視圖名]') and OBJECTPROPERTY(id, N'IsView') = 1) -- 刪除視圖 drop view [dbo].[視圖名] GO --------------- -- 判斷要?jiǎng)?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' --------------- -- 判斷要?jiǎng)?chuàng)建的表名是否存在 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[表名]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) -- 刪除表 drop table [dbo].[表名] GO --------------- --判斷要?jiǎng)?chuàng)建臨時(shí)表是否存在 If Object_Id('Tempdb.dbo.#Test') Is Not Null Begin print '存在' End Else Begin print '不存在' End --------------- -- 判斷要?jiǎng)?chuàng)建的存儲(chǔ)過程名是否存在 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[存儲(chǔ)過程名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) -- 刪除存儲(chǔ)過程 drop procedure [dbo].[存儲(chǔ)過程名] GO --------------- -- 判斷要?jiǎng)?chuàng)建的視圖名是否存在 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[視圖名]') and OBJECTPROPERTY(id, N'IsView') = 1) -- 刪除視圖 drop view [dbo].[視圖名] GO --------------- -- 判斷要?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')) -- 刪除函數(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事務(wù)備份和還原的實(shí)例代碼(必看)
下面小編就為大家?guī)硪黄猄qlserver事務(wù)備份和還原的實(shí)例代碼(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05SqlServer數(shù)據(jù)庫全角轉(zhuǎn)換成半角
SqlServer數(shù)據(jù)庫全角轉(zhuǎn)換成半角,需要的朋友可以參考一下2013-03-03SQLServer導(dǎo)出數(shù)據(jù)到MySQL實(shí)例介紹
本文為大家詳細(xì)介紹下從SQLServer導(dǎo)出數(shù)據(jù)并將數(shù)據(jù)導(dǎo)入到MySQL,具體的實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-07-07SQL Server 性能調(diào)優(yōu)之查詢從20秒至2秒的處理方法
這篇文章主要介紹了SQL Server 性能調(diào)優(yōu)之查詢從20秒至2秒的處理方法,需要的朋友可以參考下2017-07-07SQL Server代理服務(wù)無法啟動(dòng)的解決方法
錯(cuò)誤MSSQLSERVERSQLServerAgent could not be started (reason: SQLServerAgent 必須能夠以 SysAdmin 身份連接到 SQLServer,但“(未知)”不是 SysAdmin 角色的成員)2013-02-02SQL Server存儲(chǔ)過程中編寫事務(wù)處理的方法小結(jié)
這篇文章主要介紹了SQL Server存儲(chǔ)過程中編寫事務(wù)處理的方法,結(jié)合實(shí)例形式總結(jié)分析了三種存儲(chǔ)過程中編寫事務(wù)處理的方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-03-03用SQL語句實(shí)現(xiàn)隨機(jī)查詢數(shù)據(jù)并不顯示錯(cuò)誤數(shù)據(jù)的方法
用SQL語句實(shí)現(xiàn)隨機(jī)查詢數(shù)據(jù)并不顯示錯(cuò)誤數(shù)據(jù)的方法...2007-11-11SqlServer備份數(shù)據(jù)庫的4種方式介紹
這篇文章主要介紹了SqlServer備份數(shù)據(jù)庫的4種方式介紹,本文講解了用sqlserver的維護(hù)計(jì)劃、通過腳本+作業(yè)的方式備份數(shù)據(jù)庫(非xp_cmdshell和xp_cmdshell)、用powershell調(diào)用sqlcmd來執(zhí)行備份命令幾種方式,需要的朋友可以參考下2015-02-02淺談SELECT?*會(huì)導(dǎo)致查詢效率低的原因
本文主要介紹了淺談SELECT?*會(huì)導(dǎo)致查詢效率低的原因,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07