I am currently generating a Report using a Stored Procedure. The stored procedure gathers information about Unprocessed Files from a table of Files that resembles the following (simplfied for brevity):
Table Structure:
//File Table Fields
FILE_ID (int)
FILE_DATE (string)
FILE_CONTENTS (XML)
FILE_CONTENTS_STRING (string)
PROCESSED (bool)
Stored Procedure:
//Grabs the ID and Date for each of the Unprocessed Files
SELECT [FILE_ID],
[FILE_DATE]
FROM [tbFILES]
WHERE [PROCESSED] = 0
I would like to output two additional fields that are found in the FILE_CONTENTS (or FILE_CONTENTS_STRING), which are both found in the XML / String in areas like these:
Portion of XML Structure:
<FID.4>
<FID.4.1>TESTING</FID.4.1> //File Header
</FID.4>
<FID.5>
<FID.5.1>TEST</FID.5.1> //Owner Last Name
<FID.5.2>TEST</FID.5.2> //Owner First Name
<FID.5.3 />
<FID.5.4 />
<FID.5.5 />
<FID.5.6 />
<FID.5.7 />
<FID.5.8 />
</FID.5>
What I would like to accomplish is to output these two values (File Header) and Owner Name (Last,First) as part of the Stored Procedure Call.
Output:
[FILE_ID] //From Table
[FILE_DATE] //From Table
[FILE_HEADER] //From FILE_CONTENTS in <FID.4.1></FID.4.1>
[FILE_OWNER] //From FILE_CONTENTS in <FID.5.1></FID.5.1>,<FID.5.2></FID.5.2>
Is it possibly to query this type of information from the XML File (FILE_CONTENTS) or the XML File in string-form (FILE_CONTENTS_STRING) using a SQL Stored Procedure?
Edit: First Attempt (unsuccessful)
SELECT FILE_ID,
FILE_DATE,
FILE_CONTENTS.value('(/PID.4/PID.4.1)[1]', 'varchar(16)') as FILE_HEADER,
FILE_CONTENTS.value('(/PID.5/PID.5.1)[1]', 'varchar(16)') + ', ' +
FILE_CONTENTS.value('(/PID.5/PID.5.2)[1]', 'varchar(16)') as FILE_OWNER
FROM [tbFILES]
which yielded NULL for the FILE_HEADER and FILE_OWNER fields. I presume that something more complex is required?
Update again: (Problem Solved!)
I had to add an additional section to the XML, as the portion provided was originally a snippet of a larger XML document.