这个除了用游标来查询 可以用别的实现吗 给点提示

来源:百度知道 编辑:UC知道 时间:2024/05/04 04:15:22
declare @acctnome char(4)
declare mycursorme cursor for
select distinct hosp_code from #tb_detail where doctype='ME' order by hosp_code
open mycursorme
fetch next from mycursorme into @acctnome
while @@fetch_status = 0
begin
insert into #tb_tt(hosp_code,document,job_no,tran_code,rpt_code,type_dt,log_dt,tran_dt,pages,lines,dictLen,typeLen,doctype,qaed,mon,adt)
select hosp_code,document,job_no,tran_code,rpt_code,type_dt,log_dt,tran_dt,pages,lines,dictLen,typeLen,doctype,qaed,mon,adt from #tb_detail
where doctype='ME' and hosp_code=@acctnome order by hosp_code

insert into #tb_tt(hosp_code,document,job_no,tran_code,rpt_code,type_dt,log_dt,tran_dt,pages,lines,dictLen,typeLen,doctype,qaed,mon,adt)
select 'SubMeT:'+@acctnome as hosp_code,count(document) as document,'' as job_no, @Tran_Code, '' as rpt_code,null as type_dt,null as log_dt,null as tran_dt,

还可以用关联查询或子查询的方式来实现

第一句代码:

insert into #tb_tt(hosp_code,document,job_no,tran_code,rpt_code,type_dt,log_dt,tran_dt,pages,lines,dictLen,typeLen,doctype,qaed,mon,adt)
select hosp_code,document,job_no,tran_code,rpt_code,type_dt,log_dt,tran_dt,pages,lines,dictLen,typeLen,doctype,qaed,mon,adt from #tb_detail
where doctype='ME' and hosp_code in (select distinct hosp_code from #tb_detail where doctype='ME' order by hosp_code) order by hosp_code

第二句没给你改,因为@Tran_Code不知道你是从哪搞来的!