1. 程式人生 > >SQL將資料庫中儲存過程複製到另一個庫中

SQL將資料庫中儲存過程複製到另一個庫中

use   master
go

if   exists   (select   *   from   dbo.sysobjects   where   id   =   object_id(N '[dbo].[sp_copyProce] ')   and  

OBJECTPROPERTY(id,   N 'IsProcedure ')   =   1)
drop   procedure   [dbo].[sp_copyProce]
GO

/*--生成表資料指令碼的通用儲存過程,

功能:將一個數據庫中的儲存過程,複製到另一資料庫中
目標資料庫中已經有的儲存過程不覆蓋 

   2005.01(引用請保留此資訊)--*/

/*--呼叫示例

exec   master.dbo.sp_copyProce   'a ', 'b '
--*/

create   proc   sp_copyProce
@s_dbname   sysname,   --要複製儲存過程的源資料庫名
@d_dbname   sysname     --目標資料庫名
as
set   nocount   on
if   db_id(@s_dbname)   is   null
begin
raiserror( '資料庫 "%s "不存在 ',1,16,@s_dbname)
return
end
if   db_id(@d_dbname)   is   null
begin
raiserror( '資料庫 "%s "不存在 ',1,16,@d_dbname)
return
end
select   @s_dbname= '[ '+replace(@s_dbname, '] ', ']] ')+ '] '
,@d_dbname= '[ '+replace(@d_dbname, '] ', ']] ')+ '] '

--複製儲存過程資訊到臨時表
create   table   #sys_syscomments_bak(name   sysname,xtype   char(2),number   smallint,colid  

smallint,status   smallint,ctext   varbinary(8000))
exec( '
insert   #sys_syscomments_bak
(name,xtype,number,colid,status,ctext)
select   o.name,o.xtype,c.number,c.colid,c.status,c.ctext
from   '
[email protected]
_dbname+ '.dbo.syscomments   c, '[email protected]_dbname+ '.dbo.sysobjects   o
where   c.id=o.id  
and   o.status> =0
and   o.xtype= ' 'P ' '
and   not   exists(
select   *   from   '[email protected]_dbname+ '.dbo.sysobjects   where   name=o.name)
')

--建立儲存過程
declare   tb   cursor   local   for
select   'use   '
[email protected]
_dbname+ '   exec( ' 'create   proc   dbo.[ '+replace(name,N '] ',N ']] ')+ ']   as   -- ' ')    

exec   sp_recompile   [ '+replace(name,N '] ',N ']] ')+ '] '
from   #sys_syscomments_bak
declare   @s   nvarchar(4000)
open   tb
fetch   tb   into   @s
while   @@fetch_status=0
begin
exec(@s)
fetch   tb   into   @s
end
close   tb
deallocate   tb

--複製儲存過程結構
exec   sp_configure   'allow   updates ',1   reconfigure   with   override
begin   tran
exec( '
delete   c
from   '
[email protected]
_dbname+ '.dbo.syscomments   c, '[email protected]_dbname+ '.dbo.sysobjects  

o,#sys_syscomments_bak   ob
where   c.id=o.id   and   o.name=ob.name   and   o.xtype=ob.xtype
insert   '[email protected]_dbname+ '.dbo.syscomments([id],[number],[colid],[status],[ctext])
select   o.[id],ob.[number],ob.[colid],ob.[status],ob.[ctext]
from   '[email protected]_dbname+ '.dbo.sysobjects   o,#sys_syscomments_bak   ob
where   o.name=ob.name   and   o.xtype=ob.xtype ')
commit   tran
exec   sp_configure   'allow   updates ',0   reconfigure   with   override
go


--使用測試
create   database   a
go
use   a
go
create   proc   p_test1
as
select   'test1 '
go


create   proc   p_test2
as
select   'test2 '
go

create   database   b
go

exec   master.dbo.sp_copyProce   'a ', 'b '
go
select   *   from   b.dbo.sysobjects   where   xtype= 'P '

exec   b.dbo.p_test1
exec   b.dbo.p_test2
go

use   master
go

drop   database   a,b
drop   proc   sp_copyProce

相關推薦

SQL資料庫儲存過程複製一個

use   master go if   exists   (select   *   from   dbo.sysobjects   where   id   =   object_id(N '[dbo].[sp_copyProce] ')   and   OBJE

SQL Server資料庫儲存過程定義的臨時表,真的有必要顯式刪除(drop table #tableName)嗎?

問題背景 在寫SQL Server儲存過程中,如果儲存過程中定義了臨時表,有些人習慣在儲存過程結束的時候一個一個顯式地刪除過程中定義的臨時表(drop table #tName),有些人又沒有這個習慣,對於不明真相的群眾或者喜歡思考的人會問,儲存過程中定義的臨時表,最後要不要主動刪除,為什麼

在MSSQL一個儲存過程呼叫一個儲存過程

建一個簡單的儲存過程A,輸出2個值: Create Proc A  @AOUTPUT1 int output,  @AOUTPUT2 int output  As  begin  set @AOUTPUT1 = 100  set @AOUTPUT2 = 50  returnend  Go   Crea

mssql 儲存過程呼叫一個儲存過程的結果的方法分享

摘要:   下文將分享"一個儲存過程"中如何呼叫"另一個儲存過程的返回結果",並應用到自身的運算中在實際開發中,我們經常會遇到在一個儲存過程中呼叫另一個儲存過程的返回結果(儲存過程相互應用) 實現思路:主要採用臨時表將儲存過程返回的結果集進行儲存,然後供另一個儲存過程應用。

ORACLE SQL: 從一個查詢資料插入一個

insert into expertinfo (expertid,expertname,expertcode,sex,enabled) select primarykey as expertid, name as expertname,

如何使用shell從一個檔案取出不在一個檔案的內容

本文展示如何利用shell從一個檔案中,(按行)找出不在另一個檔案中的內容。 #!/bin/bash #@filename checkAddWord.sh #allword.txt         存

sql server之在儲存過程利用OpenJsonJson字串轉化為表格

在Sql server2016的版本後,資料庫增加了對Json格式的支援,詳細資訊可以參考微軟官方文件連結 應用背景 線上訂餐系統中,購物車的內容儲存在瀏覽器快取中,所以資料庫關於訂單的設計是訂單表(訂單ID,送貨地址,使用者ID,,,,,),訂單明細表(訂

mysql用儲存過程一個的資料跟新到一個

這兩天學習mysql遊標的使用 就做了一個小練習題目 很簡單但是可以鍛鍊一下mysql遊標的使用方法 將friend1表中的資料更新到user-fri friend1 需要更新的表 user_fri 儲存過程如下 BEGIN DECLARE userid int;

mysql使用儲存過程一個的資料匯入一個

儲存過程 本人也是第一次使用儲存過程,中間遇到了很多坑,在此記錄一下,希望能幫助使用的人少踩點坑。。 先說一下我使用儲存過程的需要,其實需求很簡單就是把一個表中的資料匯入一個新的表中,並且刪除原有表中的資料,說白了就是一個數據的備份 我是使用navicat

SQL如何一個的某一列的資料複製一個的某一列裡

表一: SPRD PRD_NO      SPC          001                NULL 002               NULL 003               NULL ...                    ... 表二

sql一個的數據註入一個

一個表 .com 分享 兩個 image hbm inf left info sql之將一個表中的數據註入另一個表中 需求:現有兩張表t1,t2,現需要將t2的數據通過XZQHBM相同對應放入t1表中   t1:      t2:    思路:left join

SQL server 2012 儲存過程除錯

1、儲存過程無法直接除錯 選中要除錯的儲存過程,右擊“執行儲存過程”,根據是否需要輸入引數,若需要則可以先隨便輸入,跳轉到Exec 頁面後,輸入正確合理的引數進行,F10,開始除錯,F11,進入到被呼叫的儲存過程中。 結論:無法很好的與C#客戶端程式聯合起

SQL資料庫刪除一個表在一個不存在的記錄

SQL資料庫中刪除一個表在另一個表中不存在的記錄 刪除log表中 不存在的 goods商品資料 ①.我先查詢出了不同的資料(測試) SELECT goods_id FROM `ecs_cangku_log` WHERE `goods_id` NOT IN ( SELECT go

Sql Server 2008儲存過程傳入表值引數

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

如何一個INPUT輸入的值自動複製一個INPUT?如何用js實現?

<input type="text" value="" class="f-input ui-textbox" id="settings-mobile" name="mobile" size="30"> <input type="hidden" value="" name="

【Android Studio】一個Module直接複製一個Project需要改動哪些檔案呢

一個奇怪的需求,硬拷貝 根目錄/settings.gradle include ':app' include ':ThirdPartyDemo' // 按照樣式新增本行  這樣在【Run/Debug Configurations】中就會有該Module

MY-SQL-----資料庫---索引---儲存過程(後)

   索引 索引是一種特殊的檔案,它們包含著對資料表裡所有記錄的引用指標。 它是對資料庫表中一列或多列的值進行排序的一種結構。 簡單理解 資料庫索引好比是一本書前面的目錄,能夠加快資料庫的查詢速度, 資料庫索引就是為了提高表的搜尋效率而對某些欄位中的值建立的目錄。 建立

SQL 一個表的資料插入到一個

語法: 1.原來沒有的建立的表 select * into 表A form 表B where條件 2.原來存在的表 insert into 表A select * from 表B where 條件 示例: USE Student_Course2 IF EXISTS(SELE

sql一個的資料插入到一個的方法

列名不一定要相同,只要你在HH中列出要插入列的列表跟select from mm表中的選擇的列的列表一一對應就可以了,當然兩邊的資料型別應該是相容的。 insert into hh (fielda,f

一個裡的一張表的資料複製一個的一張表

首先,要在A和B資料庫中建立兩個同名同結構的表,其中B資料庫的表為目標表. private static void SQLCH()     {         SqlConnection ConectionFrom = new SqlConnection("Data So