自動檢測兩個資料庫之間物件的儲存過程
-- ============================================================
-- Procedure Name : wh_check_dataobject
-- Function : check the dataobjects between two database
-- Failure return : 1
-- Success return : 0
--
-- Parameters :
-- @database_check : the database that is checked
-- @dataobject : (tb-table, sp-stored procedure, fn-function, tr-trigger, vw-view, all-all objects)
-- author : royalhigh, Jan, 2005/20
-- ============================================================
IF EXISTS (select * from sysobjects where id = object_id(N'wh_check_dataobject') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE wh_check_dataobject
GO
CREATE procedure wh_check_dataobject
(
@database_check varchar(128),
@dataobject varchar(3)
)
--WITH ENCRYPTION
AS
DECLARE @script_table table
(
col_scripts varchar(400)
)
DECLARE @object_table table
(
col_objects varchar(128)
)
DECLARE @scripts varchar(400)
BEGIN
SET NOCOUNT ON
IF (@database_check IS NULL or @database_check = '')
BEGIN
RAISERROR ('Parameter can not be NULL, input them, Please', 16, 1) WITH NOWAIT
RETURN 1
END
-- 1. TABLE CHECK
IF (@dataobject = 'tb' OR @dataobject = 'all')
BEGIN
PRINT 'the inexistent table:'
insert into @script_table(col_scripts)
select 'IF NOT EXISTS (SELECT 1 FROM ' + @database_check + '.dbo.sysobjects WHERE name = N''' + name + ''')'
+ ' PRINT''' + name + ''''
from dbo.sysobjects where xtype = 'U'
IF (@@error <> 0)
BEGIN
RETURN 1
END
DECLARE cur_temp CURSOR
FAST_FORWARD FOR
select col_scripts from @script_table
OPEN cur_temp
FETCH NEXT FROM cur_temp INTO @scripts
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC (@scripts)
IF (@@error <> 0)
BEGIN
CLOSE cur_temp
DEALLOCATE cur_temp
RETURN 1
END
FETCH NEXT FROM cur_temp INTO @scripts
END
CLOSE cur_temp
DEALLOCATE cur_temp
DELETE FROM @script_table
DELETE FROM @object_table
SET @scripts = ''
END
-- 2. SP CHECK
IF (@dataobject = 'sp' OR @dataobject = 'all')
BEGIN
PRINT'the inexistent stored procedure:'
insert into @script_table(col_scripts)
select 'IF NOT EXISTS (SELECT 1 FROM ' + @database_check + '.dbo.sysobjects WHERE name = N''' + name + ''')'
+ ' PRINT''' + name + ''''
from dbo.sysobjects where xtype = 'P'
IF (@@error <> 0)
BEGIN
RETURN 1
END
DECLARE cur_temp CURSOR
FAST_FORWARD FOR
select col_scripts from @script_table
OPEN cur_temp
FETCH NEXT FROM cur_temp INTO @scripts
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC (@scripts)
IF (@@error <> 0)
BEGIN
CLOSE cur_temp
DEALLOCATE cur_temp
RETURN 1
END
FETCH NEXT FROM cur_temp INTO @scripts
END
CLOSE cur_temp
DEALLOCATE cur_temp
DELETE FROM @script_table
DELETE FROM @object_table
SET @scripts = ''
END
-- 3. FUNCTION CHECK
IF (@dataobject = 'fn' OR @dataobject = 'all')
BEGIN
PRINT'the inexistent function:'
insert into @script_table(col_scripts)
select 'IF NOT EXISTS (SELECT 1 FROM ' + @database_check + '.dbo.sysobjects WHERE name = N''' + name + ''')'
+ ' PRINT''' + name + ''''
from dbo.sysobjects where xtype in('FN','TF', 'IF')
IF (@@error <> 0)
BEGIN
RETURN 1
END
DECLARE cur_temp CURSOR
FAST_FORWARD FOR
select col_scripts from @script_table
OPEN cur_temp
FETCH NEXT FROM cur_temp INTO @scripts
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC (@scripts)
IF (@@error <> 0)
BEGIN
CLOSE cur_temp
DEALLOCATE cur_temp
RETURN 1
END
FETCH NEXT FROM cur_temp INTO @scripts
END
CLOSE cur_temp
DEALLOCATE cur_temp
DELETE FROM @script_table
DELETE FROM @object_table
SET @scripts = ''
END
-- 4. TRIGGER CHECK
IF (@dataobject = 'tr' OR @dataobject = 'all')
BEGIN
PRINT'the inexistent trigger:'
insert into @script_table(col_scripts)
select 'IF NOT EXISTS (SELECT 1 FROM ' + @database_check + '.dbo.sysobjects WHERE name = N''' + name + ''')'
+ ' PRINT''' + name + ''''
from dbo.sysobjects where xtype = 'TR'
IF (@@error <> 0)
BEGIN
RETURN 1
END
DECLARE cur_temp CURSOR
FAST_FORWARD FOR
select col_scripts from @script_table
OPEN cur_temp
FETCH NEXT FROM cur_temp INTO @scripts
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC (@scripts)
IF (@@error <> 0)
BEGIN
CLOSE cur_temp
DEALLOCATE cur_temp
RETURN 1
END
FETCH NEXT FROM cur_temp INTO @scripts
END
CLOSE cur_temp
DEALLOCATE cur_temp
DELETE FROM @script_table
DELETE FROM @object_table
SET @scripts = ''
END
-- 5. VIEW CHECK
IF (@dataobject = 'vw' OR @dataobject = 'all')
BEGIN
PRINT'the inexistent view:'
insert into @script_table(col_scripts)
select 'IF NOT EXISTS (SELECT 1 FROM ' + @database_check + '.dbo.sysobjects WHERE name = N''' + name + ''')'
+ ' PRINT''' + name + ''''
from dbo.sysobjects where xtype = 'V'
IF (@@error <> 0)
BEGIN
RETURN 1
END
DECLARE cur_temp CURSOR
FAST_FORWARD FOR
select col_scripts from @script_table
OPEN cur_temp
FETCH NEXT FROM cur_temp INTO @scripts
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC (@scripts)
IF (@@error <> 0)
BEGIN
CLOSE cur_temp
DEALLOCATE cur_temp
RETURN 1
END
FETCH NEXT FROM cur_temp INTO @scripts
END
CLOSE cur_temp
DEALLOCATE cur_temp
DELETE FROM @script_table
DELETE FROM @object_table
SET @scripts = ''
END
IF (@@error <> 0)
BEGIN
RETURN 1
END
END
GO
-- ============================================================
1、使用說明
檢查X資料庫中是否存在Y版本的所有物件
(1) 在標準Y版本資料庫建立SP:wh_check_dataobject
(2) 執行wh_check_dataobject,得到比較結果
2、使用方法
@database_check :代表需要檢查的X資料庫名
@dataobject :(tb-table, sp-stored procedure, fn-function, tr-trigger, vw-view, all-all objects)
例如:wh_check_dataobject j_jill, 'all'
3、在SP中,不能呼叫object_id來取物件id值,而只能用name做為條件在sysobjects中檢索來判斷物件是否存在。
因為用“SELECT 1 FROM ' + @database_check + '.dbo.sysobjects”切換過資料庫,而object_id只取當前資料庫,
二者取的不是同一個庫,id值會有差別;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/66009/viewspace-787548/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 資料庫開發---常用物件-儲存過程資料庫物件儲存過程
- 資料庫儲存過程資料庫儲存過程
- SQL Server 比較兩個資料庫的檢視和儲存過程結構差異SQLServer資料庫儲存過程
- 使用儲存過程(PL/SQL)向資料庫中儲存BLOB物件儲存過程SQL資料庫物件
- 基於儲存過程的百萬級測試資料自動生成儲存過程
- 【資料庫】資料庫儲存過程(一)資料庫儲存過程
- MySql資料庫——儲存過程MySql資料庫儲存過程
- 儲存過程呼叫不同資料庫的資料儲存過程資料庫
- 資料庫設計:儲存過程資料庫儲存過程
- 檢視和儲存過程相關物件儲存過程物件
- 【故障公告】1個儲存過程拖垮整個資料庫儲存過程資料庫
- 淺談資料庫中的儲存過程資料庫儲存過程
- 資料庫許可權-儲存過程資料庫儲存過程
- 自動編號的儲存過程 (轉)儲存過程
- 資料庫設計:儲存過程主體資料庫儲存過程
- Mysql 資料庫水平分表 儲存過程MySql資料庫儲存過程
- 自動生成對錶進行插入和更新的儲存過程的儲存過程 (轉)儲存過程
- 如何檢測被鎖住的Oracle儲存過程Oracle儲存過程
- 查詢當前資料庫存在某個字串的儲存過程資料庫字串儲存過程
- 批量插入資料的儲存過程儲存過程
- 通過oracle 資料庫連結db link自動化備份生產庫的指令碼儲存過程Oracle資料庫指令碼儲存過程
- oracle 匿名儲存過程插入大量測試資料Oracle儲存過程
- sqlserver資料庫還原儲存過程指令碼SQLServer資料庫儲存過程指令碼
- 頭歌資料庫實驗六:儲存過程資料庫儲存過程
- 【SqlServer】清除過期資料的儲存過程SQLServer儲存過程
- 儲存過程返回資料集儲存過程
- 儲存過程在主從庫上的測試儲存過程
- 兩種SQL分頁方法儲存過程和遊標儲存過程SQL儲存過程
- MySQL之儲存過程MySql儲存過程
- 使用自動儲存管理 (ASM)建立資料庫ASM資料庫
- MySql儲存過程—2、第一個MySql儲存過程的建立MySql儲存過程
- SQL 儲存過程裡呼叫另一個儲存過程SQL儲存過程
- vue資料自動儲存的一個小DemoVue
- Oracle資料庫學習之儲存過程--提高程式執行的效率Oracle資料庫儲存過程
- Oracle資料庫儲存結構之間的關係Oracle資料庫
- 分享一個批量產生隨機測試資料的MySQL儲存過程隨機MySql儲存過程
- 把自編儲存過程設定為系統儲存過程儲存過程
- 如何基於oracle 11.2.0.4資料庫版本在資料庫間遷移非表特殊物件類比如序列及儲存過程Oracle資料庫物件儲存過程