FOR XML to generate dynamic script to drop multiple tables

Log_Table has a column named Col1 where we have table names
Col1
TableA1
TableA2
TableA3
TableB1
TableC1
TableC2
Generate dynamic script to drop tables that are in Log_Table and starts with TableA.
Instead of using the loop, use For XML

Thanks Visakh16 [+1],
you are right there was typo mistake :-) I forgot the variable in the end, and Thanks Naomi [+1],
this is true and better to use quotename or just add brackets. Actually I will not choose this approach since we chould use schema as well. the DDL in the OP is not something that I will create probably.
In any case, here is the fixed query (current approach):
declare @SQLString NVARCHAR(MAX)
SELECT
@SQLString = (select
N'IF OBJECT_ID (N''' + CAST([Col1] AS VARCHAR(MAX)) + N''') IS NOT NULL drop table [' + CAST([Col1] AS VARCHAR(MAX)) + N']; '
FROM Log_Table
WHERE (Col1 like 'TableA%')
FOR XML PATH (''))
--print @SQLString
EXECUTE sp_executesql @SQLString
GO
  Ronen Ariely
 [Personal Site]    [Blog]    [Facebook]

Similar Messages

  • How to drop multiple tables in one statement

    I could used to run the below syntax to drop multiple tables in MS SQL, but it doesn't work in Oracle seems like, I am using free Oracle SQL Developer. Not a big deal but wanna know if this can be done. Thanks,
    DROP TABLE A, B, C, D, F

    I'm not so sure in 1 select statement is it possible or not but you can try to use in this manner to see what happens ->
    drop table &tab;
    Enter value for tab: A
    Table A Successfully dropped.
    Enter value for tab: B
    Table B Successfully dropped.Regards.
    Satyaki De.

  • How to drop multiple tables in SQL Workshop

    Hi,
    I'd like to drop multiple tables in one time but
    SQL command processor returnes error message: ORA-00933.
    Could you please tell me how to drop multiple tables in
    one time?
    My trial was follows
    Drop table "table1", "table2"
    Result
    ORA-00933: SQL command not properly ended
    Regards,
    Hideki Sakamoto

    Hideki,
    SQL syntax permits you to specify only one table in a DROP TABLE command.
    The SQL Command Processor permits you to run one statement or PL/SQL block per submission.
    So there are at least two ways you can do this. You could either upload or create a SQL script in SQL Workshop which contains your multiple statements:
    drop table "table1"
    drop table "table2"
    or you could execute a single anonymous PL/SQL block in the SQL Command Processor, as in:
    begin
    execute immediate 'drop table "table1"';
    execute immediate 'drop table "table2"';
    end;
    Joel

  • Dropping multiple tables

    how to drop multiple table starting with the word say:FLOW?

    Hi ,
    What about inserting drop table commands in a single file and execute it using SQL*PLUS...?????
    Alternatively , you can see this one....
    Re: drop a list of tables
    Regards,
    Simon

  • Dropping multiple tables in SQL Developer 1.1.25

    HI all
    I am trying to drop multiple tables simultaneously from tha schema but I am unable to find any such option. My host end database in Oracle 9.2.0 and client on Win XP.
    Thanks!
    Ankur

    Hi Ankur,
    Currently that's not possible. It is requested on the SQL Developer Exchange though. If you go vote there, you'll be adding more weight to the feature, so it will be implemented sooner.
    K.

  • NameSpace for XML But in SSIS Script Task

    Hello, 
    I am creating an xml Document that pulls information from a staging table in sql. However the issue I am having is that i need  a namespace or a start element to handle a ':' but i keep getting its not a hexadecimal blah blah. 
    I feel like I have tried everything for this. 
    So maybe someone can help me  
           writer.WriteStartDocument()
     writer.WriteComment("edited with XMLSpy v2010 rel. 3 (http://www.altova.com) by MESMERiZE (MSM)")
    writer.WriteComment("Samply XML file generated by XMLSpy v2010 rel. 3 (http://www.altova.com)")
    So i have the comments show up fine but then i need it to show 
    <mssext:sendWarrantyContractBatch xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mssext="http://jlrint.com/mss/message/sendwarrantycontract/1" xsi:schemaLocation="http://jlrint.com/mss/message/sendwarrantycontract/1
    FRS_000143-SendWarrantyContractMessages.xsd">
     I can get the websites and all the xmlns to show up fine that is not the issue the issue is the mssext:sendWarrantyContractBatch 
    IS there any way to get the Colon to show up ?? 
    Remember this is in a ssis script task with visual basic 2008 code 

    check this link. it has a method specified in the comments section
    http://blogs.lessthandot.com/index.php/datamgmt/dbprogramming/create-xml-files-out-of-sql-server-with/
    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • Generating dynamic command links in data table

    Hi,
    I have a requirement to generate dynamic number of columns in a datatable. Also these columns should contain a command link.
    I have written following piece of code to do so...
    public UIData getDataTableBinding()
         UICommand comm = null;
         UIColumn col;
         UIOutput out = null;
         Application app = app = FacesContext.getCurrentInstance().getApplication();
    // Suppose I want to generate 7 columns
         for(int j = 0; j < 7; ++j) {
         out = new UIOutput();
         col = new UIColumn();
         comm = new UICommand();
         ValueBinding vb = app.createValueBinding("#{" + j + "}");
         out.setValueBinding("value", vb);
         out.setRendererType("javax.faces.Text");
         comm.setRendererType("javax.faces.Link");
    comm.getChildren().add(out);
         MethodBinding mbButton = app.createMethodBinding("#{pc_File1.doLink1Action}",
    null);
         comm.setAction(mbButton);
         col.getChildren().add(comm);
         dataTableTemp.getChildren().add(col);
         return dataTableTemp;
    I have bound the "binding" attribute of the data table to getDataTableBinding()
    method.
    If I remove all the references to UICommand i.e comm variable, table is displayed properly, but if
    I try to include a command link component as mentioned above I get "Assertion failed error"
    SRVE0068E: Could not invoke the service() method on servlet Faces Servlet. Exception thrown :
    javax.servlet.ServletException: javax.faces.el.EvaluationException: Error getting property
    'dataTableBinding' from bean of type pagecode.File1: javax.faces.FacesException: Assertion Failed
         at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:638)
         at com.ibm._jsp._File1._jspService(_File1.java:92)
         at com.ibm.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:88)
    Any kind of help on this issue would be appreciated,
    Thanks in advance,
    Raina

    Hi,
    I solved my problem just by replacing UIOutput component with HtmlOutputText component and then making it as a child component of UICommand component.
    Now I am facing a new problem. Whenever I try to refresh the page I get following exception
    "SRVE0026E: [Servlet Error]-[Faces Servlet]: java.lang.IllegalStateException: Duplicate component ID 'form1:table1:cmd0' found in view."
    I am explicitly setting the component id of the UICommand component using the setViewId() method.
    This problem is observed only when I refresh the page. In case I navigate to another page and then again come back to this current page, then I don't get this exception.
    Your help would be appreciated,
    Raina

  • Drop multiple tables

    Hi
    Can you pls help me on this
    I have 300 tables starting 'ABC_' and I would like to drop all these table starting with this
    drop table select table_name from all_tables where table_name like '%ABC_%'but the above query is not working,pls help me on this
    Thanks a lot

    If you are using Oracle 10 or later, don't forget that you need to use the 'purge' keyword to release the space.
    Personally I don't like doing destructive tasks like this through an execute immediate loop - it is better to see and be sure exactly what is going to be removed.
    My preferred option would be to generate a script and review it before running - along the lines of:
    set verify off trimspool on
    spool drop_script.sql
    select 'spool drop_script.log' from dual;
    select 'set echo on' from dual;
    select 'drop table '||owner||'.'||table_name||' purge;'
    from all_tables where table_name like '%ABC_%' ;
    select 'spool off' from dual;
    spool off

  • Dropping multiple tables having common prefix in SQL Server 2000/2005

    Hi
    I used the below to drop tables having common prefix like 'GPN'
    DECLARE @id varchar(255) DECLARE @dropCommand varchar(255) DECLARE tableCursor CURSOR FOR SELECT name FROM sys.tables WHERE NAME LIKE 'GPN%' OPEN tableCursor FETCH next FROM tableCursor INTO @id WHILE @@fetch_status=0 BEGIN SET @dropcommand = N'drop table ' + @id EXECUTE(@dropcommand) FETCH next FROM tableCursor INTO @id END CLOSE tableCursor DEALLOCATE tableCursor
    The point is this works in SQL2005 but not in SQL2000
    In SQL 2000 we get errors like
    Server: Msg 137, Level 15, State 1, Line 2
    Must declare the variable '@dropcommand'.
    Server: Msg 137, Level 15, State 1, Line 2
    Must declare the variable '@dropcommand'.
    Tried the following as well : SELECT table_name FROM Information_Schema.TABLES WHERE table_name LIKE
    but ended up with the same error.
    Please let me know where i am going wrong and how i can correct it
    Purushothama

    Hi.
    As far as I know SQLS2000 uses syntax:
    EXEC sp_executesql @dropcommand
    Isn't this a problem?
    Talerod

  • To insert the xml data's generated dynamically it to orac very very urgent

    I AM WORKING IN A PROJECT WHICH RECEIVES THE DATA FROM FLAT FILES
    AND CONVERT IT TO XML FILE>
    I am in need to convert the xml file generated dynamically through out the year map it to correct datafield it the database and insert it in to oracle data base using java and jsp, servlets.
    i also need mechanism to be explained detailed and some sample codeings
    of converting the data from database to xml because
    and i also need to convert the data taken from sql server7.0 put it in xml file and once again read parse and insert in to oracle database.
    some sample codeings of the mechanisma and explaninations.
    I am in very very urgent-

    Hello sir,
    Thanks for the guidence.
    I need to insert data's into database columns where i should fetch data from xml file.
    please
    urgent.

  • How to generate dynamic param file

    Hi, Can anyone please tell me how to generate dynamic parameter file from a table. Table has following structure: create table parameter_temp{Folder_name ,Workflow_name,session_name,parameter_name,parameter value} This table stores all the parameters for all the workflows. How can I generate parameter file for specific session or workflow. Thanks,

    Hi All, I have a scenario to implement.Need urgent help. I have relational source which has below fields. ID,Account,AccountType,Balance1,1001,GL,46567.901,1002,SL,56889.971,1003,Equity,45543.9081,1004,GL,89.541,1005,SL,-56889.971,1006,Equity,-45543.9081,1007,SL,-42777.45  Here my first requiremnt is to check if the balance value for entire file is 0 and if balance amount for each AccoutType is 0 , if both condtionn satifies the flow will go , else load will fail. I tried in below approach. SQ >> Expression >> Aggregator  In aggregaor i have first calculated sum(balance) for entire file by group by on ID column in an aggregator , and keep actual data in expression transformation. Then i took an expression to connect actual data with the sum(balance) , since i need to perform further calculation.I tried connecting expression and aggregator , but it is not allowing me to connect the posrts. And if i am using a joiner , then wrong data is loading to target. It is joining each result of aggregator with actual ports in expression.   I am not sure how to handle this scenario, so that we can just calculate sum of entire file and sum on basis of account type , is both sum are 0 then load the target , else fail.

  • Drop table for multiple tables

    Hi All,
    I want to drop multiple tables in one drop statement.
    Can it be possible?
    Thanks in advance!!
    SA

    Hi,
    CASCADE CONSTRAINTS must be taken into account. E.g.,
    set pages 0
    spool c:\windows\temp\drop.sql
    select 'DROP TABLE '||TABLE_NAME || ' CASCADE CONSTRAINTS;' from user_tables WHERE table_name like 'ZTEST%';
    spool off
    @c:\windows\temp\drop.sqlHere is a test case:
    SQL> set echo off
    SQL> create table ztest1 (ff NUMBER PRIMARY KEY);
    Table created
    SQL> insert into ztest1 select 1 from dual;
    1 row inserted
    SQL> create table ztest2 (gg NUMBER, constraint gg_fk foreign key (gg) references ztest1(ff));
    Table created
    SQL> insert into ztest2 select 1 from dual;
    1 row inserted
    SQL> drop table ztest1;
    drop table ztest1
    ORA-02449: unique/primary keys in table referenced by foreign keys
    SQL>
    SQL> set pages 0
    SQL> spool c:\windows\temp\drop.sql
    Started spooling to c:\windows\temp\drop.sql
    SQL> select 'DROP TABLE '||TABLE_NAME || ' CASCADE CONSTRAINTS;' from user_tables WHERE table_name like 'ZTEST%';
    DROP TABLE ZTEST1 CASCADE CONSTRAINTS;
    DROP TABLE ZTEST2 CASCADE CONSTRAINTS;
    SQL> spool off
    Stopped spooling to c:\windows\temp\drop.sql
    SQL> @c:\windows\temp\drop.sql
    SQL> DROP TABLE ZTEST1 CASCADE CONSTRAINTS;
    Table dropped
    SQL> DROP TABLE ZTEST2 CASCADE CONSTRAINTS;
    Table dropped
    SQL>

  • XML into multiple tables

    This is my first thread so I hope I have formatted my request appropriately...
    If I have a simple XML doc with several records (see below) what is the best / easiest way to insert the data into multiple tables (Oracle 10g Release 1)?
    For example 3 tables:
    PATIENT table (CLAIMID, FNAME, LNAME, HEALTHNUM)
    ADDRESS table (CLAIMID, STREET, CITY, PROVINCE)
    SERVICE table (CLAIMID, SERVICE, ADMITDATE, DEPARTDATE)
    <?xml version="1.0"?>
    <CLAIMS>
    <CLAIM>
    <CLAIMID>1</CLAIMID>
    <FNAME>Oscar</FNAME>
    <LNAME>Grouch</LNAME>
    <HEALTHNUM>1234657890</HEALTHNUM>
    <STREET>123 Sesame Street</STREET>
    <CITY>Sesameville</CITY>
    <PROVINCE>ON</PROVINCE>
    <SERVICE>Heart Surgery</SERVICE>
    <ADMITDATE>2007-06-05</ADMITDATE>
    <DEPARTDATE>2007-06-11</DEPARTDATE>
    </CLAIM>
    <CLAIM>
    <CLAIMID>2</CLAIMID>
    <FNAME>Poppa</FNAME>
    <LNAME>Smurf</LNAME>
    <HEALTHNUM>0987654321</HEALTHNUM>
    <STREET>44 Blue Street</STREET>
    <CITY>Smurfville</CITY>
    <PROVINCE>ON</PROVINCE>
    <SERVICE>Lung Transplant</SERVICE>
    <ADMITDATE>2007-05-28</ADMITDATE>
    <DEPARTDATE>2007-06-04</DEPARTDATE>
    </CLAIM>
    ...more <CLAIM> records
    </CLAIMS>
    I can import the XML document into an XMLType column but don't know where to go from there...
    The extractValue method seems to work only when there is a single claim record in the XML document but a typical document will contain several hundred claims.
    Thanks in advance,
    M Kent

    Use the Oracle Berkeley DB XML to store the XML document in a embedded xml database. To store in the Oracle 10g database use XSU. With XSU an XML document may also be stored in multiple tables.
    http://download-east.oracle.com/docs/cd/A97329_03/web.902/a88894/adx07xsu.htm#1016732

  • Help generate dynamic table in xslt for apache FOP

    down vote favorite
    For the following xml file, i need to generate an xsl-fo file to be converted into pdf.
    I am new to style sheets and struggling to create dynamic table. Please help.
    Also, the width for each column varies,based on the column.How would i include this into the code?
    The Column Headers and Column Values are dynamically populated in the xml file. Below is a sample.
    Can anybody please help in generating xsl-fo or xslt code?
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <ReportData>
    <ColumnHeaders>
    <ColumnHeader>
    <Name>Col-Header1</Name>
    <Width>5</Length>
    </ColumnHeader>
    <ColumnHeader>
    <Name>Col-Header2</Name>
    <Width>10</Length>
    </ColumnHeader>
    <ColumnHeader>
    <Name>Col-Header3</Name>
    <Width>8</Length>
    </ColumnHeader>
    </ColumnHeaders>
    <Rows>
    <Row>
    <Column>Row1-Col1</Column>
    <Column>Row1-Col2</Column>
    <Column>Row1-Col3</Column>
    </Row>
    <Row>
    <Column>Row2-Col1</Column>
    <Column>Row2-Col2</Column>
    <Column>Row2-Col3</Column>
    </Row>
    </Rows>
    </ReportData>

    I did some xsl-fo a few years ago for a project.
    It was an invoice generator spawning multiple PDFs out of single XML document generated from the database. Pretty cool stuff but very resource-consuming.
    I'm a bit rusty but here's a basic example for a single table, based on your sample data :
    XSLT stylesheet :
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <xsl:output indent="yes"/>
      <xsl:template match="/ReportData">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
          <fo:layout-master-set>
            <fo:simple-page-master master-name="Main" margin="8mm"
              page-width="250mm"
              page-height="100mm">
              <fo:region-body />
            </fo:simple-page-master>
          </fo:layout-master-set>
          <fo:page-sequence master-reference="Main">
            <fo:flow flow-name="xsl-region-body">
              <fo:block>
                <fo:table table-layout="fixed" border-style="solid">
                  <xsl:apply-templates select="ColumnHeaders/ColumnHeader" mode="column"/>
                  <xsl:apply-templates select="ColumnHeaders"/>
                  <fo:table-body>
                       <xsl:apply-templates select="Rows/Row"/>
                  </fo:table-body>
                </fo:table>     
              </fo:block>
            </fo:flow>
          </fo:page-sequence>
        </fo:root>
      </xsl:template>
      <xsl:template match="ColumnHeaders">
        <fo:table-header>
             <fo:table-row background-color="rgb(200,200,200)">
                  <xsl:apply-templates select="ColumnHeader" mode="header"/>
             </fo:table-row>
        </fo:table-header>
      </xsl:template>
      <xsl:template match="ColumnHeader" mode="column">
        <fo:table-column column-width="{concat(Width*10,'mm')}"/>
      </xsl:template>
      <xsl:template match="ColumnHeader" mode="header">
        <fo:table-cell border-style="solid">
          <fo:block font-weight="bold"><xsl:value-of select="Name"/></fo:block>
        </fo:table-cell>
      </xsl:template>
      <xsl:template match="Row">
           <fo:table-row>
          <xsl:apply-templates select="Column"/>
        </fo:table-row>
      </xsl:template>
      <xsl:template match="Column">
        <fo:table-cell border-style="solid">
          <fo:block><xsl:value-of select="."/></fo:block>
        </fo:table-cell>
      </xsl:template>
    </xsl:stylesheet>
    {code}
    Calling the XSLT engine and FOP :
    {code}
    D:\XSLT>gen_pdf_otn.bat
    D:\XSLT>java -jar saxon9he.jar -s:in/reportdata.xml -xsl:testfo.xsl -o:out/testfo.xml  2>errors.log
    D:\XSLT>java -jar fop.jar org.apache.fop.cli.Main -fo out/testfo.xml -pdf out/testfo.pdf -c morefontsconfig.xml  2>errors.log
    D:\XSLT>pause
    Appuyez sur une touche pour continuer...
    {code}
    Ouput :
    - The intermediate XSL-FO document :
    {code:xml}
    <?xml version="1.0" encoding="UTF-8"?>
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
       <fo:layout-master-set>
          <fo:simple-page-master master-name="Main" margin="8mm" page-width="250mm" page-height="100mm">
             <fo:region-body/>
          </fo:simple-page-master>
       </fo:layout-master-set>
       <fo:page-sequence master-reference="Main">
          <fo:flow flow-name="xsl-region-body">
             <fo:block>
                <fo:table table-layout="fixed" border-style="solid">
                   <fo:table-column column-width="50mm"/>
                   <fo:table-column column-width="100mm"/>
                   <fo:table-column column-width="80mm"/>
                   <fo:table-header>
                      <fo:table-row background-color="rgb(200,200,200)">
                         <fo:table-cell border-style="solid">
                            <fo:block font-weight="bold">Col-Header1</fo:block>
                         </fo:table-cell>
                         <fo:table-cell border-style="solid">
                            <fo:block font-weight="bold">Col-Header2</fo:block>
                         </fo:table-cell>
                         <fo:table-cell border-style="solid">
                            <fo:block font-weight="bold">Col-Header3</fo:block>
                         </fo:table-cell>
                      </fo:table-row>
                   </fo:table-header>
                   <fo:table-body>
                      <fo:table-row>
                         <fo:table-cell border-style="solid">
                            <fo:block>Row1-Col1</fo:block>
                         </fo:table-cell>
                         <fo:table-cell border-style="solid">
                            <fo:block>Row1-Col2</fo:block>
                         </fo:table-cell>
                         <fo:table-cell border-style="solid">
                            <fo:block>Row1-Col3</fo:block>
                         </fo:table-cell>
                      </fo:table-row>
                      <fo:table-row>
                         <fo:table-cell border-style="solid">
                            <fo:block>Row2-Col1</fo:block>
                         </fo:table-cell>
                         <fo:table-cell border-style="solid">
                            <fo:block>Row2-Col2</fo:block>
                         </fo:table-cell>
                         <fo:table-cell border-style="solid">
                            <fo:block>Row2-Col3</fo:block>
                         </fo:table-cell>
                      </fo:table-row>
                   </fo:table-body>
                </fo:table>
             </fo:block>
          </fo:flow>
       </fo:page-sequence>
    </fo:root>
    {code}
    - and the PDF document :
    http://mbperso.pagesperso-orange.fr/oracle/testfo.pdf                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Generate xml using FOR XML PATH from table with hierarchy

    I need to create xml from a table like:
    EL1 EL2 EL3 Attr01 Attr02 Attr03 Attr04
    E10,    ,    ,a,b,c,d
    E10,E1010,    ,a,b,c,d
    E10,E1010,E101010,a,b,c,d
    E10,E1010,E101020,a,b,c,d
    E10,E1010,E101030,a,b,c,d
    E10,E1020,    ,a,b,c,d
    E10,E1020,E102010,a,b,c,d
    E20,    ,    ,a,b,c,d
    E20,E2010,    ,a,b,c,d
    E20,E2010,E201010,a,b,c,d
    E20,E2020,    ,a,b,c,d
    E20,E2020,E202010,a,b,c,d
    E20,E2020,E202020,a,b,c,d
    The hierarchy is EL1--EL2--EL3, and the 3 columns should be elements of xml;
    The other for columns Attr01,Attr02,Attr03,Attr04 should be attributes of xml;
    The actual table could have more than 500 rows(there are many values for El1,EL2,and EL3). 
    The expected xml should like:
    <root>
      <E10 Attr01="a" Attr02="b" Attr03="c" Attr04="d">
        <E1010 Attr01="a" Attr02="b" Attr03="c" Attr04="d">
          <E101010 Attr01="a" Attr02="b" Attr03="c" Attr04="d" />
          <E101020 Attr01="a" Attr02="b" Attr03="c" Attr04="d" />
          <E101030 Attr01="a" Attr02="b" Attr03="c" Attr04="d" />
        </E1010>
        <E1020 Attr01="a" Attr02="b" Attr03="c" Attr04="d">
          <E102010 Attr01="a" Attr02="b" Attr03="c" Attr04="d" />
        </E1020>
      </E10>
      <E20 Attr01="a" Attr02="b" Attr03="c" Attr04="d">
        <E2010 Attr01="a" Attr02="b" Attr03="c" Attr04="d">
          <E201010 Attr01="a" Attr02="b" Attr03="c" Attr04="d" />
        </E2010>
        <E2020 Attr01="a" Attr02="b" Attr03="c" Attr04="d">
          <E202010 Attr01="a" Attr02="b" Attr03="c" Attr04="d" />
          <E202020 Attr01="a" Attr02="b" Attr03="c" Attr04="d" />
        </E2020>
      </E20>
    </root>
    I create a sample Src table:
    CREATE TABLE Src
    EL1 VARCHAR(10),
    EL2 VARCHAR(10),
    EL3 VARCHAR(10),
    Attr01 VARCHAR(10),
    Attr02 VARCHAR(10),
    Attr03 VARCHAR(10),
    Attr04 VARCHAR(10)
    GO
    INSERT INTO Src
    (EL1,EL2,EL3,Attr01,Attr02,Attr03,Attr04
     SELECT 'E10','','','a','b','c','d'
     UNION SELECT 'E10','E1010','','a','b','c','d'
     UNION SELECT 'E10','E1010','E101010','a','b','c','d'
     UNION SELECT 'E10','E1010','E101020','a','b','c','d'
     UNION SELECT 'E10','E1010','E101030','a','b','c','d'
     UNION SELECT 'E10','E1020','','a','b','c','d'
     UNION SELECT 'E10','E1020','E102010','a','b','c','d'
     UNION SELECT 'E20','','','a','b','c','d'
     UNION SELECT 'E20','E2010','','a','b','c','d'
     UNION SELECT 'E20','E2010','E201010','a','b','c','d'
     UNION SELECT 'E20','E2020','','a','b','c','d'
     UNION SELECT 'E20','E2020','E202010','a','b','c','d'
     UNION SELECT 'E20','E2020','E202020','a','b','c','d'
    GO
    I tried to use FOR XML PATH to generate xml for the sample data. When the records increase to a few hundreds, it's not a good idea.
    Here is my script:
    SELECT
    (SELECT Attr01 AS '@Attr01'
    ,Attr02 AS '@Attr02'
    ,Attr03 AS '@Attr03'
    ,Attr04 AS '@Attr04'
    ,( SELECT
    Attr01 AS '@Attr01'
    ,Attr02 AS '@Attr02'
    ,Attr03 AS '@Attr03'
    ,Attr04 AS '@Attr04'
    ,( SELECT
    Attr01 AS '@Attr01'
    ,Attr02 AS '@Attr02'
    ,Attr03 AS '@Attr03'
    ,Attr04 AS '@Attr04'
    FROM Src
    WHERE EL3 = 'E101010'
    FOR XML PATH('E101010'),TYPE
    ) AS 'node()'
    ,( SELECT
    Attr01 AS '@Attr01'
    ,Attr02 AS '@Attr02'
    ,Attr03 AS '@Attr03'
    ,Attr04 AS '@Attr04'
    FROM Src
    WHERE EL3 = 'E101020'
    FOR XML PATH('E101020'),TYPE
    ) AS 'node()'
    ,( SELECT
    Attr01 AS '@Attr01'
    ,Attr02 AS '@Attr02'
    ,Attr03 AS '@Attr03'
    ,Attr04 AS '@Attr04'
    FROM Src
    WHERE EL3 = 'E101030'
    FOR XML PATH('E101030'),TYPE
    ) AS 'node()'
    FROM Src
    WHERE EL2 = 'E1010' AND (EL1 <>'' AND EL3 ='')
    FOR XML PATH('E1010'),TYPE
    ) AS 'node()'--1010
    ,( SELECT
    Attr01 AS '@Attr01'
    ,Attr02 AS '@Attr02'
    ,Attr03 AS '@Attr03'
    ,Attr04 AS '@Attr04'
    ,( SELECT
    Attr01 AS '@Attr01'
    ,Attr02 AS '@Attr02'
    ,Attr03 AS '@Attr03'
    ,Attr04 AS '@Attr04'
    FROM Src
    WHERE EL3 = 'E102010'
    FOR XML PATH('E102010'),TYPE
    ) AS 'node()'
    FROM Src
    WHERE EL2 = 'E1020' AND (EL1 <>'' AND EL3 ='')
    FOR XML PATH('E1020'),TYPE
    ) AS 'node()'--1020
    FROM Src
    WHERE EL1 = 'E10' AND (EL2 ='' AND EL3 ='')
    FOR XML PATH('E10'),TYPE) 'node()'
    ,(SELECT Attr01 AS '@Attr01'
    ,Attr02 AS '@Attr02'
    ,Attr03 AS '@Attr03'
    ,Attr04 AS '@Attr04'
    ,( SELECT
    Attr01 AS '@Attr01'
    ,Attr02 AS '@Attr02'
    ,Attr03 AS '@Attr03'
    ,Attr04 AS '@Attr04'
    ,( SELECT
    Attr01 AS '@Attr01'
    ,Attr02 AS '@Attr02'
    ,Attr03 AS '@Attr03'
    ,Attr04 AS '@Attr04'
    FROM Src
    WHERE EL3 = 'E201010'
    FOR XML PATH('E201010'),TYPE
    ) AS 'node()'
    FROM Src
    WHERE EL2 = 'E2010' AND (EL1 <>'' AND EL3 ='')
    FOR XML PATH('E2010'),TYPE
    ) AS 'node()'--2010
    ,( SELECT
    Attr01 AS '@Attr01'
    ,Attr02 AS '@Attr02'
    ,Attr03 AS '@Attr03'
    ,Attr04 AS '@Attr04'
    ,( SELECT
    Attr01 AS '@Attr01'
    ,Attr02 AS '@Attr02'
    ,Attr03 AS '@Attr03'
    ,Attr04 AS '@Attr04'
    FROM Src
    WHERE EL3 = 'E202010'
    FOR XML PATH('E202010'),TYPE
    ) AS 'node()'
    ,( SELECT
    Attr01 AS '@Attr01'
    ,Attr02 AS '@Attr02'
    ,Attr03 AS '@Attr03'
    ,Attr04 AS '@Attr04'
    FROM Src
    WHERE EL3 = 'E202020'
    FOR XML PATH('E202020'),TYPE
    ) AS 'node()'
    FROM Src
    WHERE EL2 = 'E2020' AND (EL1 <>'' AND EL3 ='')
    FOR XML PATH('E2020'),TYPE
    FROM Src
    WHERE EL1 = 'E20' AND (EL2 ='' AND EL3 ='')
    FOR XML PATH('E20'),TYPE) AS 'node()'
    FOR XML PATH(''),ROOT('root')
    If I get a few hundreds of rows, how huge the script should be. Does anyone have better solution for this? Thanks.
    Tao

    wBob,
    Thanks! And sorry for late feedback.
    The XSD requires the xml structures like the following
    <Schools>
    <School01>Some school</School01>
    <School02>Some other school</School02>
    </Schools>
    I have to use the number in the element name. 
    Right now I just use the nested FOR XML PATH, although I have to write thousand lines code.
    Thanks anyway.
    Tao
    Tao

Maybe you are looking for

  • Why does Print Preview of a .eml file recursively open the same .eml file? I can't print it.

    Hello. I saved an email from my IMAP account locally to file as offlineemail.eml. When I try to open the .eml file later in Thunderbird, it opens up ok, no problem. When I do Print Preview just before sending it to my printer, two windows pop up. In

  • Running Adobe premiere in samung rv 520

    Hello all I am wondering if I can run and install Adobe Premiere Elements on a Samsung RV 520 ? heres all the spec thanks ! Christian Specifications: Colour: Metallic Silver Operating System Genuine Windows 7 Home Premium (64 bit) Processor Intel Cor

  • PS CC 2014: Custom Workspaces Not Saving Palette Positions

    I generally have two Custom Workspaces: one for when I am working on a single monitor, and one when working with dual monitors. Unfortunately, when I create the second Custom Workspace, it saves the palette position to both of the workspaces. If I tr

  • User Mode in ARD

    I am trying to figure out how to limit the access of ARD on an admin user mac account. I have a user that logs into his computer as an admin user. He needs access to only a few other computers on the network (to access applications - ie. download ema

  • SERIAL NUMBERS IN PRODUCTION ORDERS

    Hi All ,        I am entering the serial number manually at the time of Production Order Creation . For Order1 i have entered the serial number as ABC1  and for the next order i enter the same serial number it is allowing . so my requirements is syst