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

  • SEARCH
  • Home
  • 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 7996437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:42:37+00:00 2026-06-04T14:42:37+00:00

In my database ( GoogleSEOData ), I have got one Table (GoogleMarkupList) and below

  • 0

In my database (GoogleSEOData), I have got one Table (GoogleMarkupList) and below sample data are there in table:

PUBLICATION_ID  |   PAGEID  |   URL
-------------------------------------------------------------
233             |   654345  |   /english/index.aspx
345             |   654345  |   /de/english/index.aspx
432             |   654345  |   /ru/russian/index.aspx
533             |   654345  |   /ae/arabic/index.aspx
233             |   452323  |   /english/offers.aspx
345             |   452323  |   /de/english/offers.aspx
432             |   452323  |   /ru/russian/offers.aspx
533             |   452323  |   /ae/arabic/offers.aspx
233             |   834343  |   /english/destinations.aspx
345             |   834343  |   /de/english/destinations.aspx
432             |   834343  |   /ru/russian/destinations.aspx
533             |   834343  |   /ae/arabic/destinations.aspx

Now I want to write SQL Procedure which will take File Path of the server as input say (D://GoogleMarkup) and would create below type of XML files on server (For above sample data).

Name of XML file for 654345 type of data will be 654345.XML

<ps>
<p n="233" u="/english/index.aspx" />
<p n="345" u="/de/english/index.aspx" />
<p n="432" u="/ru/russian/index.aspx" />
<p n="533" u="/ae/arabic/index.aspx" />
</ps>

Name of XML file for 452323 type of data will be 452323.XML

<ps>
<p n="233" u="/english/offers.aspx" />
<p n="345" u="/de/english/offers.aspx" />
<p n="432" u="/ru/russian/offers.aspx" />
<p n="533" u="/ae/arabic/offers.aspx" />
</ps>

Name of XML file for 834343 type of data will be 834343.XML

<ps>
<p n="233" u="/english/destinations.aspx" />
<p n="345" u="/de/english/destinations.aspx" />
<p n="432" u="/ru/russian/destinations.aspx" />
<p n="533" u="/ae/arabic/destinations.aspx" />
</ps>

I know there is utility

SQLCMD -S Server -d Database -E -v pageid=%pid% 
       -Q "Select publication_id as [n], url  from table as [p] where pageid=$(pageid) for xml auto, root('ps')" 
       -o D://GoogleMarkup/%pid%.xml

But how to create all the XMLs in one go, so that it will read pageid one by one and would pass to SQLCMD to create XML, do I need to write cursor for that, please suggest with some good code sample.

Please suggest!!

  • 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-06-04T14:42:38+00:00Added an answer on June 4, 2026 at 2:42 pm

    You can create a SP to cycle and repeat the command to create the XML file

    IMPORTANT:
    The sqlcmd utility requiers :XML ON sqlcmd command on the single line!

    Without :XML ON sqlcmd command the result is like:

    XML_F52E2B61-18A1-11d1-B105-00805F49916B
    ---------------------------------------------------
    0x440249004400440546006C006F00610074004408440061007...
    
    (1 rows affected)
    

    This is a reason why we can’t use the inline sql query to export data to xml using sqlcmd.

    But we can use the bcp utility for inline sql queries.

    CREATE PROCEDURE [dbo].[DO_XML]
    AS 
    
    Declare @PAGEID int
    , @PAGEID_TEXT varchar(100)
    , @CMD varchar(2000)
    , @FILE varchar(100)
    
    -- local cursor to cycle
    DECLARE cXML CURSOR LOCAL FAST_FORWARD FOR
    SELECT  PAGEID from GoogleMarkupList with (NOLOCK) group by PAGEID  
    
    OPEN cXML
    
        FETCH NEXT FROM cXML INTO @PAGEID
        WHILE @@FETCH_STATUS = 0
    
        BEGIN
            set @PAGEID_TEXT = cast(@PAGEID as varchar(100))
    
            -- dynamic command to create XML
            -- MUST BE ALL IN 1 LINE
            set @CMD = 'bcp "Select publication_id as [n], url  from GoogleSEOData.dbo.GoogleMarkupList as [p] where pageid='+ @PAGEID_TEXT +' FOR XML AUTO, ELEMENTS, ROOT(''ps'')"  queryout E:\GoogleMarkup\'+@PAGEID_TEXT+'.XML -S MANOJ-PC\KRISH -T -c'
    
            -- do it
            exec master..xp_cmdshell @CMD, no_output
    
            FETCH NEXT FROM cXML INTO @PAGEID
    
    END    
    CLOSE cXML
    DEALLOCATE cXML
    

    This SP has been tested and work correctly by creating all the XML files

    Add what about the command line ?

    here the example:

    bcp "Select publication_id as [n], url  from GoogleSEOData.dbo.GoogleMarkupList as [p] where pageid=452323 FOR XML AUTO, ELEMENTS, ROOT('ps')" queryout E:\GoogleMarkup\452323.xml -S GoogleSEOData -T -c
    

    For more info:
    SQL Server Export using bcp/sqlcmd Utilities and XML

    Hope it help

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Database newbie here. I'm setting up a mysql table. One of the fields will
database/SQL novice here. I have a table with a column called, for example, EmployeeID.
Database is Oracle. I have a single table with three columns, A, B and
Database : SQL Server 2008 Is there a way to have an insert statement
Database: Sql Server I have table column named page_text which contains following value I
Database Table company_info ----------------------------- | companyname | companytype | ----------------------------- | Company One |
Database:POSTGRESQL I have a table location_type with 2 columns id,name. I want to delete
Database is OracleXE and here is the problem: data gets entered in tables UPS
Database: DB2 v9.5 on AIX Scenario: I have 2 instances- db2inst1 and db2inst2. I
Database setup (MySQL) table: top_fives id, uid, first, second, third, fourth, fifth, creation_date 1,

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.