Update Temp table to Insert node in XML data using SQL

Rick

My question is related to this question. However I am writing a clean scenario on which I need help. I am a beginner in SQL and pardon me If I'm incorrect anywhere.

I have a procedure(huge on. replicating a small sample here) that spits out bunch of XMLs. On my second procedure, based on Parametric condition I have to insert a node in the XML for every CID and do a final select. I am adding a Rextester link below:

UPDATED LINK: http://rextester.com/EFGQB11125

Current output:

  <Main>
    <ID>1001</ID>
    <details>
        <name>John</name>
        <age>12</age>
    </details>
</Main>
<Main>
    <ID>1002</ID>
    <details>
        <name>Rick</name>
        <age>19</age>
    </details>
</Main>
<Main>
    <ID>1003</ID>
    <details>
        <name>Diane</name>
        <age>25</age>
    </details>
</Main>
<Main>
    <ID>1004</ID>
    <details>
        <name>Kippy</name>
        <age>26</age>
    </details>
</Main>

Desired output:

When @type = 'N'/'U' then

    <Main>
    <ID>1001</ID>
    <type>N</type>
    <details>
        <name>John</name>
        <age>12</age>
    </details>
</Main>
<Main>
    <ID>1002</ID>
    <type>U</type>
    <details>
        <name>Rick</name>
        <age>19</age>
    </details>
</Main>
<Main>
    <ID>1003</ID>
    <type>N</type>
    <details>
        <name>Diane</name>
        <age>25</age>
    </details>
</Main>
<Main>
    <ID>1004</ID>
    <type>N</type>
    <details>
        <name>Kippy</name>
        <age>26</age>
    </details>
</Main>

So, for every CID, based on @type values, a node should be inserted with that value. Any help?!

(Sorry for being redundant by any chance)

LukStorms

The trick used in the example below uses a bit of Dynamic Sql magic.

Basically building 1 big SQL with all the updates for each ID.

declare @Sql varchar(max); 

select @Sql = concat(@Sql,'UPDATE #final SET xml_data.modify(''insert <type>',ctype,'</type> after (/Main/ID)[text()="',cid,'"][1]'');',CHAR(13),CHAR(10))
from #tbl;

-- select @Sql as xml_modify_sqls;
exec(@Sql);

select * from #final;

And here's some SQL to setup the temporary tables with the sample data:

IF OBJECT_ID('tempdb..#tbl') IS NOT NULL DROP TABLE #tbl;
create table #tbl (cid int, ctype varchar(5));
IF OBJECT_ID('tempdb..#cdetails') IS NOT NULL DROP TABLE #cdetails;
create table #cdetails (cid int, name varchar(5), age int);
IF OBJECT_ID('tempdb..#final') IS NOT NULL DROP TABLE #final;
create table #final (xml_data xml);
insert into #tbl (cid, ctype) values
(1001,'N'), (1002,'U'), (1003,'N'), (1004,'N');
insert into #cdetails (cid, name, age) values
(1001,'John',12), (1002,'Rick',19), (1003,'Diane',25), (1004,'Kippy',26);
insert into #final (xml_data)
select xml_data
from (
    select 
    cd1.cid as ID,
    -- type = t.ctype as type,
    details =
    (
        select 
         cd.name,
         cd.age
        from #cdetails cd
        where cd.cid = cd1.cid
        For XML Path(''), Type
    )
    from #cdetails cd1
    join #tbl t on cd1.cid = t.cid
    For XML Path('Main')
) q(xml_data);
select * from #final;

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Insert XML Data to MySQL Table Using PHP

分類Dev

How to update a SQL table column with XML data

分類Dev

how to insert and update data into postgresql from node js using if condition

分類Dev

SQL inserting data into a temp table, splitting a column

分類Dev

Insert or update data using cte_results in SQL Server

分類Dev

Insert data row wise in a SQL Table from a CSV using PowerShell

分類Dev

Insert XML nodes into a table in SQL Server

分類Dev

insert data into table using the today() function

分類Dev

SQL Insert data from 2 table to new table

分類Dev

Select table from Sql server and insert data to Mysql table

分類Dev

How to insert data into Microsoft Sql using powershell?

分類Dev

UPDATE Column in Same Table after INSERT using trigger in Oracle

分類Dev

How to update a table using SQL in a PHP code?

分類Dev

How to update table using foreign key in sql

分類Dev

How to insert an xml file in a single cell of sql table

分類Dev

SQL - How to declare date range in a temp table?

分類Dev

SQL Insert multiple record while using ON DUPLICATE KEY UPDATE

分類Dev

Trouble using temp table in where clause

分類Dev

How to insert multiple rows into SQL Server Parallel Data Warehouse table

分類Dev

SQL Server insert table data script with new Primary Key

分類Dev

How to use sql simple query to insert data from different table

分類Dev

Insert into SQL table

分類Dev

How to insert values directly into SQL base table using pyhton?

分類Dev

INSERT using T-SQL with 2 table-value functions

分類Dev

JS: How to make logic (using loops) to insert data into this table?

分類Dev

Update MySQL table after INSERT

分類Dev

SQL Server parse XML to table - multiple node with the same name and first node should be table columns

分類Dev

UPDATE from temp table picking the "last" row per group

分類Dev

SQLite: How to retrieve data from column in one table using SELECT to insert retrieved data in another table

Related 関連記事

  1. 1

    Insert XML Data to MySQL Table Using PHP

  2. 2

    How to update a SQL table column with XML data

  3. 3

    how to insert and update data into postgresql from node js using if condition

  4. 4

    SQL inserting data into a temp table, splitting a column

  5. 5

    Insert or update data using cte_results in SQL Server

  6. 6

    Insert data row wise in a SQL Table from a CSV using PowerShell

  7. 7

    Insert XML nodes into a table in SQL Server

  8. 8

    insert data into table using the today() function

  9. 9

    SQL Insert data from 2 table to new table

  10. 10

    Select table from Sql server and insert data to Mysql table

  11. 11

    How to insert data into Microsoft Sql using powershell?

  12. 12

    UPDATE Column in Same Table after INSERT using trigger in Oracle

  13. 13

    How to update a table using SQL in a PHP code?

  14. 14

    How to update table using foreign key in sql

  15. 15

    How to insert an xml file in a single cell of sql table

  16. 16

    SQL - How to declare date range in a temp table?

  17. 17

    SQL Insert multiple record while using ON DUPLICATE KEY UPDATE

  18. 18

    Trouble using temp table in where clause

  19. 19

    How to insert multiple rows into SQL Server Parallel Data Warehouse table

  20. 20

    SQL Server insert table data script with new Primary Key

  21. 21

    How to use sql simple query to insert data from different table

  22. 22

    Insert into SQL table

  23. 23

    How to insert values directly into SQL base table using pyhton?

  24. 24

    INSERT using T-SQL with 2 table-value functions

  25. 25

    JS: How to make logic (using loops) to insert data into this table?

  26. 26

    Update MySQL table after INSERT

  27. 27

    SQL Server parse XML to table - multiple node with the same name and first node should be table columns

  28. 28

    UPDATE from temp table picking the "last" row per group

  29. 29

    SQLite: How to retrieve data from column in one table using SELECT to insert retrieved data in another table

ホットタグ

アーカイブ