将动态 SQL 的输出分配给变量(没有存储过程)

发布于 2024-11-28 06:59:39 字数 643 浏览 0 评论 0原文

我有下面的代码,我想通过动态 SQL 获取行的 id。

请注意,变量 @posselect@catselect 已填充,但我将其保留以进行概述。

declare @SQL nvarchar(1000)
declare @posselect nvarchar(50)
declare @catselect nvarchar(100)

declare @bannerid int

SET @SQL = 'SELECT TOP 1 @id = id FROM banners
            WHERE publishdate < GETDATE() 
            AND depublishdate > GETDATE()' + @posselect + @catselect + 
           'ORDER BY [views] ASC'

EXEC sp_executesql @SQL, @bannerid = @id output  'on this line I get the error

print '@bannerid:'+STR(@bannerid)

我收到错误:

必须声明标量变量“@id”。

I have the code below, where I want to get the id of a row via dynamic SQL.

Please note that variables @posselect and @catselect are filled, but I left that out for overview.

declare @SQL nvarchar(1000)
declare @posselect nvarchar(50)
declare @catselect nvarchar(100)

declare @bannerid int

SET @SQL = 'SELECT TOP 1 @id = id FROM banners
            WHERE publishdate < GETDATE() 
            AND depublishdate > GETDATE()' + @posselect + @catselect + 
           'ORDER BY [views] ASC'

EXEC sp_executesql @SQL, @bannerid = @id output  'on this line I get the error

print '@bannerid:'+STR(@bannerid)

I get the error:

Must declare the scalar variable "@id".

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

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

发布评论

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

评论(3

彩扇题诗 2024-12-05 06:59:39

您的代码未显示您正在设置 @id 字段

declare @SQL nvarchar(1000)
declare @posselect nvarchar(50)
declare @catselect nvarchar(100)

declare @id int
declare @bannerid int

set @SQL='SELECT TOP 1 @id=id FROM banners
WHERE publishdate<GETDATE() AND depublishdate>GETDATE()' + @posselect + @catselect + '
ORDER BY [views] asc'

EXEC sp_executesql @SQL,@bannerid=@id output  'on this line I get the error

print '@bannerid:'+STR(@bannerid)

Your code doesn't show that you are setting your @id field

declare @SQL nvarchar(1000)
declare @posselect nvarchar(50)
declare @catselect nvarchar(100)

declare @id int
declare @bannerid int

set @SQL='SELECT TOP 1 @id=id FROM banners
WHERE publishdate<GETDATE() AND depublishdate>GETDATE()' + @posselect + @catselect + '
ORDER BY [views] asc'

EXEC sp_executesql @SQL,@bannerid=@id output  'on this line I get the error

print '@bannerid:'+STR(@bannerid)
剪不断理还乱 2024-12-05 06:59:39

我可能遗漏了一些东西,但我看不到您在哪里声明 @id (如错误消息所示)。

尝试将其更改为这样:

declare @SQL nvarchar(1000) declare @posselect nvarchar(50) declare @catselect nvarchar(100)

declare @bannerid int
declare @id int

set @SQL='SELECT TOP 1 @id=id FROM banners WHERE publishdateGETDATE()' + @posselect + @catselect + ' ORDER BY [views] asc'

EXEC sp_executesql @SQL,@bannerid=@id output 'on this line I get the error

print '@bannerid:'+STR(@bannerid)

I might be missing something but I don't see where you are declaring @id (as the error message suggests).

Try changing it to this:

declare @SQL nvarchar(1000) declare @posselect nvarchar(50) declare @catselect nvarchar(100)

declare @bannerid int
declare @id int

set @SQL='SELECT TOP 1 @id=id FROM banners WHERE publishdateGETDATE()' + @posselect + @catselect + ' ORDER BY [views] asc'

EXEC sp_executesql @SQL,@bannerid=@id output 'on this line I get the error

print '@bannerid:'+STR(@bannerid)
岁月苍老的讽刺 2024-12-05 06:59:39

尝试一下

DECLARE @params NVARCHAR(128) = N'@id int output';

EXEC sp_executesql @SQL, @params, @id=@bannerid output   

,我相信这是正确的语法。

Try

DECLARE @params NVARCHAR(128) = N'@id int output';

EXEC sp_executesql @SQL, @params, @id=@bannerid output   

I believe that's the correct syntax.

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