declare @sql varchar(4000)
set @sql='select 1 as id'
select @sql=@sql+' ,max(case Name when '''+Name+''' then score end)['+Name+']'
from test
set @sql=@sql+' from test'
print @sql
exec(@sql)
SQl行列转换
最新推荐文章于 2025-01-09 22:06:35 发布
本文介绍了一个使用T-SQL动态构建复杂查询的过程。通过定义变量@sql,并逐步拼接SQL语句,最终实现了从test表中选择特定列并计算最大值的功能。此方法适用于需要灵活改变查询条件的场景。
3万+

被折叠的 条评论
为什么被折叠?



