日期:2014-05-16 浏览次数:20565 次
/*
函数名称: huoqu_next
函数功能:获取会员回访下一条数据
参数说明:
@BsTableName
@FzTableName
@gongsiNo 公司编号
@Card_jlrq_start 查找会员卡建立日期
@Card_jlrq_end 到此日期日期
@work_no 维修单号
*/
CREATE PROCEDURE huoqu_next
@BsTableName varchar(255), -- 表名
@FzTableName varchar(255),
@gongsiNo varchar(10),
@Card_jlrq_start datetime,
@Card_jlrq_end datetime,
@work_no varchar(50)
as
SET nocount on
if OBJECT_ID('tempdb..#temp') is not null
drop table #temp
SELECT a.card_no ,a.Card_jlrq ,a.card_kehu_mc ,a.card_kehu_shouji,
a.gongsiNo ,a.gongsiMc ,MAX(b.work_no) work_no ,
b.xche_ssje ,min(c.wxxm_mc) wxxm_mc ,
identity(int,1,1) rownum
into #temp
FROM @BsTableName.dbo.[Card] a
LEFT JOIN @BsTableName.dbo.[work_pz_sj] b
ON a.card_no = b.card_no
LEFT JOIN @BsTableName.dbo.[work_mx_sj] c ON b.work_no = c.work_no
WHERE a.gongsiNo=@gongsiNo and a.Card_jlrq >=@Card_jlrq_start
and a.Card_jlrq <=@Card_jlrq_end
and not exists(select 1 from @FzTableName.dbo.[NewMemberVisit] n
where a.card_no = n.card_no)
GROUP BY a.card_no ,a.Card_jlrq ,a.card_kehu_mc ,a.card_kehu_shouji,
a.gongsiNo,a.gongsiMc ,b.xche_ssje
ORDER BY card_jlrq ASC
select *
from #temp t
where rownum =(select rownum+1 from #temp t where work_no=@work_no)
GO