我需要将单行的所有值放入一个字符串中,例如
表:
CustomerName  Address Zip
Alex          Moscow  1234结果是:
CustomerName: Alex
Address: Moscow
Zip: 1234重要提示-我不知道字段名称/类型,所以它应该遍历所有字段,所有非空值都添加到列表中。
看起来它可以使用xquery做到这一点,但是找不到正确的语法。有什么提示吗?
谢谢!
发布于 2011-08-22 17:43:54
不像Mikael的解决方案那么优雅。但我还是想把它包括进来。
DECLARE @yourtable nvarchar(128)
DECLARE @sql as nvarchar(2100)
DECLARE @col as nvarchar(2000)
SET @yourtable = '<tablename>'
SELECT @col = coalesce(@col, '' ) + '+'''+t2.column_name+': '' + cast([' + t2.column_name + '] as varchar) + char(32)'
FROM INFORMATION_SCHEMA.TABLES t1 join 
INFORMATION_SCHEMA.COLUMNS t2 on t1.table_name = t2.table_name
where t2.is_nullable = 'NO' and t1.table_name = @yourtable
and t1.table_type = 'BASE TABLE' and t1.table_schema = t2.table_schema
and t2.table_schema = 'dbo'
SET  @sql = 'select ' + stuff(@col, 1,1,'') +' from ' + @yourtable
EXEC (@sql)https://stackoverflow.com/questions/7145485
复制相似问题