Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server 2005 Querying XML column Data
    primarykey
    data
    text
    <p>I have a table called <code>People</code> with a columns of datatype <code>xml</code> called <code>properties</code>. I've used this to store random information about each person basically allowing people to store any extra data that are added in the future without a database redesign. Not all people will have the same elements in their xml. </p> <pre><code>CREATE TABLE [dbo].[Person]( [PersonID] [bigint] IDENTITY(1,1) NOT NULL, [PersonType] [nvarchar](50) NULL, [Title] [nvarchar](5) NULL, [Forename] [nvarchar](60) NULL, [Surname] [nvarchar](60) NULL, [Company] [nvarchar](60) NULL, [Properties] [xml] NULL ) </code></pre> <p>An example of the xml is:</p> <pre><code>&lt;PropertyList xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt; &lt;Property Name="Class"&gt;Class A&lt;/Property&gt; &lt;Property Name="CarRegistration"&gt;123456&lt;/Property&gt; &lt;Property Name="MedicalNotes"&gt;None&lt;/Property&gt; &lt;/PropertyList&gt; </code></pre> <p>First question is I can't seem to find a SQL query that will allow me to get a list of records that match criteria stored in the xml. </p> <p>For example how would i get all records where the <code>Class="Class A"</code>. I've tried :</p> <pre><code>SELECT PersonID, Properties.value('/PropertyList/Property[@Name="Class"][1]','nvarchar(50)') FROM Person </code></pre> <p>I know this is incorrect but I get the error "requires a singleton (or empty sequence)" and I'm not quite sure whats gone wrong.</p> <p>And second side question is I've combined several older databases into a single person list however the old database frontends still need to access their bit of the data. My plan was to create a view for each database frontend with a layout specific for its needs all linking back to the main people table. However some of their fields are now stored in the XML. Is there any way of creating a view to update the XML without seeing the xml i.e. so it looks and acts just like a view on any other table. I hope i explained that correctly. For each view I will have a specific set of XML properties I need them to edit and all records will have them so its not so random. </p> <p>Thanks for any help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload