SQL Transform XML attributes into columns

Asked

Viewed 26 times

0

I have the following code:

IF OBJECT_ID('dbo.tabTeste') IS NOT NULL DROP TABLE dbo.tabTeste
GO
CREATE TABLE dbo.tabTeste ( 
    id_tab INT IDENTITY PRIMARY KEY, 
    field1_tab VARCHAR(255),
    paramsxml_tab XML)
GO
INSERT INTO dbo.tabTeste (field1_tab, paramsxml_tab) 
    SELECT 'Teste','<row war_dat="1" war_ini="2" war_end="3"></row>'
GO
SELECT * FROM dbo.tabTeste

which results in:

id_tab - field1_tab - paramsxml_tab
1      - Teste      - '<row war_dat="1" war_ini="2" war_end="3" />'

As in this SELECT transform the values of xml attributes into columns ?

id_tab - field1_tab - war_dat - war_ini - war_end
1      - Teste      - 1       - 2       - 3

1 answer

1

I managed to find something that solved my Query minutes after posting the question:

SELECT

    id_tab, 

    field1_tab, 

    paramsxml_tab.value('(/row/@war_dat)[1]', 'int') as war_dat,

    paramsxml_tab.value('(/row/@war_ini)[1]', 'int') as war_ini,

    paramsxml_tab.value('(/row/@war_end)[1]', 'int') as war_end

FROM dbo.tabTeste

Browser other questions tagged

You are not signed in. Login or sign up in order to post.