SQL问题,至wwwwgou老弟,求助
DECLARE @SQL2 VARCHAR(MAX),@emp_id nvarchar(8),@on_date nvarchar(6),@end_date nvarchar(8)
set @emp_id='0001'
set @on_date='201305'
set @end_date='201308'
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 title in ('部室基础工资','领导效益工资')) ) --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