Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3667122
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:57:44+00:00 2026-05-19T01:57:44+00:00

I have an account table and a campaign table, each account has certain number

  • 0

I have an account table and a campaign table, each account has certain number of campaigns associated with it. No I want to export the account IDs and all the Campaign IDs associated with that account id in to XML in this structure

<Accounts>
            <Account>
                        <AccountID></AccountID>
                        <AccountName></AccountName>
                        <CampaignIDs>
                                    <CampaignID></CampaignID>
                                    <CampaignID></CampaignID>
                        </CampaignIDs>
            </Account>
</Accounts>

I am using XML Explicit to control the output of the data into XML and here is what I have got so far.

SELECT
    1 AS Tag,
    NULL AS Parent,
    NULL AS 'Accounts!1',
    NULL AS 'Account!2!AccountID!Element',
    NULL AS 'Account!2!AccountName!Element',
    NULL AS 'Account!2!FMID!Element'
    UNION ALL
 SELECT
 2 AS Tag,
 1 AS Parent, 
 NULL,
 a.id as AccountID,
 a.Name as AccountName,
 NULL
 from Account a
FOR XML EXPLICIT

Now I want to execute another query like Select id from campaign where accountid = var
and then append all those campaign IDs to the xml structure.

How do I go about this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:57:44+00:00Added an answer on May 19, 2026 at 1:57 am

    I would recommend using FOR XML PATH instead of FOR XML EXPLICIT – it’s much easier to use and much more expressive.

    See this:

    -- set up test data
    declare @Accounts table (AccountID INT, AccountName VARCHAR(50))
    declare @Campaigns table (CampaignID INT, AccountID INT, CampaignName varchar(50))
    
    insert into @Accounts values(1, 'Account #1'),(2, 'Account #2')
    insert into @Campaigns values(1, 1, 'Campaign #1-1'), (2, 1, 'Campaign #2-1'), (3, 1, 'Campaign #3-1'),
    (4, 2, 'Campaign #1-2'), (5, 2, 'Campaign #2-2')
    
    -- SELECT with FOR XML PATH and a nested SELECT/FOR XML PATH,TYPE    
    select 
        AccountID,
        AccountName,
        (SELECT CampaignID  
         FROM @Campaigns c
         WHERE c.AccountID = a.AccountID
         FOR XML PATH(''),TYPE) AS 'CampaignIDs'
    FROM 
        @Accounts a
    FOR XML PATH('Account'),ROOT('Accounts')
    

    This SELECT statement gives me the following output:

    <Accounts>
      <Account>
        <AccountID>1</AccountID>
        <AccountName>Account #1</AccountName>
        <CampaignIDs>
          <CampaignID>1</CampaignID>
          <CampaignID>2</CampaignID>
          <CampaignID>3</CampaignID>
        </CampaignIDs>
      </Account>
      <Account>
        <AccountID>2</AccountID>
        <AccountName>Account #2</AccountName>
        <CampaignIDs>
          <CampaignID>4</CampaignID>
          <CampaignID>5</CampaignID>
        </CampaignIDs>
      </Account>
    </Accounts>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a LIST (query output)that has the distinct ID’s from a table Account.
I have a table that saves some account limits like users. For most rows
i have an account number that is stored in as a query string. How
I have 2 tables - an Account table and a Users table. Each account
I want select multiple (all) values from table Account. string query = SELECT *
We have an Oracle database, and the customer account table has about a million
Suppose I have a table: ACCOUNT It has columns: ID , NAME , BALANCE
I have something like this: create table account ( id int identity(1,1) primary key,
I have a table like this (Oracle, 10) Account Bookdate Amount 1 20080101 100
I have a table with game scores, allowing multiple rows per account id: scores

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.