Hi,
I'm still sort of a newbie with TSQL, and a total newbie to XML, so please bear with me.
It's in SQL Express 2008. Compatibility level is set at 100.
I'm trying to import an XML file into a table. It's able to import the whole thing into a table, but what I want is for it to parse into separate fields. It won't even compile.
The code is more-or-less copied from the SQL Bible (book), which has served me very well in the past. Unfortunately, it doesn't really explain what the "o(h)" and "i(x)" in its example code means, and I can't see anything like it when googling, so I'm kind of coding blind. This is undoubtedly the root of the problem.
The problem is that the text (marked in red below) has the red squigglies in SSMS, so it won't parse. Mousing over "p.value", it says, "cannot find either column 'p' or the user defined function or the aggregate 'p.value', or the name is ambiguous." "r.value" says the same. "p.nodes" says, "Invalid object name 'p.nodes'." Please help!
Here's the code, minus the stored procedure container:
Thanks!
Katie
I'm still sort of a newbie with TSQL, and a total newbie to XML, so please bear with me.
It's in SQL Express 2008. Compatibility level is set at 100.
I'm trying to import an XML file into a table. It's able to import the whole thing into a table, but what I want is for it to parse into separate fields. It won't even compile.
The code is more-or-less copied from the SQL Bible (book), which has served me very well in the past. Unfortunately, it doesn't really explain what the "o(h)" and "i(x)" in its example code means, and I can't see anything like it when googling, so I'm kind of coding blind. This is undoubtedly the root of the problem.
The problem is that the text (marked in red below) has the red squigglies in SSMS, so it won't parse. Mousing over "p.value", it says, "cannot find either column 'p' or the user defined function or the aggregate 'p.value', or the name is ambiguous." "r.value" says the same. "p.nodes" says, "Invalid object name 'p.nodes'." Please help!
Here's the code, minus the stored procedure container:
Code:
DECLARE @xml XML
SELECT @xml = CAST(bulkcolumn AS XML)
FROM OPENROWSET(BULK 'C:\Users\K\Documents\Database-20120309190505.xml', SINGLE_BLOB) AS p
INSERT INTO dbo.IMPORT_Fields(PageID, PageTitle, RevisionTimestamp, RevisionText)
SELECT
[COLOR=#ff0000]p.value[/color]('@id','INT') AS PageID,
[COLOR=#ff0000]p.value[/color]('@title','VARCHAR(250)') AS PageTitle,
[COLOR=#ff0000]r.value[/color]('@timestamp','VARCHAR(50)') AS RevisionTimestamp,
[COLOR=#ff0000]r.value[/color]('@text','NTEXT') AS RevisionText
FROM @xml.nodes('/mediawiki/page') o(p)
CROSS APPLY [COLOR=#ff0000]p.nodes[/color]('revision') i(r)
Thanks!
Katie