一个游标的使用

create rocedure sp_getNewCompanyJob
(
@recordcount int
)
as

declare @com_job_table table(c_j_id uniqueidentifier,c_id int,c_name varchar(100),j_title_all varchar(8000),lastlogindate datetime)
declare @c_id int
declare @c_name varchar(50)
declare @j_id int
declare @j_title varchar(100)
declare @all varchar(1000)
declare @date datetime
declare @count int
declare @href varchar(50)
declare @link varchar(50)
declare @jobcount int
set @jobcount=0
declare c_j_cursor  cursor for
select  rc_company.c_id,rc_company.c_name,rc_job.j_id,rc_job.j_title ,e_date_lastlogin
from rc_euser
inner join rc_company on rc_euser.e_id=rc_company.e_id
inner join rc_job on rc_euser.e_id=rc_job.e_id
where  rc_euser.e_state=1 and rc_euser.e_top=1 and rc_company.c_state=1 and c_license is not null
order by e_date_lastlogin desc, rc_job.j_date desc
open c_j_cursor
fetch next from c_j_cursor into @c_id,@c_name,@j_id,@j_title,@date
WHILE @@FETCH_STATUS = 0
begin
 
 select @count=count(c_id) from @com_job_table where c_id=@c_id
 if @count=0
 begin
  set @jobcount=0
  set @jobcount=1
  set @link='view_company.aspx?cid='+cast(@c_id as varchar(10))+'&'+'jid='+cast(@j_id as varchar(10))
  set @href='<a href='+@link+'>'+@j_title+'</a>'
  insert into @com_job_table values(newid(),@c_id,@c_name,@href,@date)
 end 
 else
 begin
         set @jobcount=@jobcount+1
  if @jobcount <=3
  begin
  set @link='view_company.aspx?cid='+cast(@c_id as varchar(10))+'&'+'jid='+cast(@j_id as varchar(10))
  set @href='<a href='+@link+'>'+@j_title+'</a>'
  select @all=j_title_all from @com_job_table where  c_id=@c_id
  set @all=@all+','+@href
  update @com_job_table set j_title_all=@all where c_id=@c_id 
  end
 end

fetch next from c_j_cursor into @c_id,@c_name,@j_id,@j_title,@date
end
close c_j_cursor--关闭
DEALLOCATE c_j_cursor--释放
select @all=j_title_all from @com_job_table where  c_id=@c_id
set @all=@all+'a>'
update @com_job_table set j_title_all=@all where c_id=@c_id 
if @recordcount=0
begin
select * from @com_job_table
return
end
else
begin
select top 10 * from @com_job_table
end

posted @ 2008-01-25 21:00  痴人说梦  阅读(145)  评论(0编辑  收藏  举报