我在SQL Server 2008表中有一個XML列。我想要做的是爲我的存儲過程提供一個給定的參數去掉參數中的任何空格(使用REPLACE
),並在WHERE
條件中使用這個參數,但是接下來使用XQuery存在子句也可以在xml數據上使用REPLACE
方法:在SQL Server中使用替換條件查詢XML數據
-- Add the parameters for the stored procedure here
@PostCode varchar(20) = ''
AS
BEGIN
-- strip out any spaces from the post code param
SET @PostCode = REPLACE(@PostCode, ' ','')
SELECT TOP 1 *
FROM sd_LocalAuthorities
WHERE PostCodes.exist(N'REPLACE(/PostCodes/PostCode/text(), '' '','''')[. = sql:variable("@PostCode")]') = 1
END
我得到了錯誤的XQuery的sd_LocalAuthorities.PostCodes.exist()
沒有功能「{} http://www.w3.org/2004/07/xpath-functions:REPLACE()
運行該程序時。有沒有其他REPLACE()
我可以使用這個WHERE
標準去掉空格,我不想修改表本身。
存在函數需要XQuery表達式作爲參數,而不是T-SQL函數。工作,但*效率不高的方法是將PostCodes轉換爲varchar,替換雙空格並轉換爲xml。 – EKOlog 2015-02-10 14:58:33
感謝您指示我試過這個,就像您說它效率不高,需要10秒鐘才能運行查詢 – user3783297 2015-02-10 15:10:19