标题:sqlserver作业执行不正常
只看楼主
chunfirst
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-9-14
结帖率:0
已结贴  问题点数:20 回复次数:1 
sqlserver作业执行不正常
我用sqlserver语句写了一个脚本,如下所示。在查询分析器能正常获得“未登录人员信息”和“登录人员信息”。但把脚本引入作业后,只能得到“未登录人员信息”,不能得到“登录人员信息”。请问是什么原因?
declare @messages nvarchar(max)
declare @deptname nvarchar(50)
declare @position_name nvarchar(50)
declare @username nvarchar(max)
declare @role_id int
declare @rs   int
set @messages='未登录人员信息:

'
declare depts cursor for

select wdl.deptname,wdl.position_name,left(username_set,len(username_set)-1) username,rs
from
( -- wdl
select system_depts.name deptname,hhh.* from
(-- hhh
select dept_id,position_name,
(select username+',' from
(-- username_set

select users.id,users.username username,position.name position_name,depts.name deptname ,position.dept_id
 from system_positionmember member left join system_users users on users.id=member.user_id
left join system_position position on member.position_id=position.id
left join system_depts depts on users.dept_id=depts.id
where users.id not in (
select login_user from checkwork_report where login_date = convert(char(10),getdate()-1,120))
and depts.superior<>0
 ) tb
where position_name=tb1.position_name and dept_id=tb1.dept_id for xml path('')) username_set,count(*) rs
from
(--tb1

select users.id,users.username username,position.name position_name,depts.name deptname,position.dept_id
 from system_positionmember member left join system_users users on users.id=member.user_id
left join system_position position on member.position_id=position.id
left join system_depts depts on users.dept_id=depts.id
where users.id not in (
select login_user from checkwork_report where login_date = convert(char(10),getdate()-1,120))
 and depts.superior<>0) tb1

group by position_name,dept_id
) hhh
--select * from system_depts
left join system_depts on hhh.dept_id=system_depts.id
) wdl

open depts

fetch next from depts into @deptname,@position_name,@username,@rs
while (@@fetch_status=0)
begin

set @messages=@messages+ @deptname+'('+@position_name+')'+' : '+@username+'。共计'+cast(@rs as nvarchar(10))+'人未登录。'+'

'

fetch next from depts into @deptname,@position_name,@username,@rs
end

--select @messages  yy
--关闭游标
close depts
--撤销游标
deallocate depts

---登录人员信息
set @messages='

'+@messages+'已登录人员信息:'+'

'
declare depts cursor for
select wdl.deptname,wdl.position_name,left(username_set,len(username_set)-1) username,rs
from
( -- wdl
select system_depts.name deptname,hhh.* from
(-- hhh
select dept_id,position_name,
(select username+',' from
(-- username_set

select users.id,users.username username,position.name position_name,depts.name deptname ,position.dept_id
 from system_positionmember member left join system_users users on users.id=member.user_id
left join system_position position on member.position_id=position.id
left join system_depts depts on users.dept_id=depts.id
where users.id  in (
select login_user from checkwork_report where login_date = convert(char(10),getdate()-1,120))
and depts.superior<>0 ) tb
where position_name=tb1.position_name and dept_id=tb1.dept_id for xml path('')) username_set,count(*) rs
from
(--tb1

select users.id,users.username username,position.name position_name,depts.name deptname,position.dept_id
 from system_positionmember member left join system_users users on users.id=member.user_id
left join system_position position on member.position_id=position.id
left join system_depts depts on users.dept_id=depts.id
where users.id  in (
select login_user from checkwork_report where login_date = convert(char(10),getdate()-1,120))
 and depts.superior<>0) tb1

group by position_name,dept_id
) hhh
left join system_depts on hhh.dept_id=system_depts.id
) wdl



open depts

fetch next from depts into @deptname,@position_name,@username,@rs
while (@@fetch_status=0)
begin

set @messages=@messages+ @deptname+'('+@position_name+')'+' : '+@username+'。共计'+cast(@rs as nvarchar(10))+'人登录。'+'

'

fetch next from depts into @deptname,@position_name,@username,@rs
end

--关闭游标
close depts
--撤销游标
deallocate depts


declare roles cursor for

select id from system_users users right join system_user_role user_role on
users.id=user_role.user_id where user_role.role_id=1

open roles

fetch next from roles into @role_id

while (@@fetch_status=0)
begin

insert into messages(user_id,fromuser_id,source_id,type,entity_class,entity_id,url,pri,ssid,createdate,
title,content,isread,isReceiverreaded,readdate,isreply,istransmit,sms,email,im,from_id,attachs,eas,status)

select @role_id,2,1,1,null,null,null,null,null,getdate(),
convert(nvarchar(10),getdate()-1,120)+' :登录信息',@messages,
1,1,getdate(),null,null,9,0,0,0,'',0,1

fetch next from roles into @role_id

end

close roles
--撤销游标
deallocate roles
搜索更多相关主题的帖子: cursor 信息 人员 
2012-09-14 09:35
netlin
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:24
帖 子:544
专家分:4308
注 册:2012-4-9
得分:20 
好复杂的脚本,学习了!

做自己喜欢的事!
2012-09-15 08:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-381357-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.932997 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved