- SQL code
set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo--零售项目综合ALTER proc [dbo].[P_零售项目综合] @yyyymmfrom int,@yyyymmto intasbegin with basetable as (SELECT V_人员详细.Staff_Code, V_人员详细.Region_Code, V_人员详细.Sales_Name, V_人员详细.Employee_Name, 'NFABI' AS Project, R_new115NFABI.v, R_new115NFABI.ddFROM V_人员详细 LEFT JOIN (SELECT R_new115人员培训信息按月.Employee_Code, R_new115人员培训信息按月.v, R_new115人员培训信息按月.dd FROM R_new115人员培训信息按月 WHERE ((R_new115人员培训信息按月.Training_Class) = 'NFABI Exam-Call Center考试') and dd>[email protected] and dd<[email protected])R_new115NFABI ON V_人员详细.Staff_Code = R_new115NFABI.Employee_Code UNION ALLSELECT V_人员详细.Staff_Code, V_人员详细.Region_Code, V_人员详细.Sales_Name, V_人员详细.Employee_Name, 'elearning' AS Project, R_new115elearning.v, R_new115elearning.ddFROM V_人员详细 LEFT JOIN (SELECT R_new115人员培训信息按月.Employee_Code, R_new115人员培训信息按月.v, R_new115人员培训信息按月.dd FROM R_new115人员培训信息按月 WHERE ((R_new115人员培训信息按月.Training_Class) = 'E-learning Exam') and dd>[email protected] and dd<[email protected])R_new115elearning ON V_人员详细.Staff_Code = R_new115elearning.Employee_Code UNION ALLSELECT V_人员详细.Staff_Code, V_人员详细.Region_Code, V_人员详细.Sales_Name, V_人员详细.Employee_Name, '销量' AS Project, R_new115销量.v, R_new115销量.ddFROM V_人员详细 LEFT JOIN (SELECT Employee_Code, v, dd FROM R_new115销量按月 WHERE dd>[email protected] and dd<[email protected])R_new115销量 ON V_人员详细.Staff_Code = R_new115销量.Employee_Code UNION ALL SELECT V_人员详细.Staff_Code, V_人员详细.Region_Code, V_人员详细.Sales_Name, V_人员详细.Employee_Name, '星级' AS Project, R_new115星级.v, R_new115星级.ddFROM V_人员详细 LEFT JOIN (SELECT Employee_Code, v, dd FROM R_new115星级按月 WHERE dd>[email protected] and dd<[email protected])R_new115星级 ON V_人员详细.Staff_Code = R_new115星级.Employee_Code) select * into #t from basetable declare @sql varchar(max)set @sql='' [email protected]select @[email protected]+',['+case when dd is null then '' else dd end +']' from #t group by dd order by dd--变量多值赋值if @sql=',[]'select '没有数据' as '没有数据'elsebeginSELECT @sql=REPLACE(@sql,',[]','')--去掉nullset @sql=stuff(@sql,1,1,'')--去掉首个','exec('select * from #t pivot (sum(v) for dd in(' [email protected]+ ')) as c')endend
执行的时候
@yyyymmfrom = 201101,
@yyyymmto = 201102 出错标题的错误 但把yyyymmto=20112 就不出错了 求解
------解决方案--------------------
试试将该变量所含的数据用引号引起来,如果是要作为动态语句的执行部分字串,则更要这样处理.
------解决方案--------------------
好长,print @sql看看是什么情况
------解决方案--------------------
print @sql
------解决方案--------------------
[email protected],
字段dd的数据类型是什么?
------解决方案--------------------
先需要确定是哪里错误了
------解决方案--------------------
- SQL code
--为什么时间不用datetime型非要搞个int?自作孽