用sql语句复制存储过程

飞牛 飞牛 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
现在想将数据库结构移到另一个数据库
使用下面的方法复制一般的存储过程没什么问题,如果存储过程太大(超过最大字节数8060)按下面的方法就没法复制了
不知道有没有什么解决办法
希望各位大侠帮忙看看

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

/*生成表数据脚本的通用存储过程 邹建 (引用请保留此信息)--*/

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 '+@s_dbname+'.dbo.syscomments c,'+@s_dbname+'.dbo.sysobjects o
where c.id=o.id
and o.status>=0
and o.xtype=''P''
and not exists(
select * from '+@d_dbname+'.dbo.sysobjects where name=o.name)
')

--创建存储过程
declare tb cursor local for
select 'use '+@d_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 '+@d_dbname+'.dbo.syscomments c,'+@d_dbname+'.dbo.sysobjects

o,#sys_syscomments_bak ob
where c.id=o.id and o.name=ob.name and o.xtype=ob.xtype
insert '+@d_dbname+'.dbo.syscomments([id],[number],[colid],[status],[ctext])
select o.[id],ob.[number],ob.[colid],ob.[status],ob.[ctext]
from '+@d_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


...全文

相关推荐

评论 24
收藏 已收藏
分享
返回顶部
飞牛
MS-SQL Server 34,876 成员 254,635 内容