【问题标题】:sql server sp_executesql error with sql string带有sql字符串的sql server sp_executesql错误
【发布时间】:2011-10-04 17:51:57
【问题描述】:

我有一个如下所示的字符串:

set @sqlstring = N'select @mindate = min(time), @maxdate = max(time) from ' + @st_churn_active_table;

我打印它,它看起来像这样:

从derp.derp_table 中选择@mindate = min(time), @maxdate = max(time)

我使用如下参数定义运行 sp_executesql:

执行 sp_executesql @sqlstring, N'@maxdate date,@mindate date'

这样的错误:

参数化查询 '(@maxdate date,@mindate date)select @mindate = min(time), @maxda' 需要未提供的参数 '@maxdate'。

字符串变为
'(@maxdate date,@mindate date)select @mindate = min(time), @maxda'
sql字符串被切断了,有谁知道为什么以及如何解决这个问题?

谢谢!

【问题讨论】:

    标签: sql sql-server-2008 sp-executesql


    【解决方案1】:
    【解决方案2】:

    首先,sql server 看到了整个字符串。只有 错误消息 会截断它。请注意,即使截断的 sql 字符串只有部分名称,错误也正确获取了缺少参数的名称。

    其次,这不会按您希望的方式工作。 You can't use output parameters that way with sp_executesql.

    但最后是为什么它会抛出错误的问题。我不能确定,但​​我怀疑这里的问题是类型不匹配,所以它不能使用你给它的参数。我希望 sql server 在这种情况下会抛出更好的错误消息(抱怨类型不匹配),但是我没有方便的 sql server 来测试,我想不出任何其他原因导致问题。

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-12
      相关资源
      最近更新 更多