sql server - TSQL - FOR XML add data to the root node -


i have table:

create table [dbo].[xdothpay] (     accountno char(10) not null,     paymentdoneon datetime null,     paymentdoneby char(30) null,     invoicenumber char(10) null,     amountpaid decimal(12,3) null,     comments char(254) null,     clientid char(50) null,     installid char(50) null,     batchid char(14) not null  ) 

that need export regularly via ssis , sftp.

the receiver of export specifies format:

<?xml version="1.0" encoding="utf-8"?> <payments clientid="10865" xmlns="http://someurl.com/core/xml/payment">     <payment>         <accountno>value</accountno>         <paymentdoneon>value</paymentdoneon>         <paymentdoneby>value</paymentdoneby>         <invoicenumber>value</invoicenumber>         <amountpaid>value</amountpaid>         <comments>value</comments>     </payment> </payments> 

this t-sql statement:

select      accountno,     paymentdoneon,     paymentdoneby,     invoicenumber,     amountpaid,     comments      xdmobothpay xml path ('payment'), root ('payments'), elements 

this results in:

<payments>     <payment>         <accountno> 972140</accountno>         <paymentdoneon>2017-06-08t00:00:00</paymentdoneon>         <paymentdoneby>r entry ba13177 </paymentdoneby>         <invoicenumber> 3804</invoicenumber>         <amountpaid>468.000</amountpaid>         <comments>non-onl payment </comments>     </payment> ... 

which close, need data root element (payments). column in table clientid needs included in root element clientid attribute. clientid consistent on rows table.

i have explored several approaches not getting format required.

thanks torin

i advise not use fixed width char columns, rather use varchar columns. sample adds namespace , xml different need. sample 1 specific clientid, suppose need.

the result has unfortunate thing, namespace applied payment element. if that's not acceptable, there workaround generate without xml namespace, , apply string functions insert namespace attribute.


create table #xdothpay(     accountno char(10) not null,     paymentdoneon datetime null,     paymentdoneby char(30) null,     invoicenumber char(10) null,     amountpaid decimal(12,3) null,     comments char(254) null,     clientid char(50) null,     installid char(50) null,     batchid char(14) not null );  insert #xdothpay(accountno,amountpaid,batchid,clientid,comments,installid,invoicenumber,paymentdoneby,paymentdoneon) values(972140,468,'bath-id',10865,'comments','install-id','invoicenr','paymentdoneby','20170101'),       (972141,468,'bath-id',10865,'comments','install-id','invoicenr','paymentdoneby','20170101');  xmlnamespaces(default 'http://someurl.com/core/xml/payment') select      10865 '@clientid',     (         select              accountno,             paymentdoneon,             paymentdoneby,             invoicenumber,             amountpaid,             comments                      #xdothpay                     i.clientid=10865                      xml path ('payment'), type     )      xml path ('payments'), elements;  drop table #xdothpay; 

result:

<payments xmlns="http://someurl.com/core/xml/payment" clientid="10865">   <payment xmlns="http://someurl.com/core/xml/payment">     <accountno>972140    </accountno>     <paymentdoneon>2017-01-01t00:00:00</paymentdoneon>     <paymentdoneby>paymentdoneby                 </paymentdoneby>     <invoicenumber>invoicenr </invoicenumber>     <amountpaid>468.000</amountpaid>     <comments>comments                                                                                                                                                                                                                                                      </comments>   </payment>   <payment xmlns="http://someurl.com/core/xml/payment">     <accountno>972141    </accountno>     <paymentdoneon>2017-01-01t00:00:00</paymentdoneon>     <paymentdoneby>paymentdoneby                 </paymentdoneby>     <invoicenumber>invoicenr </invoicenumber>     <amountpaid>468.000</amountpaid>     <comments>comments                                                                                                                                                                                                                                                      </comments>   </payment> </payments> 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -