存储过程动态 SQL 将结果存储在变量中

发布于 2024-12-11 00:04:16 字数 1399 浏览 0 评论 0原文

我在上面写了一个存储过程

DECLARE @AreaID AS INT
DECLARE @DayPrior AS INT
DECLARE @TableName AS VARCHAR(50)
DECLARE @StoreQuery AS NVARCHAR(MAX)
DECLARE @SQL_ExtractDlSql AS NVARCHAR(MAX)
DECLARE @ParameterDefinition AS NVARCHAR(2000)

SET @AreaID = 1
SET @DayPrior = 1
SET @TableName = 'Tbl_Lube'

SET @SQL_ExtractDlSql = 'SELECT Download_SQL From
                         HDDDataPackage.dbo.tbl_HDD_DataDownloadSetting
                         Where AreaId=@AreaID AND TableName=@TableName'

SET @ParameterDefinition = '@AreaID INT,@DayPrior INT,@TableName VARCHAR(50)'

EXECUTE sp_executesql @SQL_ExtractDlSql,
                      @ParameterDefinition,
                      @AreaID,
                      @DayPrior,
                      @TableName

PRINT @SQL_ExtractDlSql

存储过程,我得到了我想要的打印结果,但我没有这样做,而是希望将选定的数据存储作为变量。 然后我所做的是声明一个变量@StoreSql,并对 SQL 语句进行一些修改,如下所示:

变量声明:

DECLARE @StoreSql AS NVARCHAR(MAX)

修改代码:

SET @SQL_ExtractDlSql = 'SELECT @StoreSql = Download_SQL From
                         HDDDataPackage.dbo.tbl_HDD_DataDownloadSetting
                         Where AreaId=@AreaID AND TableName=@TableName'

我执行代码,出现错误必须声明标量变量“@StoreSql”。

我完全不知道如何处理这个问题。任何人都可以提供帮助。此外,我是 sqlserver 的新手,也是存储过程的新手。

请毫不犹豫地指出我的任何错误,任何错误……例如逻辑或等等。我从错误中学习。

问候 梁克

I have A stored procedure written

DECLARE @AreaID AS INT
DECLARE @DayPrior AS INT
DECLARE @TableName AS VARCHAR(50)
DECLARE @StoreQuery AS NVARCHAR(MAX)
DECLARE @SQL_ExtractDlSql AS NVARCHAR(MAX)
DECLARE @ParameterDefinition AS NVARCHAR(2000)

SET @AreaID = 1
SET @DayPrior = 1
SET @TableName = 'Tbl_Lube'

SET @SQL_ExtractDlSql = 'SELECT Download_SQL From
                         HDDDataPackage.dbo.tbl_HDD_DataDownloadSetting
                         Where AreaId=@AreaID AND TableName=@TableName'

SET @ParameterDefinition = '@AreaID INT,@DayPrior INT,@TableName VARCHAR(50)'

EXECUTE sp_executesql @SQL_ExtractDlSql,
                      @ParameterDefinition,
                      @AreaID,
                      @DayPrior,
                      @TableName

PRINT @SQL_ExtractDlSql

I above Stored procedure, I get What I wanted on print, but instead of doing that, I wanted the selected data stores as a variable.
Then what I done is I declared a variable @StoreSql and made some modified into the SQL statement show below:

Variable Declaration:

DECLARE @StoreSql AS NVARCHAR(MAX)

Modified the Code:

SET @SQL_ExtractDlSql = 'SELECT @StoreSql = Download_SQL From
                         HDDDataPackage.dbo.tbl_HDD_DataDownloadSetting
                         Where AreaId=@AreaID AND TableName=@TableName'

I executed the code, I get error Must declare the scalar variable "@StoreSql".

I have totally no idea how to deal with this.Any one can help.Beside,I am new to sqlserver,new to stored procedures.

Please don't hesitate to point out my any mistake,any..such as logic or etc. I learn from mistake.

Regards
LiangCk

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

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

发布评论

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

评论(1

泪冰清 2024-12-18 00:04:16

动态 SQL 字符串无法引用外部作用域中的变量。您需要将其声明为 output 参数

SET @ParameterDefinition = '@AreaID INT,
                            @DayPrior INT,
                            @TableName VARCHAR(50), 
                            @StoreSql NVARCHAR(MAX) OUTPUT'

EXECUTE sp_executesql @SQL_ExtractDlSql,
                      @ParameterDefinition,
                      @AreaID,
                      @DayPrior,
                      @TableName,
                      @StoreSql OUTPUT

The dynamic SQL string cannot reference variables from the outer scope. You need to declare it as an output parameter

SET @ParameterDefinition = '@AreaID INT,
                            @DayPrior INT,
                            @TableName VARCHAR(50), 
                            @StoreSql NVARCHAR(MAX) OUTPUT'

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