使用XML Query时,我遇到了dapper的问题。
此查询工作正常
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()="user@mail.com"]') = 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()=@position]'') = 1 OR
a.Workflow.exist(''//workflow/step/actor[@creator="true" and @mode="email" and text()=@email]'') = 1)
',N'@accountId nvarchar(4000),@position nvarchar(4000),@email nvarchar(4000)',@accountId=N'1234',@position=N'POS1',@email=N'user@mail.com'
以下是我用来执行查询的代码:
_dbConnection.Query<int>(toRenewSql, new { accountId = accountId, email = email, position = position })).First();
对电子邮件进行硬编码,位置使查询正常工作。
答案 0 :(得分:0)
我终于找到了问题,要在sql查询中使用sql参数,你必须在sql:variable中写入参数名称,如sql:variable(&#34; @Param&#34;) ;
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'user@mail.com'