简体   繁体   English

创建 XML 的“特定”形式,在 SQL 服务器 T-SQL 中没有字符串连接

[英]Creating a “specific” form of XML, without string concatenation in SQL server T-SQL

I have some weird requirements that demand a specific XML format.我有一些奇怪的要求,需要特定的 XML 格式。 Due to short deadlines and my lack of skills I decided to do a fast solution where I generate the XML through string concatenation.由于截止日期短且我缺乏技能,我决定做一个快速解决方案,通过字符串连接生成 XML。

DECLARE @tbl TABLE(personID int identity, name varchar(20), lastname varchar(20), country varchar(20));
INSERT INTO @tbl VALUES
 ('bob','bobby','USA')
,('mike','mikeson','Canada')
,('jack', 'jackson', 'Mexico')

select '<personID="' + cast(personID  as varchar) +'" country="' + country + '"/>' +
        '"<FIELD fieldname="name" value="' + name + '"/>' +
        '<FIELD fieldname="lastname" value="' + lastname + '"/>' +
'</personID>'
from @tbl

and this gives the output which I need.这给出了我需要的 output。 However, I've been told numerous times that this is not best practice, and creating XMLs through string concatenation is discouraged.但是,我多次被告知这不是最佳实践,并且不鼓励通过字符串连接创建 XML。 Is there some other way I can achieve the same outcome using more advanced XML techniques?有没有其他方法可以使用更先进的 XML 技术实现相同的结果?

--string concat, is it valid xml??
select try_cast('<personID="' + cast(personID  as varchar) +'" country="' + country + '"/>' +
        '"<FIELD fieldname="name" value="' + name + '"/>' +
        '<FIELD fieldname="lastname" value="' + lastname + '"/>' +
'</personID>' as xml)
from @tbl;

--..add null columns
INSERT INTO @tbl VALUES
,(null, null, null)
,(null, 'null name', null)
,('null lastname', null, null);

--exclude fieldname for null columns
select
(
select t.personID as '@ID', t.country as '@country',
    case when t.name is not null then 'name' end as 'FIELD/@fieldname', 
    t.name as 'FIELD/@value',
    '',
    case when t.lastname is not null then 'lastname' end as 'FIELD/@fieldname', t.lastname as 'FIELD/@value'
for xml path('person'), type
)   
from @tbl t
where name is not null or lastname is not null or country is not null;

--A
select
(
select t.personID as '@ID', t.country as '@country',
    'name' as 'FIELD/@fieldname', t.name as 'FIELD/@value',
    '',
    'lastname' as 'FIELD/@fieldname', t.lastname as 'FIELD/@value'
for xml path('person'), type
)   
from @tbl t;

--B
select
(
select t.personID as 'person/@ID', t.country as 'person/@country',
    'name' as 'person/FIELD/@fieldname', t.name as 'person/FIELD/@value',
    '' as 'person',
    'lastname' as 'person/FIELD/@fieldname', t.lastname as 'person/FIELD/@value'
for xml path(''), type
)   
from @tbl t;


--single xml for the whole table
select t.personID as '@ID', t.country as '@country',
    'name' as 'FIELD/@fieldname', t.name as 'FIELD/@value',
    '',
    'lastname' as 'FIELD/@fieldname', t.lastname as 'FIELD/@value'
from @tbl t
for xml path('person');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM