日期:2014-05-18  浏览次数:20443 次

从数据类型 nvarchar 转换为 float 时出错。
SQL code

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go



--零售项目综合

ALTER proc [dbo].[P_零售项目综合] 
@yyyymmfrom  int,
@yyyymmto  int
as

begin 
with basetable as

 (SELECT V_人员详细.Staff_Code, V_人员详细.Region_Code, V_人员详细.Sales_Name, V_人员详细.Employee_Name, 'NFABI' AS Project, R_new115NFABI.v, R_new115NFABI.dd
FROM 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>=@yyyymmfrom and dd<=@yyyymmto)

R_new115NFABI ON V_人员详细.Staff_Code = R_new115NFABI.Employee_Code
 UNION ALL

SELECT V_人员详细.Staff_Code, V_人员详细.Region_Code, V_人员详细.Sales_Name, V_人员详细.Employee_Name, 'elearning' AS Project, R_new115elearning.v, R_new115elearning.dd
FROM 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>=@yyyymmfrom and dd<=@yyyymmto)

R_new115elearning ON V_人员详细.Staff_Code = R_new115elearning.Employee_Code
 UNION ALL

SELECT V_人员详细.Staff_Code, V_人员详细.Region_Code, V_人员详细.Sales_Name, V_人员详细.Employee_Name, '销量' AS Project, R_new115销量.v, R_new115销量.dd
FROM V_人员详细 LEFT JOIN 

    (SELECT  Employee_Code, v, dd
    FROM R_new115销量按月
    WHERE dd>=@yyyymmfrom and dd<=@yyyymmto)


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星级.dd
FROM V_人员详细 LEFT JOIN 

    (SELECT  Employee_Code, v, dd
    FROM R_new115星级按月
    WHERE dd>=@yyyymmfrom and dd<=@yyyymmto)


R_new115星级 ON V_人员详细.Staff_Code = R_new115星级.Employee_Code) 

select * into #t from basetable 

declare @sql varchar(max)
set @sql=''  --初始化变量@sql
select @sql=@sql+',['+case when dd is null then '' else dd end +']' from #t group by dd order by dd--变量多值赋值
if @sql=',[]'
select '没有数据'  as '没有数据'
else
begin
SELECT @sql=REPLACE(@sql,',[]','')--去掉null
set @sql=stuff(@sql,1,1,'')--去掉首个','
exec('select * from #t pivot (sum(v) for dd in(' +@sql+ ')) as c')
end

end



执行的时候
@yyyymmfrom = 201101,
@yyyymmto = 201102 出错标题的错误 但把yyyymmto=20112 就不出错了 求解

------解决方案--------------------
试试将该变量所含的数据用引号引起来,如果是要作为动态语句的执行部分字串,则更要这样处理.
------解决方案--------------------
好长,print @sql看看是什么情况
------解决方案--------------------
print @sql
------解决方案--------------------
参数@yyyymmto是int型,

字段dd的数据类型是什么?

------解决方案--------------------
先需要确定是哪里错误了
------解决方案--------------------
SQL code

--为什么时间不用datetime型非要搞个int?自作孽