我有以下问题
select distinct
t.Code as Prime,
t.nom as Alfa,
t.Nom as Name,
t.Adresse as Street,
t.CP as ZipCode,
t.Localite as City,
case t.CodePays when null then 'BE' when '' then 'BE' else t.CodePays end as Country,
Case t.CodeLangue when 'NL' then 1 when 'FR' then 2 when 'EN' then 3 when 'DE' then 4 else 1 end as Language,
'EUR' as CurrencyCode,
Case t.Tva when '' then 0 else 1 end as VATCode,
Case t.Tva when '' then 0 else 1 end as VATStatus,
t.Tva as VATNumber,
case t.CodePays when null then 'BE' when '' then 'BE' else t.CodePays end as CountryVATNumber,
0 as Status /* 0=pas importé*/
from tiers t inner join tiersexport te on t.code=te.code where t.CodeTypeTiers in(1,3)
order by t.Code FOR XML PATH('Customer'), ROOT('Customers');
谁为我生成了合适的 XML
<Customers>
<Customer>
...
</Customer>
</Customers>
我需要有
<?xml version="1.0" encoding="ISO-8859-1"?>
<ImportExpMPlus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Customers>
<Customer>
...
</Customer>
</Customers>
</ImportExpMPlus>
你能告诉我怎么做吗?
我试过了
WITH XMLNAMESPACES( 'http://www.w3.org/2001/XMLSchema' as xsd,'http://www.w3.org/2001/XMLSchema-instance' as xsi)
select distinct
t.Code as Prime,
t.nom as Alfa,
t.Nom as Name,
t.Adresse as Street,
t.CP as ZipCode,
t.Localite as City,
case t.CodePays when null then 'BE' when '' then 'BE' else t.CodePays end as Country,
Case t.CodeLangue when 'NL' then 1 when 'FR' then 2 when 'EN' then 3 when 'DE' then 4 else 1 end as Language,
'EUR' as CurrencyCode,
Case t.Tva when '' then 0 else 1 end as VATCode,
Case t.Tva when '' then 0 else 1 end as VATStatus,
t.Tva as VATNumber,
case t.CodePays when null then 'BE' when '' then 'BE' else t.CodePays end as CountryVATNumber,
0 as Status /* 0=pas importé*/
from tiers t inner join tiersexport te on t.code=te.code where t.CodeTypeTiers in(1,3)
order by t.Code FOR XML PATH('Customer'), ROOT('Customers');
但后来我收到了
<Customers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Customer>
...
</Customer>
</Customers>
通过变量添加额外的元素,像这样:
DECLARE @xml_header NVARCHAR(MAX);
SET @xml_header = N'<?xml version="1.0" encoding="ISO-8859-1"?>\n<ImportExpMPlus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">\n';
DECLARE @xml_footer NVARCHAR(MAX);
SET @xml_footer = N'\n</ImportExpMPlus>';
DECLARE @xml_data NVARCHAR(MAX);
SET @xml_data = (SELECT DISTINCT
t.Code AS Prime
, t.nom AS Alfa
, t.Nom AS Name
, t.Adresse AS Street
, t.CP AS ZipCode
, t.Localite AS City
, CASE t.CodePays WHEN NULL THEN 'BE' WHEN '' THEN 'BE' ELSE t.CodePays END AS Country
, CASE t.CodeLangue WHEN 'NL' THEN 1 WHEN 'FR' THEN 2 WHEN 'EN' THEN 3 WHEN 'DE' THEN 4 ELSE 1 END AS LANGUAGE
, 'EUR' AS CurrencyCode
, CASE t.Tva WHEN '' THEN 0 ELSE 1 END AS VATCode
, CASE t.Tva WHEN '' THEN 0 ELSE 1 END AS VATStatus
, t.Tva AS VATNumber
, CASE t.CodePays WHEN NULL THEN 'BE' WHEN '' THEN 'BE' ELSE t.CodePays END AS CountryVATNumber
, 0 AS STATUS
FROM tiers t
-- INNER JOIN tiersexport te ON t.code=te.code
WHERE t.CodeTypeTiers IN (1, 3)
ORDER BY t.Code
FOR XML PATH('Customer')
, ROOT('Customers')
)
SELECT @xml_header + @xml_data + @xml_footer
注意:我认为连接表没有明显的目的
tiersexport
,也许您也可以摆脱DISTINCT
,因为连接可能是重复行的原因。
看这个 dbfiddle:https://dbfiddle.uk/eNKqdjlY