wwwwgou兄,求去重复值
@fieldlist重复,求去重复
CREATE PROC up_yourprocname
@emp_id varchar(100),
@on_date CHAR(6),
@end_date CHAR(6),
@title NVARCHAR(500)
AS
SET NOCOUNT ON
DECLARE @SQL2 VARCHAR(MAX)
declare @fieldlist varchar(8000)
SET @fieldlist=
STUFF(
(
select ',' + QUOTENAME(wage_subject_name) from wage_subject
where wage_subject_id in (select distinct(a.wage_subject_id) from wage_set_val a,wage_set b
where b.voucher_id=a.voucher_id and a.emp_id=@emp_id and a.voucher_id in (select voucher_id from wage_set
where data_month>=@on_date and data_month<=@end_date AND CHARINDEX(','+title+',', ','+@title+',') > 0) ) --and a.voucher_id='4'
group by wage_subject_id,wage_subject_name order by wage_subject_id FOR XML PATH('')
),1,1,'')
--SELECT @fieldlist
--确保你的@sql格式没问题,然后只能动态拼SQL
SET @SQL2 = '
SELECT *
FROM
(
SELECT
b.title 标题,
f.wage_set_name 发放类别 ,
b.data_month 月份,
-- d.dept_id ,
-- a.emp_id ,
-- d.note_info ,
e.wage_subject_name ,
a.val
FROM wage_set_val a ,
wage_set b ,
ebs_v c ,
wage_set_emp d,
wage_subject E,
wage_set_type f
WHERE a.voucher_id = b.voucher_id
and e.wage_subject_id=a.wage_subject_id
AND b.voucher_id = c.voucher_id