sql2000数据库同步,写的存储过程循环不运行。请高手帮忙看看!!!
一个数据库是HWATT,从HWATT数据库把用户导入到kaoqin数据库中。
CREATE PROCEDURE INSERTUSER AS
begin
declare @userid int
declare @username int
declare @count int
declare @userkaoname varchar
declare @sqlstr varchar
set @count = 0
select @count = count(*) from HWATT.dbo.KQZ_Employee where Birthday between dateadd(wk,datediff(wk,0,getdate()),0) and getdate()
while @count>0
begin
select @sqlstr = 'select top('+convert(varchar,@count)+') '+convert(varchar,@userid)+' = EmployeeCode, '+convert(varchar,@userkaoname)+' = EmployeeName from HWATT.dbo.KQZ_Employee where Birthday between dateadd(wk,datediff(wk,0,getdate()),0) and getdate()'
exec (@sqlstr)
select @username = userid from userinfo where BADGENUMBER = @userid
if(@username<0)
begin
insert into USERINFO(BADGENUMBER,NAME,DEFAULTDEPTID,ATT,INLATE,OUTEARLY,OVERTIME,SEP,HOLIDAY,LUNCHDURATION,privilege,InheritDeptSch,InheritDeptSchClass,AutoSchPlan,MinAutoSchInterval,RegisterOT,InheritDeptRule,EMPRIVILEGE) VALUES(@userid,@userkaoname,'1','1','1','1','1','1','1','1','0','1','1','1','24','1','1','0')
set @count = convert(int,@count)-1
end
set @count = convert(int,@count)-1
end
end
GO 数据库 sql select
[解决办法]
CREATE PROCEDURE INSERTUSER
AS
set nocount on
begin try
insert into USERINFO(BADGENUMBER,NAME,DEFAULTDEPTID,ATT,INLATE,OUTEARLY,OVERTIME,SEP,HOLIDAY,LUNCHDURATION,privilege,
InheritDeptSch,InheritDeptSchClass,AutoSchPlan,MinAutoSchInterval,RegisterOT,InheritDeptRule,EMPRIVILEGE)
select a.EmployeeCode,a.EmployeeName,'1','1','1','1','1','1','1','1','0','1','1','1','24','1','1','0'
from HWATT.dbo.KQZ_Employee a
inner join userinfo b on a.EmployeeCode=b.BADGENUMBER -->一定要用inner join 连接
where Birthday between dateadd(wk,datediff(wk,0,getdate()),0) and getdate()
and not exists (select 1 from USERINFO c where a.EmployeeCode=c.BADGENUMBER) -->加这一么判断过滤掉重复
select '运行成功'
end try
begin catch
select '运行失败'
end catch
GO