I have done some basic XML querying using T-SQL but I am not sure how to attack this problem.
I would like to query all rows in a table where a specific XML attribute has a requested value.
Here is my table schema:
CREATE TABLE [dbo].[Applications_Submissions]
(
[Id] [int] IDENTITY(1,1) NOT NULL,
[SubmissionGuid] [uniqueidentifier] NOT NULL,
[TestMode] [bit] NOT NULL,
[SubmissionID] [int] NOT NULL,
[Status] [int] NOT NULL,
[ProcessId] [int] NOT NULL,
[Version] [int] NOT NULL,
[ReturnUrl] [nvarchar](1000) NULL,
[Applications] [xml] NOT NULL,
[CreatedByUser] [int] NOT NULL,
[DateCreated] [datetime] NOT NULL,
[DateDeleted] [datetime] NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [FG_Applications]
) ON [FG_Applications] TEXTIMAGE_ON [FG_Applications]
The XML data stored in the [Application] column is structured like this:
<Submission xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Id="15" TestMode="false" SubmissionId="15" Status="0" ProcessId="0" Version="1" CreatedByUser="2" DateCreated="2016-09-28T15:08:25.667" DateDeleted="0001-01-01T00:00:00">
<ReturnUrl>/Register/RegistrationData/Register/1094/1/[SGUID]</ReturnUrl>
<SubmissionGuid>f1891c84-1fda-41b4-a78b-605bfdcaf45a</SubmissionGuid>
<Applications>
<Application ApplicationID="15" ApplicationName="Test Form">
<FriendlyUrl>test-form-1234</FriendlyUrl>
<Pages>
<Page PageId="16" ApplicationId="15" ViewOrder="0">
<PageTitle>Page 1</PageTitle>
<Fields>
<Field FieldId="26" FieldType="10" ViewOrder="0">
<FieldTitle>Who are you</FieldTitle>
<FieldValues>Jeffrey</FieldValues>
<FieldsResponses />
<PossibleFieldValues />
</Field>
<Field FieldId="27" FieldType="0" ViewOrder="0">
<FieldTitle>Why are you here?</FieldTitle>
<FieldValues />
<FieldsResponses />
<PossibleFieldValues />
</Field>
<Field FieldId="28" FieldType="5" ViewOrder="0">
<FieldTitle>Pick any</FieldTitle>
<FieldsResponses>
<FieldsResponse FieldResponseId="19" LinkToApplication="0" ViewOrder="0" Selected="false">
<Response>One</Response>
</FieldsResponse>
<FieldsResponse FieldResponseId="20" LinkToApplication="12" ViewOrder="0" Selected="false">
<Response>Two</Response>
</FieldsResponse>
<FieldsResponse FieldResponseId="21" LinkToApplication="0" ViewOrder="0" Selected="false">
<Response>Three</Response>
</FieldsResponse>
</FieldsResponses>
<PossibleFieldValues />
</Field>
</Fields>
</Page>
<Page PageId="17" ApplicationId="15" ViewOrder="0">
<PageTitle>Page 2</PageTitle>
<Fields>
<Field FieldId="29" FieldType="6" ViewOrder="0">
<FieldTitle>Agreement</FieldTitle>
<FieldsResponses>
<FieldsResponse FieldResponseId="22" LinkToApplication="16" ViewOrder="0" Selected="false">
<Response>I agree</Response>
</FieldsResponse>
<FieldsResponse FieldResponseId="23" LinkToApplication="16" ViewOrder="0" Selected="false">
<Response>I disagree</Response>
</FieldsResponse>
</FieldsResponses>
<PossibleFieldValues />
</Field>
</Fields>
</Page>
</Pages>
</Application>
</Applications>
</Submission>
I would like to get the table rows based on the ApplicationID attribute that is stored in the <Application> node. Each <Submission> can have multiple <Application> nodes.
What is the best way of doing this?
Thank you so much!