“sp_executesql”附近​​的语法不正确

发布于 2024-08-01 19:11:09 字数 270 浏览 3 评论 0原文

我不明白为什么以下会给我错误。 我认为这与注释掉的部分有关,但@SQL是nvarchar(4000)。

BEGIN
  sp_executesql N'SELECT ''td'''
  --sp_executesql @SQL, N'@StartDate DateTime, @EndDate DateTime, @End2 DateTime, @Program varchar(4)', @StartDate, @EndDate, @End2, @Program
END

I don't understand why the following is giving me the error. I thought it was related to the commented out section, but @SQL is nvarchar(4000).

BEGIN
  sp_executesql N'SELECT ''td'''
  --sp_executesql @SQL, N'@StartDate DateTime, @EndDate DateTime, @End2 DateTime, @Program varchar(4)', @StartDate, @EndDate, @End2, @Program
END

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莫多说 2024-08-08 19:11:09

这就是为什么:

-- This works just fine:
BEGIN
  -- You must have an exec before your sp_executesql or it will not work in a block
  exec sp_executesql N'SELECT ''td'''
END

当您位于块中时,您不能在没有 exec 的情况下只调用存储过程。

This is why:

-- This works just fine:
BEGIN
  -- You must have an exec before your sp_executesql or it will not work in a block
  exec sp_executesql N'SELECT ''td'''
END

You can't just call a stored proc without an exec when you are in a block.

墨小墨 2024-08-08 19:11:09

为什么将其包含在 BEGIN ... END 中? 在块外部运行 sp_executesql 将起作用。

您可以选择在 sp_executesql 之前放置 exec

Why do you have this enclosed in a BEGIN ... END? Running the sp_executesql external the block will work.

Optionally you can put an exec before sp_executesql.

你穿错了嫁妆 2024-08-08 19:11:09

在某些情况下,我也必须使用 master:

exec master..sp_executesql 

On some occasions I had to use master as well:

exec master..sp_executesql 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文