Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Hi everyone i am a new comer for this forum ,I have a small problem with format the xml to a table in SQL SERVER stored procedure [modified]

Hi everyone i am a new comer for this forum ,I have a small problem with format the xml to a table in SQL SERVER stored procedure [modified]

Scheduled Pinned Locked Moved Database
databasehelpsql-serversysadminxml
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Thabo Codeproject
    wrote on last edited by
    #1

    This is the senario, i am trying to copy a table and store that table into a XML variable(Not physically xml file) and read that XML variable ,at the same time also trying to retrive and insert all data to another table.The stored procedure is given below

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    GO
    -- =============================================
    -- Author: Thabo
    -- Create date: 18.02.2011
    -- Description: TEST XML
    -- =============================================
    CREATE PROCEDURE sppassingStoredProcedue
    DECLARE @x XML
    AS
    BEGIN

    SET NOCOUNT ON;
    SET @x=(SELECT PramryKeyForThisTable,AutoNumberColumn,Data1,Data2,Data3 FROM tblInsertTableForAutoNumber FOR XML AUTO ,TYPE ,ELEMENTS XSINIL,ROOT ('TABLEROOT'))

    INSERT INTO CopyOftblInserTableForAutonumber
    SELECT
    tab.col.value('@PrimaryKeyForThisTable[1]','SMALLINT') AS PramryKeyForThisTable,
    tab.col.value('@AutoNumberColumn[1]','SMALLINT')AS AutoNumberColumn,
    tab.col.value('@Data1[1]','VARCHAR(50)') AS Data1,
    tab.col.value('@Data2[1]','VARCHAR(50)') AS Data2,
    tab.col.value('@Data3[1]','VARCHAR(50)') AS Data3
    FROM @x.nodes('//TABLEROOT/tblInsertTableForAutoNumber')AS tab(Col)

    END

    Actually the second table is the copy of the first table.It has same stucure of the first table The table structure is, colum name datatype ----------- -------- PramryKeyForThisTable smallint AutoNumberColumn smallint Data1 varchar(50) Data2 varchar(50) Data3 varchar(50) I have got different different error messages every time.i hve got this error message for above cording.

    Msg 156, Level 15, State 1, Procedure sppassingStoredProcedue, Line 7
    Incorrect syntax near the keyword 'DECLARE'.
    Msg 156, Level 15, State 1, Procedure sppassingStoredProcedue, Line 8
    Incorrect syntax near the keyword 'AS'."

    I couldnt figure out how to do this. Can any one pls give a good solution for this. Thabo

    -- Modified Friday, February 18, 2011 11:33 AM

    W L 2 Replies Last reply
    0
    • T Thabo Codeproject

      This is the senario, i am trying to copy a table and store that table into a XML variable(Not physically xml file) and read that XML variable ,at the same time also trying to retrive and insert all data to another table.The stored procedure is given below

      set ANSI_NULLS ON
      set QUOTED_IDENTIFIER ON
      GO
      -- =============================================
      -- Author: Thabo
      -- Create date: 18.02.2011
      -- Description: TEST XML
      -- =============================================
      CREATE PROCEDURE sppassingStoredProcedue
      DECLARE @x XML
      AS
      BEGIN

      SET NOCOUNT ON;
      SET @x=(SELECT PramryKeyForThisTable,AutoNumberColumn,Data1,Data2,Data3 FROM tblInsertTableForAutoNumber FOR XML AUTO ,TYPE ,ELEMENTS XSINIL,ROOT ('TABLEROOT'))

      INSERT INTO CopyOftblInserTableForAutonumber
      SELECT
      tab.col.value('@PrimaryKeyForThisTable[1]','SMALLINT') AS PramryKeyForThisTable,
      tab.col.value('@AutoNumberColumn[1]','SMALLINT')AS AutoNumberColumn,
      tab.col.value('@Data1[1]','VARCHAR(50)') AS Data1,
      tab.col.value('@Data2[1]','VARCHAR(50)') AS Data2,
      tab.col.value('@Data3[1]','VARCHAR(50)') AS Data3
      FROM @x.nodes('//TABLEROOT/tblInsertTableForAutoNumber')AS tab(Col)

      END

      Actually the second table is the copy of the first table.It has same stucure of the first table The table structure is, colum name datatype ----------- -------- PramryKeyForThisTable smallint AutoNumberColumn smallint Data1 varchar(50) Data2 varchar(50) Data3 varchar(50) I have got different different error messages every time.i hve got this error message for above cording.

      Msg 156, Level 15, State 1, Procedure sppassingStoredProcedue, Line 7
      Incorrect syntax near the keyword 'DECLARE'.
      Msg 156, Level 15, State 1, Procedure sppassingStoredProcedue, Line 8
      Incorrect syntax near the keyword 'AS'."

      I couldnt figure out how to do this. Can any one pls give a good solution for this. Thabo

      -- Modified Friday, February 18, 2011 11:33 AM

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      The first thing you should do is to change the place of the declaration of @X. If it's a parameter then:

      CREATE PROCEDURE sppassingStoredProcedue @x XML
      AS
      BEGIN
      ...

      and if it's just a variable inside the procedure:

      CREATE PROCEDURE sppassingStoredProcedue
      AS
      DECLARE @x XML
      BEGIN
      ...

      The need to optimize rises from a bad design.My articles[^]

      T 1 Reply Last reply
      0
      • T Thabo Codeproject

        This is the senario, i am trying to copy a table and store that table into a XML variable(Not physically xml file) and read that XML variable ,at the same time also trying to retrive and insert all data to another table.The stored procedure is given below

        set ANSI_NULLS ON
        set QUOTED_IDENTIFIER ON
        GO
        -- =============================================
        -- Author: Thabo
        -- Create date: 18.02.2011
        -- Description: TEST XML
        -- =============================================
        CREATE PROCEDURE sppassingStoredProcedue
        DECLARE @x XML
        AS
        BEGIN

        SET NOCOUNT ON;
        SET @x=(SELECT PramryKeyForThisTable,AutoNumberColumn,Data1,Data2,Data3 FROM tblInsertTableForAutoNumber FOR XML AUTO ,TYPE ,ELEMENTS XSINIL,ROOT ('TABLEROOT'))

        INSERT INTO CopyOftblInserTableForAutonumber
        SELECT
        tab.col.value('@PrimaryKeyForThisTable[1]','SMALLINT') AS PramryKeyForThisTable,
        tab.col.value('@AutoNumberColumn[1]','SMALLINT')AS AutoNumberColumn,
        tab.col.value('@Data1[1]','VARCHAR(50)') AS Data1,
        tab.col.value('@Data2[1]','VARCHAR(50)') AS Data2,
        tab.col.value('@Data3[1]','VARCHAR(50)') AS Data3
        FROM @x.nodes('//TABLEROOT/tblInsertTableForAutoNumber')AS tab(Col)

        END

        Actually the second table is the copy of the first table.It has same stucure of the first table The table structure is, colum name datatype ----------- -------- PramryKeyForThisTable smallint AutoNumberColumn smallint Data1 varchar(50) Data2 varchar(50) Data3 varchar(50) I have got different different error messages every time.i hve got this error message for above cording.

        Msg 156, Level 15, State 1, Procedure sppassingStoredProcedue, Line 7
        Incorrect syntax near the keyword 'DECLARE'.
        Msg 156, Level 15, State 1, Procedure sppassingStoredProcedue, Line 8
        Incorrect syntax near the keyword 'AS'."

        I couldnt figure out how to do this. Can any one pls give a good solution for this. Thabo

        -- Modified Friday, February 18, 2011 11:33 AM

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        and the very first thing to do when posting code is using PRE tags, as Mika did. You can still edit your message if you want... :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        T 1 Reply Last reply
        0
        • L Luc Pattyn

          and the very first thing to do when posting code is using PRE tags, as Mika did. You can still edit your message if you want... :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          T Offline
          T Offline
          Thabo Codeproject
          wrote on last edited by
          #4

          Thank u Luc Pattyn i edit thos within Pre tag :) Thabo

          L 1 Reply Last reply
          0
          • T Thabo Codeproject

            Thank u Luc Pattyn i edit thos within Pre tag :) Thabo

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            much better! :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            1 Reply Last reply
            0
            • W Wendelius

              The first thing you should do is to change the place of the declaration of @X. If it's a parameter then:

              CREATE PROCEDURE sppassingStoredProcedue @x XML
              AS
              BEGIN
              ...

              and if it's just a variable inside the procedure:

              CREATE PROCEDURE sppassingStoredProcedue
              AS
              DECLARE @x XML
              BEGIN
              ...

              The need to optimize rises from a bad design.My articles[^]

              T Offline
              T Offline
              Thabo Codeproject
              wrote on last edited by
              #6

              Thanks...I didnt find that when i read my code :-D Now it was compiling succesfully, but when i execute sp all the colums values of copy table are given null. The first table have 19 rows but with data.The copy table also have 19 rows but without data i mean all are NULL! Is their any thing error in logic ? The thing is ,i dind work with XML in SQL SEERVER before. :sigh: Thabo

              W 1 Reply Last reply
              0
              • T Thabo Codeproject

                Thanks...I didnt find that when i read my code :-D Now it was compiling succesfully, but when i execute sp all the colums values of copy table are given null. The first table have 19 rows but with data.The copy table also have 19 rows but without data i mean all are NULL! Is their any thing error in logic ? The thing is ,i dind work with XML in SQL SEERVER before. :sigh: Thabo

                W Offline
                W Offline
                Wendelius
                wrote on last edited by
                #7

                No problem, You could try to run the operations in smaller parts in order to find out the problem. For example you could run the following without creating a stored procedure:

                DECLARE @x XML
                begin
                SET @x=(SELECT PramryKeyForThisTable,AutoNumberColumn,Data1,Data2,Data3
                FROM tblInsertTableForAutoNumber
                FOR XML AUTO ,TYPE ,ELEMENTS XSINIL,ROOT ('TABLEROOT'))

                print convert(varchar(max), @x)

                SELECT tab.col.value('@PrimaryKeyForThisTable[1]','SMALLINT') AS PramryKeyForThisTable,
                tab.col.value('@AutoNumberColumn[1]','SMALLINT')AS AutoNumberColumn,
                tab.col.value('@Data1[1]','VARCHAR(50)') AS Data1,
                tab.col.value('@Data2[1]','VARCHAR(50)') AS Data2,
                tab.col.value('@Data3[1]','VARCHAR(50)') AS Data3
                FROM @x.nodes('//TABLEROOT/tblInsertTableForAutoNumber')AS tab(Col)
                end

                Hopefully that first prints the contents of @x and then show you all the data that's selected from the nodes query. You could try to modify the columns in the select statement. If you test something like:

                SELECT tab.col.value('(PrimaryKeyForThisTable/text())[1]','SMALLINT') AS PramryKeyForThisTable, ...

                The need to optimize rises from a bad design.My articles[^]

                T 1 Reply Last reply
                0
                • W Wendelius

                  No problem, You could try to run the operations in smaller parts in order to find out the problem. For example you could run the following without creating a stored procedure:

                  DECLARE @x XML
                  begin
                  SET @x=(SELECT PramryKeyForThisTable,AutoNumberColumn,Data1,Data2,Data3
                  FROM tblInsertTableForAutoNumber
                  FOR XML AUTO ,TYPE ,ELEMENTS XSINIL,ROOT ('TABLEROOT'))

                  print convert(varchar(max), @x)

                  SELECT tab.col.value('@PrimaryKeyForThisTable[1]','SMALLINT') AS PramryKeyForThisTable,
                  tab.col.value('@AutoNumberColumn[1]','SMALLINT')AS AutoNumberColumn,
                  tab.col.value('@Data1[1]','VARCHAR(50)') AS Data1,
                  tab.col.value('@Data2[1]','VARCHAR(50)') AS Data2,
                  tab.col.value('@Data3[1]','VARCHAR(50)') AS Data3
                  FROM @x.nodes('//TABLEROOT/tblInsertTableForAutoNumber')AS tab(Col)
                  end

                  Hopefully that first prints the contents of @x and then show you all the data that's selected from the nodes query. You could try to modify the columns in the select statement. If you test something like:

                  SELECT tab.col.value('(PrimaryKeyForThisTable/text())[1]','SMALLINT') AS PramryKeyForThisTable, ...

                  The need to optimize rises from a bad design.My articles[^]

                  T Offline
                  T Offline
                  Thabo Codeproject
                  wrote on last edited by
                  #8

                  Hi thanks,,, i tried based on your advice.I just chnaged my code little bit. like this

                  set ANSI_NULLS ON
                  set QUOTED_IDENTIFIER ON
                  GO
                  -- =============================================
                  -- Author: Thabo
                  -- Create date: 18.02.2011
                  -- Description: TEST XML
                  -- =============================================
                  ALTER PROCEDURE [dbo].[sppassingStoredProcedue]
                  AS
                  DECLARE @x XML
                  BEGIN

                  SET NOCOUNT ON;
                  SET @x=(SELECT PramryKeyForThisTable,AutoNumberColumn,Data1,Data2,Data3 FROM tblInsertTableForAutoNumber FOR XML AUTO,ROOT('MYROOT'))
                  

                  INSERT INTO CopyOftblInserTableForAutonumber
                  SELECT
                  R.Tab.value('@PrimaryKeyForThisTable[1]','SMALLINT)') AS PramryKeyForThisTable,
                  R.Tab.value('@AutoNumberColumntext[1]','SMALLINT')AS AutoNumberColumn,
                  R.Tab.value('@Data1 [1]','VARCHAR(50)') AS Data1,
                  R.Tab.value('@Data2 [1]','VARCHAR(50)') AS Data2,
                  R.Tab.value('@Data3 [1]','VARCHAR(50)') AS Data3
                  FROM @x.nodes('//MYROOT/tblInsertTableForAutoNumber')AS R(Tab)

                  END

                  i also print the @x variable.I got the correct XML with all data.But when i go to the XQuery also i got all the row and values except PrimaryKeyForThisTable, AutoNumberColumntext column values.i got null values for those columns.I thing there might be a dataconversion problem.I tried the text() function .But nothing happened.How i get those values?

                  Thabo

                  W 1 Reply Last reply
                  0
                  • T Thabo Codeproject

                    Hi thanks,,, i tried based on your advice.I just chnaged my code little bit. like this

                    set ANSI_NULLS ON
                    set QUOTED_IDENTIFIER ON
                    GO
                    -- =============================================
                    -- Author: Thabo
                    -- Create date: 18.02.2011
                    -- Description: TEST XML
                    -- =============================================
                    ALTER PROCEDURE [dbo].[sppassingStoredProcedue]
                    AS
                    DECLARE @x XML
                    BEGIN

                    SET NOCOUNT ON;
                    SET @x=(SELECT PramryKeyForThisTable,AutoNumberColumn,Data1,Data2,Data3 FROM tblInsertTableForAutoNumber FOR XML AUTO,ROOT('MYROOT'))
                    

                    INSERT INTO CopyOftblInserTableForAutonumber
                    SELECT
                    R.Tab.value('@PrimaryKeyForThisTable[1]','SMALLINT)') AS PramryKeyForThisTable,
                    R.Tab.value('@AutoNumberColumntext[1]','SMALLINT')AS AutoNumberColumn,
                    R.Tab.value('@Data1 [1]','VARCHAR(50)') AS Data1,
                    R.Tab.value('@Data2 [1]','VARCHAR(50)') AS Data2,
                    R.Tab.value('@Data3 [1]','VARCHAR(50)') AS Data3
                    FROM @x.nodes('//MYROOT/tblInsertTableForAutoNumber')AS R(Tab)

                    END

                    i also print the @x variable.I got the correct XML with all data.But when i go to the XQuery also i got all the row and values except PrimaryKeyForThisTable, AutoNumberColumntext column values.i got null values for those columns.I thing there might be a dataconversion problem.I tried the text() function .But nothing happened.How i get those values?

                    Thabo

                    W Offline
                    W Offline
                    Wendelius
                    wrote on last edited by
                    #9

                    Hi, Since I don't have the data you have and don't know that table structure it's hard to where the problem lies. I created a small test-case which works fine (at least on my machine :)). Could you try this and perhaps it helps you to pinpoint the problem. If the problem still remains, could you post the creation script for the table along with few test rows.

                    create table Test (
                    id int not null primary key identity(1,1),
                    val varchar(10) not null
                    );

                    insert into Test (val) values ('First');
                    insert into Test (val) values ('Second');
                    delete from Test;
                    insert into Test (val) values ('First');
                    insert into Test (val) values ('Second');

                    DECLARE @x XML
                    begin
                    SET @x=(SELECT id, val
                    FROM Test
                    FOR XML AUTO ,TYPE ,ELEMENTS XSINIL,ROOT('TABLEROOT'));

                    print convert(varchar(max), @x)

                    SELECT tab.col.value('id[1]','SMALLINT') AS NewId,
                    tab.col.value('val[1]','VARCHAR(10)') AS NewVal
                    FROM @x.nodes('//TABLEROOT/Test')AS tab(Col);
                    end

                    mika p.s. Don't mind about the delete in the middle of the inserts. I just wanted to see that I get the actual identity values so if this is run once the id's should be 3 and 4.

                    The need to optimize rises from a bad design.My articles[^]

                    T 1 Reply Last reply
                    0
                    • W Wendelius

                      Hi, Since I don't have the data you have and don't know that table structure it's hard to where the problem lies. I created a small test-case which works fine (at least on my machine :)). Could you try this and perhaps it helps you to pinpoint the problem. If the problem still remains, could you post the creation script for the table along with few test rows.

                      create table Test (
                      id int not null primary key identity(1,1),
                      val varchar(10) not null
                      );

                      insert into Test (val) values ('First');
                      insert into Test (val) values ('Second');
                      delete from Test;
                      insert into Test (val) values ('First');
                      insert into Test (val) values ('Second');

                      DECLARE @x XML
                      begin
                      SET @x=(SELECT id, val
                      FROM Test
                      FOR XML AUTO ,TYPE ,ELEMENTS XSINIL,ROOT('TABLEROOT'));

                      print convert(varchar(max), @x)

                      SELECT tab.col.value('id[1]','SMALLINT') AS NewId,
                      tab.col.value('val[1]','VARCHAR(10)') AS NewVal
                      FROM @x.nodes('//TABLEROOT/Test')AS tab(Col);
                      end

                      mika p.s. Don't mind about the delete in the middle of the inserts. I just wanted to see that I get the actual identity values so if this is run once the id's should be 3 and 4.

                      The need to optimize rises from a bad design.My articles[^]

                      T Offline
                      T Offline
                      Thabo Codeproject
                      wrote on last edited by
                      #10

                      Hi thank u very much.... I made a simple mistake... i re wrote the coding as this

                      set QUOTED_IDENTIFIER ON
                      GO
                      -- =============================================
                      -- Author: Thabo
                      -- Create date: 18.02.2011
                      -- Description: TEST XML
                      -- =============================================
                      CREATE PROCEDURE [dbo].[sppassingStoredProcedue]
                      AS
                      DECLARE @x XML
                      BEGIN

                      SET NOCOUNT ON;
                      SET @x=(SELECT PramryKeyForThisTable,AutoNumberColumn,Data1,Data2,Data3 FROM tblInsertTableForAutoNumber FOR XML AUTO,ROOT('MYROOT'))
                      

                      INSERT INTO CopyOftblInserTableForAutonumber
                      SELECT
                      R.Tab.value('@PramryKeyForThisTable [1]','SMALLINT') AS PramryKeyForThisTable,
                      R.Tab.value('@AutoNumberColumn [1]','SMALLINT')AS AutoNumberColumn,
                      R.Tab.value('@Data1 [1]','VARCHAR(50)') AS Data1,
                      R.Tab.value('@Data2 [1]','VARCHAR(50)') AS Data2,
                      R.Tab.value('@Data3 [1]','VARCHAR(50)') AS Data3
                      FROM @x.nodes('//MYROOT/tblInsertTableForAutoNumber')AS R(Tab)

                      END

                      The mistake which i made before is ,the colum name which is in XQUERY was not same as in select statement.Now All are Working fine :-D Thank you agin for your help.I am a new person to Codeproject.But i got great response !!!!Thank you .Now i am always using code project ;)

                      Thabo

                      W 1 Reply Last reply
                      0
                      • T Thabo Codeproject

                        Hi thank u very much.... I made a simple mistake... i re wrote the coding as this

                        set QUOTED_IDENTIFIER ON
                        GO
                        -- =============================================
                        -- Author: Thabo
                        -- Create date: 18.02.2011
                        -- Description: TEST XML
                        -- =============================================
                        CREATE PROCEDURE [dbo].[sppassingStoredProcedue]
                        AS
                        DECLARE @x XML
                        BEGIN

                        SET NOCOUNT ON;
                        SET @x=(SELECT PramryKeyForThisTable,AutoNumberColumn,Data1,Data2,Data3 FROM tblInsertTableForAutoNumber FOR XML AUTO,ROOT('MYROOT'))
                        

                        INSERT INTO CopyOftblInserTableForAutonumber
                        SELECT
                        R.Tab.value('@PramryKeyForThisTable [1]','SMALLINT') AS PramryKeyForThisTable,
                        R.Tab.value('@AutoNumberColumn [1]','SMALLINT')AS AutoNumberColumn,
                        R.Tab.value('@Data1 [1]','VARCHAR(50)') AS Data1,
                        R.Tab.value('@Data2 [1]','VARCHAR(50)') AS Data2,
                        R.Tab.value('@Data3 [1]','VARCHAR(50)') AS Data3
                        FROM @x.nodes('//MYROOT/tblInsertTableForAutoNumber')AS R(Tab)

                        END

                        The mistake which i made before is ,the colum name which is in XQUERY was not same as in select statement.Now All are Working fine :-D Thank you agin for your help.I am a new person to Codeproject.But i got great response !!!!Thank you .Now i am always using code project ;)

                        Thabo

                        W Offline
                        W Offline
                        Wendelius
                        wrote on last edited by
                        #11

                        You're welcome. Glad that it helped :)

                        The need to optimize rises from a bad design.My articles[^]

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups