Sql-Server

執行計劃警告基數 xml 數據類型

  • February 19, 2020

我有以下陳述:

-- 1st
DECLARE @AuditParameters XML = (
   SELECT
       1 AS AccountID,
       2 AS CategoryID,
       3 AS CategoryAttributeID,
       '4' AS SyncBatchGUID
   FOR XML PATH(N'Parameters'), ELEMENTS XSINIL
)
-- 2nd
DECLARE @AuditParameters XML = convert(xml, (
   SELECT
       1 AS AccountID,
       2 AS CategoryID,
       3 AS CategoryAttributeID,
       '4' AS SyncBatchGUID
   FOR XML PATH(N'Parameters'), ELEMENTS XSINIL
))

兩個語句產生相同的執行計劃,但是,它們顯示警告消息

表達式中的類型轉換 (CONVERT_IMPLICIT(xml,

$$ Expr1004 $$,0)) 可能會影響查詢計劃選擇中的“CardinalityEstimate”。

您可能會注意到我嘗試將第二條語句轉換為 XML,但我收到了相同的警告消息。

我怎樣才能擺脫上述聲明中的這個警告資訊?

謝謝

在 FOR XML 查詢中使用TYPE 指令

SQL Server 對xml (Transact-SQL) 的支持使您可以選擇通過指定 TYPE 指令請求將 FOR XML 查詢的結果作為 xml 數據類型返回。

-- 1st
DECLARE @AuditParameters XML = (
   SELECT
       1 AS AccountID,
       2 AS CategoryID,
       3 AS CategoryAttributeID,
       '4' AS SyncBatchGUID
   FOR XML PATH(N'Parameters'), ELEMENTS XSINIL,Type
)

添加TYPEXML從內部查詢返回而不是字元串

DECLARE @AuditParameters XML = (
   SELECT
       1 AS AccountID,
       2 AS CategoryID,
       3 AS CategoryAttributeID,
       '4' AS SyncBatchGUID
   FOR XML PATH(N'Parameters'), ELEMENTS XSINIL, TYPE
)

引用自:https://dba.stackexchange.com/questions/259972