如何正确使用输出参数作为变量
how to properly use output parameter as variable
我有这个代码
DECLARE @columnName varchar(25)
DECLARE @cursor CURSOR
DECLARE @sql nvarchar(1000)
DECLARE @tableName as nvarchar(30)
DECLARE @PartNo as nvarchar(30)
DECLARE @value as integer
set @PartNo = 'partnumber'
set @tableName = 'sometable'
SET @cursor = CURSOR FOR
SELECT c.name
FROM sys.columns c
INNER JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.object_id = (SELECT object_id FROM sys.tables
WHERE name = @tableName)
AND c.name NOT IN ('something')
OPEN @cursor
FETCH NEXT FROM @cursor INTO @columnName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = 'select sum('+@columnName+') from checktime where partNo = '''+@PartNo+''''
EXEC sp_executesql @sql, N'@columnName varchar(25), @PartNo nvarchar(30), @value int OUTPUT',
@columnName,
@PartNo,
@value OUTPUT
FETCH NEXT FROM @cursor INTO @columnName
END
CLOSE @cursor
DEALLOCATE @cursor
这段代码的问题是,即使我不想输出变量,它也已经输出了。我想使用这样的代码
DECLARE @columnName varchar(25)
DECLARE @cursor CURSOR
DECLARE @sql nvarchar(1000)
DECLARE @tableName as nvarchar(30)
DECLARE @PartNo as nvarchar(30)
DECLARE @value as integer
set @PartNo = 'partnumber'
set @tableName = 'sometable'
SET @cursor = CURSOR FOR
select c.name from sys.columns c inner join sys.tables t on c.object_id = t.object_id
where c.object_id = (select object_id from sys.tables where name = @tableName )
and c.name not in ('something')
OPEN @cursor
FETCH NEXT
FROM @cursor INTO @columnName
WHILE @@FETCH_STATUS = 0
BEGIN
set @sql = 'select sum('+@columnName+') from checktime where partNo = '''+@PartNo+''''
EXEC sp_executesql @sql, N'@columnName varchar(25), @PartNo nvarchar(30), @value int OUTPUT',
@columnName,
@PartNo,
@value OUTPUT
select case when @value > 0 then 'YEY' end
FETCH NEXT
FROM @cursor INTO @columnName
END
CLOSE @cursor
DEALLOCATE @cursor
我正在创建一个存储过程,用于搜索列名是否为 NULL,如果是,我会将其放入我的 select 语句中。我正在构建一个动态 SQL,其中输出将只生成所有不为空的表,但是这个任务,我将自己创建它,我只想知道如何操作 OUTPUT 变量。
J
在您的 sp_executesql 中,@value 参数未以任何方式使用。这是没用的。所以你可以修改你的初始如下
set @sql = 'select sum('+@columnName+'), 5 from CursorQ where partNo = '''+@PartNo+''' having sum('+@columnName+') > 0'
EXEC sp_executesql @sql, N'@columnName varchar(25), @PartNo nvarchar(30)',
@columnName,
@PartNo--, @value OUTPUT
你看我已经放置了一个带有 HAVING 子句的过滤条件
另一个选项是使用@value 参数。您可以修改脚本以使用@value,如下所示
set @sql = 'set @value = (select sum('+@columnName+') from CursorQ where partNo = '''+@PartNo+''')'
EXEC sp_executesql @sql, N'@columnName varchar(25), @PartNo nvarchar(30), @value int OUTPUT',
@columnName,
@PartNo,
@value OUTPUT
if isnull(@value,0)>0
select @value where isnull(@value,0)>0
你可以意识到我将 SUM 值分配给输出然后在下面 SELECT 我使用了一个 IF 子句检查 @value 是否不为空且大于零(在 SELECT WHERE 子句是多余的,但您可以更喜欢使用 WHERE 而不是 IF)
我有这个代码
DECLARE @columnName varchar(25)
DECLARE @cursor CURSOR
DECLARE @sql nvarchar(1000)
DECLARE @tableName as nvarchar(30)
DECLARE @PartNo as nvarchar(30)
DECLARE @value as integer
set @PartNo = 'partnumber'
set @tableName = 'sometable'
SET @cursor = CURSOR FOR
SELECT c.name
FROM sys.columns c
INNER JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.object_id = (SELECT object_id FROM sys.tables
WHERE name = @tableName)
AND c.name NOT IN ('something')
OPEN @cursor
FETCH NEXT FROM @cursor INTO @columnName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = 'select sum('+@columnName+') from checktime where partNo = '''+@PartNo+''''
EXEC sp_executesql @sql, N'@columnName varchar(25), @PartNo nvarchar(30), @value int OUTPUT',
@columnName,
@PartNo,
@value OUTPUT
FETCH NEXT FROM @cursor INTO @columnName
END
CLOSE @cursor
DEALLOCATE @cursor
这段代码的问题是,即使我不想输出变量,它也已经输出了。我想使用这样的代码
DECLARE @columnName varchar(25)
DECLARE @cursor CURSOR
DECLARE @sql nvarchar(1000)
DECLARE @tableName as nvarchar(30)
DECLARE @PartNo as nvarchar(30)
DECLARE @value as integer
set @PartNo = 'partnumber'
set @tableName = 'sometable'
SET @cursor = CURSOR FOR
select c.name from sys.columns c inner join sys.tables t on c.object_id = t.object_id
where c.object_id = (select object_id from sys.tables where name = @tableName )
and c.name not in ('something')
OPEN @cursor
FETCH NEXT
FROM @cursor INTO @columnName
WHILE @@FETCH_STATUS = 0
BEGIN
set @sql = 'select sum('+@columnName+') from checktime where partNo = '''+@PartNo+''''
EXEC sp_executesql @sql, N'@columnName varchar(25), @PartNo nvarchar(30), @value int OUTPUT',
@columnName,
@PartNo,
@value OUTPUT
select case when @value > 0 then 'YEY' end
FETCH NEXT
FROM @cursor INTO @columnName
END
CLOSE @cursor
DEALLOCATE @cursor
我正在创建一个存储过程,用于搜索列名是否为 NULL,如果是,我会将其放入我的 select 语句中。我正在构建一个动态 SQL,其中输出将只生成所有不为空的表,但是这个任务,我将自己创建它,我只想知道如何操作 OUTPUT 变量。
J
在您的 sp_executesql 中,@value 参数未以任何方式使用。这是没用的。所以你可以修改你的初始如下
set @sql = 'select sum('+@columnName+'), 5 from CursorQ where partNo = '''+@PartNo+''' having sum('+@columnName+') > 0'
EXEC sp_executesql @sql, N'@columnName varchar(25), @PartNo nvarchar(30)',
@columnName,
@PartNo--, @value OUTPUT
你看我已经放置了一个带有 HAVING 子句的过滤条件
另一个选项是使用@value 参数。您可以修改脚本以使用@value,如下所示
set @sql = 'set @value = (select sum('+@columnName+') from CursorQ where partNo = '''+@PartNo+''')'
EXEC sp_executesql @sql, N'@columnName varchar(25), @PartNo nvarchar(30), @value int OUTPUT',
@columnName,
@PartNo,
@value OUTPUT
if isnull(@value,0)>0
select @value where isnull(@value,0)>0
你可以意识到我将 SUM 值分配给输出然后在下面 SELECT 我使用了一个 IF 子句检查 @value 是否不为空且大于零(在 SELECT WHERE 子句是多余的,但您可以更喜欢使用 WHERE 而不是 IF)