我有下表(称为temp1):
+-------+------+------+------+----------------------+
| cntry | col1 | col2 | col3 | ACV Distribution (w) |
+-------+------+------+------+----------------------+
| UK | 100 | 100 | 100 | 0 |
| FR | 100 | 100 | 100 | 0 |
+-------+------+------+------+----------------------+
运行此语句时:
DECLARE @listStr varchar(MAX);
DECLARE @query varchar(MAX);
drop table if exists temp2 ;
set @liststr = ( select * from temp1 where cntry = 'UK' for xml path('') )
set @query =('SELECT top 10 col1,col2,col3,' + @listStr + ' INTO temp2 FROM temp3' )
execute(@query);
SELECT * FROM temp2 ;
我收到此错误:
错误:列名ACV分布(w)包含FOR XML要求的无效XML标识符
我被困在这里,因为我无法更改cols的名称,而不得不使用动态sql。