使用CROSS APPLY查询,尝试根据属性值获取字段

时间:2018-04-25 20:03:37

标签: sql-server xml sql-server-2012 xml-attribute

我尝试根据属性(Culture)的值,从SQL Server中存储为XML的表单中获取所有字段名称。实际上,我可以获取文本值,但它们会连接到FieldName列。

XML结构如下所示:

<Form>
  <Field>
    <Field Name="email" Type="text" ColumnSize="6">
      <Localizations>
        <Localization Culture="fr">
          <Text>Adresse couriel</Text>
          <Placeholder>Entrez votre adresse courriel</Placeholder>
        </Localization>
        <Localization Culture="fr">
          <Text>Email</Text>
          <Placeholder>Enter your email</Placeholder>
        </Localization>
      </Localizations>
    </Field>
  </Fields>
</Form>

SQL查询:

DECLARE @language VARCHAR(2)
SET @language = 'en'
SELECT 
    c.query('data(@Name)') AS FieldUniqueName, 
    c.query('data(./Localizations/Localization/Text)') AS FieldName, 
    c.query('data(@Type)') AS FieldType
FROM dbo.Form Form CROSS APPLY FormContent.nodes('/Form/Fields/Field') x(c)
WHERE Id = @formId
AND FormContent.exist('/Form/Fields/Field/Localizations/Localization[@Culture=sql:variable("@language")]') = 1

结果如下所示:

FieldUniqueName  |  FieldName             | FieldType
name             |  Nom Name              | text
email            |  Adresse couriel Email | text
phone            |  Téléphone Phone Number| text

这两种语言都在FieldName列中!

我也尝试过:

...
c.value('data(./Localizations/Localization/Text)[1]', 'VARCHAR(100)') AS FieldName,
...

但我需要在[1][2]之间进行选择以获得正确的语言...

2 个答案:

答案 0 :(得分:2)

除了事实,你提供的XML(结构和&#34; fr&#34;两者)都有一些错误,你可以试试这个:

DECLARE @tbl TABLE(ID INT IDENTITY, FormContent XML);
INSERT INTO @tbl VALUES
(N'<Form>
  <Fields>
    <Field Name="email" Type="text" ColumnSize="6">
      <Localizations>
        <Localization Culture="fr">
          <Text>Adresse couriel</Text>
          <Placeholder>Entrez votre adresse courriel</Placeholder>
        </Localization>
        <Localization Culture="en">
          <Text>Email</Text>
          <Placeholder>Enter your email</Placeholder>
        </Localization>
      </Localizations>
    </Field>
  </Fields>
</Form>');

查询将根据您的变量返回loc值。

DECLARE @language VARCHAR(2)
SET @language = 'en'
SELECT 
    fld.value(N'@Name',N'nvarchar(max)') AS FieldUniqueName, 
    fld.value(N'@Type',N'nvarchar(max)') AS FieldType, 
    loc.value(N'(Text/text())[1]','nvarchar(max)') AS LocalText, 
    loc.value(N'(Placeholder/text())[1]','nvarchar(max)') AS LocalPlaceholder
FROM @tbl Form 
CROSS APPLY FormContent.nodes(N'/Form/Fields/Field') AS A(fld)
CROSS APPLY A.fld.nodes(N'Localizations/Localization[@Culture=sql:variable("@language")]') B(loc)
WHERE Id = 1

答案 1 :(得分:0)

好的,我找到了对我的问题的回复!我没有使用where子句,而是更改了FieldName的XPath。像魅力一样:

DECLARE @language VARCHAR(2)
SET @language = 'en'
SELECT 
    c.query('data(@Name)') AS FieldUniqueName, 
    c.query('data(./Localizations/Localization[@Culture=sql:variable("@language")]/Text)') AS FieldName, 
    c.query('data(@Type)') AS FieldType
FROM dbo.Form Form CROSS APPLY FormContent.nodes('/Form/Fields/Field') x(c)
WHERE Id = @formId