你好,我只想知道一些事情...... 说我有以下查询..
DECLARE @earninglist varchar(1000)
Set @earninglist=STUFF((SELECT DISTINCT '],['
+LTRIM([Description]) FROM
PR_Earnings
ORDER BY '],[' + LTRIM([Description])
FOR XML PATH('')
), 1, 2, '')+']'
Declare @sql varchar(max)
set @sql='Select '+@earninglist+' from earnings;'
exec(@sql);
说收入清单包含以下字段:'Cola','Incentives' 如何声明@earninglist以便我可以获得以下查询:
set @sql='Select Coalesce([Cola],0)Cola,Coalesce([Incentives],0)Incentives from earnings'
使用东西..请帮忙..
嗯......如果你注意到第一组@sql中的@earninglist,它将会是这样的:'Select [Cola],[Incentives] from earnings'
我想做的是让@earninglist的结果集成为像这样:'Select Coalesce([Cola],0)Cola,Coalesce([Incentives],0)Incentives from earnings'
..有可能吗?
答案 0 :(得分:1)
尝试:
DECLARE @earninglist varchar(1000)
Set @earninglist=STUFF((SELECT DISTINCT '],Coalesce([' +LTRIM([Description])+'],0)['+LTRIM([Description]) FROM
PR_Earnings
ORDER BY '],Coalesce([' +LTRIM([Description])+'],0)['+LTRIM([Description])
FOR XML PATH('')
), 1, 2, '')+']'
Declare @sql varchar(max)
set @sql='Select '+@earninglist+' from earnings;'
exec (@sql);