sql 判斷數(shù)據(jù)庫,表,存儲(chǔ)過程等是否存在的代碼
發(fā)布日期:2022-01-30 14:33 | 文章來源:腳本之家
代碼:
--庫是否存在 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='表名'
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章