在同一个Sql Server中建了两个数据库 dbA,dbB。怎样操作两个数据库中的表呢?我想了个办法,存储过程如下:
use [dbA] //在数据库A中操作
if object_id('tempdb..#tempTable') is not null //判断是否已经存在临时表'#tempTable'
Begin
drop table #tempTable //如果存在,则先删除
End
select * into #temptable from employee //创建一张临时表,把数据库A中employee表的内容存进临时表
use [dbB] //在数据库B中操作
select * from #temptable a,Department b where a.dpId=b.dpId //联表查询
if object_id('tempdb..#tempTable') is not null //判断是否已经存在临时表'#tempTable'
Begin
drop table #tempTable //如果存在,则先删除
End
select * into #temptable from employee //创建一张临时表,把数据库A中employee表的内容存进临时表
use [dbB] //在数据库B中操作
select * from #temptable a,Department b where a.dpId=b.dpId //联表查询