1

I'm writing a select statement for a large database whereby I'm copying from one system to another.

Both use the same column names as the element names.

For example

SELECT
    1 'job/queue',
    0 'job/branch',
    'create-apm' 'parameters/yzt/char20.1',
    c.Name 'apmdata/prospect/p.cm/',
    c.Addr1 'apmdata/prospect/p.cm/'
FROM
    dbo.ic_brpolicy AS p
INNER JOIN
    dbo.ic_yyclient AS c ON c.B@ = p.B@
                         AND c.Ref@ = p.Ref@
WHERE
    p.PolRef@ = 'ANCX38PC01'
FOR XML PATH('xmlexecute'), TYPE ;

You can see 'Name' and 'Addr1' both of these are the same names the elements need to be i.e.

c.Name 'apmdata/prospect/p.cm/Name'
c.Addr1 'apmdata/prospect/p.cm/Addr1'

Is there anyway I can get the column title as the output element without using dynamic SQL?

If dynamic SQL is the only way to do it then that's the route I'll have to go down as it'll save lots of time but still isn't ideal from a best practice point of view.

Cheers.

1 Answer 1

1

Managed to get around it by 'Sub Querying' the statement I needed.

SELECT
    1 'job/queue'
  , 0 'job/branch'
  , 'create-apm' 'parameters/yzt/char20.1'
  ,(
        SELECT REPLACE(c.[Addr1],'&','and') 'Address1',
               c.Addr2 'Address2',
               c.Addr3 'Address3',
               c.Addr4 'Address4',
               c.Pcode 'Postcode'
        WHERE c.B@ = p.B@
              AND c.Ref@ = SUBSTRING(p.PolRef@, 1, 6)
        FOR XML PATH('HomeAddress'), TYPE
    ) AS "apmdata/prospect/p.cm"
  --, c.Name 'apmdata/prospect/p.cm/Name'
  --, c.Addr1 'apmdata/prospect/p.cm/Addr1'
FROM
    dbo.ic_brpolicy AS p
INNER JOIN
    dbo.ic_yyclient AS c
        ON c.B@ = p.B@
           AND c.Ref@ = p.Ref@
WHERE
    p.PolRef@ = 'ANCX38PC01'
FOR XML PATH('xmlexecute'), 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.