新人自学SQL中,在动态行列转换发现个问题不会解决,求助于各位大大,这是我的语句
declare @text varchar(8000)
set @text='select textNo,textName'
select @text= @text+', max(case TextDate when '''+textDate+''' then score else 0 end) [' + TextDate+ ']'from (select distinct Date from text)as a
set @text=@text+'from text group by textNo,textName'
exec(@text)
提示数据类型 varchar 和 date 在 add 运算符中不兼容,其中数据库TextDate 为DATE类型,score 为float,其余都是varchar,请问该怎么修正
------解决方案--------------------
DECLARE @text VARCHAR(8000)
SET @text = 'select textNo,textName'
SELECT @text = @text + ', max(case TextDate when '''
+ CONVERT(VARCHAR(10), textDate, 23) + ''' then score else 0 end) ['
+ TextDate + ']'
FROM ( SELECT DISTINCT
[Date]
FROM [text]
) AS a
SET @text = @text + 'from text group by textNo,textName'
EXEC(@text)
------解决方案--------------------
子查询 select distinct Date from text 中根本没有 TextDate 字段,你拼到 @text 中的 TextDate 到底是什么东西?