2016-11-21 7 views
0

XMLクエリを処理するときに問題が発生しました。Dapper sqlパラメータがxmlクエリで機能しない

このクエリは

SELECT COUNT(1) 
FROM Agreements a INNER JOIN 
    AgreementParts ap ON ap.AgreementId = a.Id 
WHERE a.ToRenew = 1 
AND ap.AccountId = N'1234' 
AND (a.Workflow.exist('//workflow/step/actor[@creator="true" and @mode="position" and text()="POS1"]') = 1 OR 
    a.Workflow.exist('//workflow/step/actor[@creator="true" and @mode="email" and text()="[email protected]"]') = 1) 

細かい作業しかし、Dapperのとパラメータを使用してこのクエリを呼び出すには、ここで0

exec sp_executesql N' 
SELECT COUNT(1) 
FROM Agreements a INNER JOIN 
    AgreementParts ap ON ap.AgreementId = a.Id 
WHERE a.ToRenew = 1 
AND ap.AccountId = @accountId 
AND (a.Workflow.exist(''//workflow/step/actor[@creator="true" and @mode="position" and text()[email protected]]'') = 1 OR 
    a.Workflow.exist(''//workflow/step/actor[@creator="true" and @mode="email" and text()[email protected]]'') = 1) 
',N'@accountId nvarchar(4000),@position nvarchar(4000),@email nvarchar(4000)',@accountId=N'1234',@position=N'POS1',@email=N'[email protected]' 

常に返す私は、クエリを実行するために使用するコードです:

_dbConnection.Query<int>(toRenewSql, new { accountId = accountId, email = email, position = position })).First(); 

電子メールをハードコーディングすると、その位置によってクエリが機能します。

答えて

0

sql:variable( "@ Param");のようなsql:変数内にパラメータ名を記述する必要があります。

exec sp_executesql N' 
SELECT COUNT(1) 
FROM Agreements a INNER JOIN 
    AgreementParts ap ON ap.AgreementId = a.Id 
WHERE a.ToRenew = 1 
AND ap.AccountId = @accountId 
AND (a.Workflow.exist(''//workflow/step/actor[@creator="true" and @mode="position" and text()=sql:variable("@position")]'') = 1 OR 
a.Workflow.exist(''//workflow/step/actor[@creator="true" and @mode="email" and text()=sql:variable("@email")]'') = 1) 
',N'@accountId nvarchar(4000),@position nvarchar(4000),@email nvarchar(4000)',@accountId=N'1234',@position=N'POS1',@email=N'[email protected]' 
関連する問題