简体   繁体   English

参数化动态sql不返回任何结果

[英]Parameterized dynamic sql returns no results

From a C# application I create the following parameterized dynamic sql statement as captured by SQL profiler: 在C#应用程序中,我创建了SQL profiler捕获的以下参数化动态sql语句:

Executing this statement returns no results: 执行此语句不会返回任何结果:

exec sp_executesql N'SELECT IDWebFormAnswer FROM WebFormAnswers WHERE IDWebform = 55 AND IDWebFormQuestion = 478 AND (ANSWER = ''@answer0'')', N'@answer0 nvarchar(2)', @answer0=N'XL'

However, if I simply replace @answer0 with XL in the following, I get 4 rows returned. 但是,如果我在下面简单地用XL替换@ answer0,我会返回4行。

exec sp_executesql N'SELECT IDWebFormAnswer FROM WebFormAnswers WHERE IDWebform = 55 AND IDWebFormQuestion = 478 AND (ANSWER = ''XL'')', N'@answer0 nvarchar(2)', @answer0=N'XL'

I do not understand why this happens? 我不明白为什么会这样? Am I building the query wrong? 我构建查询错了吗?

When you use a parameter you should not enclose it in quotes. 使用参数时,不应将其括在引号中。 If you do that then the parameter name becomes a literal string. 如果这样做,那么参数名称将成为文字字符串。 With your code the query search for an ANSWER that contains the value '@Answer0' and there is none. 使用您的代码,查询将搜索包含值“@ Answer0”的ANSWER,但没有。

 exec sp_executesql N'SELECT IDWebFormAnswer FROM WebFormAnswers 
      WHERE IDWebform = 55 AND IDWebFormQuestion = 478 AND 
      (ANSWER = @answer0)', N'@answer0 nvarchar(2)', @answer0=N'XL'

Steve already answered it well so I will try and give some tips that I picked up through my several failures while working with Dynamic SQL. Steve已经很好地回答了这个问题,所以我会尝试提供一些技巧,这些技巧是我在使用Dynamic SQL时遇到的几个故障。 Hope it is useful. 希望它有用。

First write out your query as such with all parameter declaration 首先用所有参数声明写出你的查询

DECLARE @answer0 nvarchar(2)

SELECT IDWebFormAnswer
FROM WebFormAnswers
WHERE IDWebform = '55'
  AND IDWebFormQuestion = '478'
  AND (ANSWER = @answer0)

Now replace all single quotes with 2 Single quotes (I use CTRL+H, replace ' with '') 现在用2个单引号替换所有单引号(我使用CTRL + H,替换'with'')

DECLARE @answer0 nvarchar(2)

SELECT IDWebFormAnswer
FROM WebFormAnswers
WHERE IDWebform = ''55''
  AND IDWebFormQuestion = ''478''
  AND (ANSWER = @answer0)

Next Break this query down and encapsulate with leading and trailing Single quotes and integrate with sp_executesql syntax. 下一步中断此查询并使用前导和尾随单引号进行封装,并与sp_executesql语法集成。

DECLARE @SQLString nvarchar(500),
        @ParmDefinition nvarchar(500),
        @ParmValue1 nvarchar(2);

 /* Build the SQL string one time.*/
SET @SQLString = N'SELECT IDWebFormAnswer
                   FROM WebFormAnswers
                   WHERE IDWebform = ''55''
                     AND IDWebFormQuestion = ''478''
                     AND (ANSWER = @answer0)';


SET @ParmDefinition = N'@answer0 nvarchar(2)';

 /* Execute the string with the first parameter value. */
SET @ParmValue1 = N'XL';

EXECUTE sp_executesql @SQLString, @ParmDefinition,
                       @answer0 = @ParmValue1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM