Expand all the xml elements into columns
-
sábado, 14 de abril de 2012 13:27
how can I change this query to expand all the xml elements into columns:
SELECT TOP 40000 [id]
,[xmldata]
FROM [PrepaidCard].[dbo].[TransactionsXML]id data
412 <MTID>100</MTID><LOCALDATE>2011-08-10</LOCALDATE><LOCALTIME>115321</LOCALTIME>
Todas las respuestas
-
sábado, 14 de abril de 2012 13:41
SELECT TOP 40000 [id], [xmldata].value('(/MTID/node())[1]', 'nvarchar(max)') as MTID, [xmldata].value('(/LOCALDATE/node())[1]', 'nvarchar(max)') as LOCALDATE, [xmldata].value('(/LOCALTIME/node())[1]', 'nvarchar(max)') as LOCALTIME FROM [PrepaidCard].[dbo].[TransactionsXML]
Something like this - I need to actually check the syntax
Chuck
- Editado Chuck Pedretti sábado, 14 de abril de 2012 13:42
- Marcado como respuesta Quantum Information sábado, 14 de abril de 2012 13:54
-
sábado, 14 de abril de 2012 13:44
Looks like that worked - just change the datatypes in the .value statements from nvarchar(max) to whatever makes sense for the data
DECLARE @temp as TABLE(id int, xmldata xml) INSERT @temp VALUES(1,'<MTID>100</MTID><LOCALDATE>2011-08-10</LOCALDATE><LOCALTIME>115321</LOCALTIME>') SELECT TOP 40000 [id], [xmldata].value('(/MTID/node())[1]', 'nvarchar(max)') as MTID, [xmldata].value('(/LOCALDATE/node())[1]', 'nvarchar(max)') as LOCALDATE, [xmldata].value('(/LOCALTIME/node())[1]', 'nvarchar(max)') as LOCALTIME FROM @temp
Chuck
- Editado Chuck Pedretti sábado, 14 de abril de 2012 13:45
-
sábado, 14 de abril de 2012 13:50
nice one, is there a way to automatically churn out those 3 extra columns without having to do the
[xmldata].value('(/MTID/node())[1]', 'nvarchar(max)') as MTID, [xmldata].value('(/LOCALDATE/node())[1]', 'nvarchar(max)') as LOCALDATE, [xmldata].value('(/LOCALTIME/node())[1]', 'nvarchar(max)') as LOCALTIME
-
sábado, 14 de abril de 2012 13:50
Try this:
SELECT id, xmlData.value('(MTID/text())[1]', 'INT') MTID, xmlData.value('(LOCALDATE/text())[1]', 'CHAR(10)') LOCALDATE, xmlData.value('(LOCALTIME/text())[1]', 'CHAR(8)') LOCALTIME FROM [PrepaidCard].[dbo].[TransactionsXML] -
sábado, 14 de abril de 2012 13:52
thx but say I have elements per node and don't want to write 20 columns, can it be done in 1 line?
SELECT id, cheat code here? FROM [PrepaidCard].[dbo].[TransactionsXML]
-
sábado, 14 de abril de 2012 13:53
nice one, is there a way to automatically churn out those 3 extra columns without having to do the
[xmldata].value('(/MTID/node())[1]', 'nvarchar(max)') as MTID, [xmldata].value('(/LOCALDATE/node())[1]', 'nvarchar(max)') as LOCALDATE, [xmldata].value('(/LOCALTIME/node())[1]', 'nvarchar(max)') as LOCALTIME
Not that I know of - because if you think of XML you could have any level of nesting of the tagsChuck
-
sábado, 14 de abril de 2012 13:55cool mate , thx

