跨数据库访问数据表:

opendatasource('sqloledb', 'data source=DATABAK ;UID=sa;pwd=).数据库.dbo.表名

跨数据库访问数据表:

Create Procedure KqToDeptPersonMday
As
begin
--人事离职处理 更新人员表
declare @PersonNo as int,@Name as nvarchar(10),@duty as nvarchar(10)
--拷贝人事系统的离职人员表到临时表#templz
select cast(工号 as int) as 工号,姓名,直间接 into #templz from opendatasource('sqloledb', 'data source=DATABAK ;UID=sa;pwd=).gwidb.dbo.out_TANXUEMEISYSTEMpawf
--拷贝人事系统的在职人员表到临时表#tempzz
select cast(工号 as int) as 工号,姓名,直间接 into #tempzz from opendatasource('sqloledb', 'data source=DATABAK ;UID=sa;pwd=).gwidb.dbo.out_TANXUEMEISYSTEMpawf

declare Cursorpawf cursor
local static for select * from #templz
Open Cursorpawf
while 1=1
begin
fetch Cursorpawf into
@PersonNo
,@Name
,@duty
if @@fetch_status<>0
break
if @PersonNo is not null
begin
---opendatasource('sqloledb', 'data source=SYTELINE ;UID=sa;pwd=SVTEP').SHATDB.dbo.DeptPerson //打开数据表
if exists(select * from syteline.SHATDB.dbo.DeptPerson where PersonNo=@PersonNo)
begin
--存在更新该人员的在职状态
update syteline.SHATDB.dbo.DeptPerson
set Workflag='N'
where PersonNo=@PersonNo
end
else
begin
--不存在不处理
end
end
else
end
close Cursorpawf
deallocate Cursorpawf
--人事在职人员更新处理,(存在更新人员的工号和姓名,不存在新增工号 姓名 在职 直间接)
declare CursorPerf cursor
local static for select cast(工号 as int) as 工号,姓名,直间接 from #tempzz
Open CursorPerf
while 1=1
begin
fetch CursorPerf into
@PersonNo
,@Name
,@duty
if @@fetch_status<>0
break
if @PersonNo is not null
begin
if exists(select * from syteline.SHATDB.dbo.DeptPerson where PersonNo=@PersonNo)
begin
--存在更新该人员的工号和姓名
update syteline.SHATDB.dbo.DeptPerson
set PersonNo=@PersonNo,[Name]=@Name
where PersonNo=@PersonNo
end
else
begin
--不存在新增该人员 工号 姓名 在职状态 和直间接
insert syteline.SHATDB.dbo.DeptPerson(PersonNo,[Name],Workflag,duty)values(@PersonNo,@Name,'Y',@duty)
end
end
else
end
close CursorPerf
deallocate CursorPerf
end
drop table #templz
drop table #tempzz

你可能感兴趣的:(数据库)