读取从SQL保存为文本的XML

时间:2010-10-22 21:36:51

标签: sql sql-server xml tsql

这是我的查询:

select top 5   cast(content_html as xml) as [prodxml],
prodxml.value('data(ClassTemplate[1]', 'nvarchar(max) ') as prod2
from content 
where 
end_date >= getdate()
and folder_id != 71682 

我一直在接受:

Msg 4121, Level 16, State 1, Line 1
Cannot find either column "prodxml" or the user-defined function or aggregate "prodxml.value", or the name is ambiguous.

我做错了什么?

1 个答案:

答案 0 :(得分:3)

  

我无法直接查询prod1,我怎样才能找到所有包含“其他”作为类模板的记录?

您不能在同一SELECT语句的另一列中引用列别名 - 使用:

SELECT TOP 5   
       CAST(t.content_html AS XML).value('(/root/ClassTemplate)[1]', 'NVARCHAR(max)') AS prod2
  FROM CONTENT t
 WHERE t.end_date >= getdate()
   AND t.folder_id != 71682 

如果要根据WHERE子句中的prod2值过滤掉 - 请使用:

  FROM CONTENT t
 WHERE CAST(t.content_html AS XML).value('(/root/ClassTemplate)[1]', 'NVARCHAR(max)') = 'Other'
   AND t.end_date >= getdate()
   AND t.folder_id != 71682