TSQL:将一行中所有字段的值合并为一个字符串 [英] TSQL: Values of all field in a row into one string

查看:19
本文介绍了TSQL:将一行中所有字段的值合并为一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将单行的所有值(而不是空值)放入一个字符串中,例如

i need to put all, not null, values of single row into one string, like

表格:

CustomerName  Address Zip
Alex          Moscow  1234

结果:

CustomerName: Alex
Address: Moscow
Zip: 1234

重要说明 - 我不知道字段名称/类型,因此它应该遍历所有字段,并且所有非空值都添加到列表中.

Important note - i don't know field names/types, so it should go through all fields and all not null values add to list.

看起来它可以使用 xquery 做到这一点,但找不到正确的语法.有什么提示吗?

Looks like it can do this using xquery, but can't find right syntax. Any hints?

谢谢!

推荐答案

select T2.N.value('local-name(.)', 'nvarchar(128)')+': '+
       T2.N.value('.', 'nvarchar(max)')
from (select *
      from YourTable
      for xml path(''), type) as T1(X)
  cross apply T1.X.nodes('/*') as T2(N)

这篇关于TSQL:将一行中所有字段的值合并为一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆