关于存储过程或试图建立问题(邹键在吗?)

daijingjie2002 2005-01-04 11:09:31
我有两个数据库a1和a2,a2中有部分a1中的对象,请问如何才能把a2没有的部分根据a1建立,如何编程实现
...全文
191 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
NinGoo 2005-01-04
  • 打赏
  • 举报
回复
select name from A1..sysobjects where xtype='P'
AND name not in
(
select name from A2..sysobjects where xtype='P')

可以列出a1有而a2没有的存储过程的名称,然后手动导入吧
daijingjie2002 2005-01-04
  • 打赏
  • 举报
回复
因为我b1中有和a1中同名的过程,但过程内容不同
NinGoo 2005-01-04
  • 打赏
  • 举报
回复
直接把a1库中所有的object导入到a1不就结了?搞那么复杂?

daijingjie2002 2005-01-04
  • 打赏
  • 举报
回复
例如在a1库中有存储过程b,在a2库中没有,如何编程实现把B存储过程从A1库中加入到A2库中?
Andy__Huang 2005-01-04
  • 打赏
  • 举报
回复
樓主好象說得不太清楚吧?你要建立什麼?是關聯查詢還是數據庫導入?
Andy__Huang 2005-01-04
  • 打赏
  • 举报
回复
如果是同一臺機子上,訪問另外一個數據庫的表時,你指定另外一個數據名就可以

select a.* ,b.* from tb db1.dbo.table1 a
left join db2.dbo.table2 b on a.id=b.id
RainYang 2005-01-04
  • 打赏
  • 举报
回复
UP吧
lxysjl 2005-01-04
  • 打赏
  • 举报
回复
不懂
daijingjie2002 2005-01-04
  • 打赏
  • 举报
回复
没有人会吗
chinaandys 2005-01-04
  • 打赏
  • 举报
回复
这很模糊,帮你UP
vinsonshen 2005-01-04
  • 打赏
  • 举报
回复
我好好的努力学习~~~
daijingjie2002 2005-01-04
  • 打赏
  • 举报
回复
邹大哥上面说的是数据倒入问题了,我想判断如果a1库中没有b1存储过程就把a1库中的b1过程加入到b1
中?
631799 2005-01-04
  • 打赏
  • 举报
回复
zjcxc(邹建) 让我把这个给你看看

参考:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_copydb]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_copydb]
GO

/*--数据库数据复制

将一个数据库中的数据复制到另一个数据库
如果某列在目标数据库中为标识列,将不会被复制

适用范围:数据库结构发生了变化,想将旧数据库进行升级
这样就可以根据新的数据库结构创建一个空库,然后
将旧数据库的所有数据复制到新库中
--邹建 203.10--*/

/*--调用示例

exec p_copydb 'bns_aa','bns_new'
exec p_copydb 'acc_五医','acc_演示数据8'
--*/
create proc p_copydb
@o_dbname sysname, --要复制数据的数据库--源数据库
@n_dbname sysname --接收数据的数据库--目标数据库
--@cleardb bit=0 --清空目标数据库
as
declare @sql nvarchar(4000)

--禁用约束/触发器,防止复制时的数据冲突
set @sql='declare #tbc cursor for select name
from '+@n_dbname+'..sysobjects where xtype=''U'' and status>=0'
exec(@sql)

declare @tbname sysname
open #tbc
fetch next from #tbc into @tbname
while @@fetch_status=0
begin
set @sql='alter table '+@n_dbname+'..['+@tbname+'] NOCHECK CONSTRAINT ALL'
exec(@sql)
set @sql='alter table '+@n_dbname+'..['+@tbname+'] disable trigger ALL'
exec(@sql)
fetch next from #tbc into @tbname
end
close #tbc

--复制数据
declare @sql1 varchar(8000)
set @sql='declare #tb cursor for select a.name from '
+@o_dbname+'..sysobjects a inner join '
+@n_dbname+'..sysobjects b on a.name=b.name
where a.xtype=''U'' and b.xtype=''U'''
exec(@sql)
open #tb
fetch next from #tb into @tbname
while @@fetch_status=0
begin
select @sql1=''
,@sql='select @sql1=@sql1+'',[''+a.name+'']'' from(
select name from '+@o_dbname+'..syscolumns where id in
(select id from '+@o_dbname+'..sysobjects where name='''+@tbname+''')
) a inner join (
select name from '+@n_dbname+'..syscolumns where status<>0x80 and id in
(select id from '+@n_dbname+'..sysobjects where name='''+@tbname+''')
) b on a.name=b.name'
exec sp_executesql @sql,N'@sql1 nvarchar(4000) out',@sql1 out

select @sql1=substring(@sql1,2,8000)
exec('insert into '+@n_dbname+'..['+@tbname+']('+@sql1
+') select '+@sql1+' from '+@o_dbname+'..['+@tbname+']')
if @@error<>0
print('insert into '+@n_dbname+'..['+@tbname+']('+@sql1
+') select '+@sql1+' from '+@o_dbname+'..['+@tbname+']')
fetch next from #tb into @tbname
end
close #tb
deallocate #tb

--数据复制完成后启用约束
open #tbc
fetch next from #tbc into @tbname
while @@fetch_status=0
begin
set @sql='alter table '+@n_dbname+'..['+@tbname+'] CHECK CONSTRAINT ALL'
exec(@sql)
set @sql='alter table '+@n_dbname+'..['+@tbname+'] enable trigger ALL'
exec(@sql)
fetch next from #tbc into @tbname
end
close #tbc
deallocate #tbc
go



daijingjie2002 2005-01-04
  • 打赏
  • 举报
回复
zjcxc(邹建) 在吗?
daijingjie2002 2005-01-04
  • 打赏
  • 举报
回复
我知道这样可以导入,这样太麻烦,我想程序化,谢谢指导

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧