0

I have database with two tables: Transaction and from_person.

I need to convert it to specified format like this:

<transaction>                      
  <transaction_number>TRNWEB0147</transaction_number
  <transaction_location>felesteen</transaction_location>
  <date_transaction>2016-05-25T00:00:00</date_transaction>
  <from_funds_code>C</from_funds_code>
<from_person>                    
  <gender>M</gender>
  <title>Mr.</title>
  <first_name>Mohamed</first_name>
  <middle_name>Mohamed</middle_name>
  <prefix>AHMED</prefix>
  <last_name>yahia</last_name>
  <birth_date>1984-11-16T00:00:00</birth_date>
  <ssn>28411160225124</ssn>
</from_person>
</transaction>

I try to do this query:

select tr.transactionnumber
      ,tr.transaction_location
      ,tr.transaction_description
      ,tr.date_transaction
      ,tr.teller
      ,tr.authorized
      ,tr.transmode_
      ,tr.amount_local
      ,(select fp.from_funds_code
              ,fp.from_country
          from from_person fp
         where fp.from_funds_code = tr.t_from_my_client
           for xml path(''), elements, type)
  from dbo. [ transaction ] tr
   for xml path(''), elements, type

but result become in this format:

  <transaction_number>TRNWEB0147</transaction_number
  <transaction_location>felesteen</transaction_location>
  <date_transaction>2016-05-25T00:00:00</date_transaction>
  <from_funds_code>C</from_funds_code>              
  <gender>M</gender>
  <title>Mr.</title>
  <first_name>Mohamed</first_name>
  <middle_name>Mohamed</middle_name>
  <prefix>AHMED</prefix>
  <last_name>yahia</last_name>
  <birth_date>1984-11-16T00:00:00</birth_date>
  <ssn>28411160225124</ssn>

I tried many ways but not succeed, please help.

4
  • 1
    Have you tried setting for xml path('from_person') in your subquery? Commented Nov 7, 2016 at 13:03
  • no what you mean? need more detail ? Commented Nov 7, 2016 at 13:09
  • 1
    "A Subquery or Inner query or Nested query is a query within another SQL query" (quoted from tutorialspoint.com - SQL - Sub Queries). You have only one. In that subquery you use FOR XML PATH(''). Try FOR XML PATH('from_person'). Commented Nov 7, 2016 at 13:12
  • i know what mean subquery but it's Done Excellent by adding my missed line that you added Commented Nov 7, 2016 at 13:19

1 Answer 1

1

Please use this query.

SELECT transaction_number,transaction_location,date_transaction,from_funds_code,
(
    SELECT gender,title,first_name,middle_name,prefix,last_name,birth_date,ssn
    FROM from_person FP
    WHERE FP.FROM_FUNDS_CODE  =TR.T_FROM_MY_CLIENT 
    FOR XML PATH('from_person'), ELEMENTS, TYPE 
)
FROM dbo.[TRANSACTION] TR
FOR XML PATH('transaction'), ELEMENTS, TYPE
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.