我正在使用带有Microsoft SQL Server的Transact-SQL,我们的查询如下所示:
SELECT Cast( Cast ( Cast(XMLBlob as XML).query(N'//continent/forest/tree/age/Text()') as nvarchar) as bigint),
AnotherField
FROM [MyDB].[dbo].[mytable]
WHERE Cast( Cast ( Cast(XMLBlob as XML).query(N'//continent/forest/tree/age/Text()') as nvarchar) as bigint)
between 10 and 100
XML强制转换是一项昂贵的操作,因为它在WHERE和SELECT中都使用了,看起来我应该能够将它作为变量保存在WHERE中(按操作顺序) ,在SELECT之前进行评估,并在SELECT中使用它而不必再次强制转换。这可能吗?
答案 0 :(得分:1)
您可以使用内部查询来检索XML值。然后在内部查询之外,您返回该bigint值并过滤所需的值:
SELECT innerTable.Age, innerTable.AnotherField
FROM (
SELECT Cast( Cast ( Cast(XMLBlob as
XML).query(N'//continent/forest/tree/age/Text()') as nvarchar) as bigint) AS Age,
AnotherField
FROM [MyDB].[dbo].[mytable]
) AS innerTable
WHERE innerTable.Age between 10 and 100
顺便说一句......为什么你需要一个bigint来存储Age?如果你存储的年份看起来像矫枉过正,即使是那些生活了数千年的树木:)