我想请求您就此事提供帮助。
我需要一个可以更新其中一个表的列的变量。
我遇到的第一个问题是我需要从一系列列中检索数据,检查这些列是否为数字。
为了解决这个问题,我使用了TSQL,效果很好。现在,我似乎走到了尽头。我需要检索此过程的结果。我尝试将其转换为函数,但是根据许多试验(以及一些谷歌搜索)似乎TSQL不能用于here所述的函数,所以我坚持使用存储过程,但是如何检索结果?我试图使用OUTPUT参数,但是我遇到了这个错误
The formal parameter "@R" was not declared as an OUTPUT parameter, but the actual parameter passed in requested output.
即使我将R
声明为输出参数,我也声明@R
在我的sp_executesql
事务中输出结果,但我仍然得到错误,请问是什么我做错了吗?
请保存,存储过程正常,我只需要输出。谢谢。
ALTER procedure [dbo].[SaveRinHead]
@SumNo as nvarchar(15)
,@R as decimal(18,3) output
as
declare @cursor CURSOR
declare @colname as integer
declare @top as integer
declare @query as nvarchar(MAX)
declare @TSQL as nvarchar(MAX)
declare @topass as nvarchar(MAX) = ''
declare @DimItem as nvarchar(10)
set @DimItem = (select distinct dimitem from SumNo)
SET @cursor = CURSOR FOR
(select cast([Name] as decimal(18,0)) from sys.columns where object_id in (select object_id from sys.tables where [name] = 'ADetails' )and [Name] in ('1','2','3','4','5','6','7','8','9','10'))order by [Name] asc
OPEN @cursor
FETCH NEXT
FROM @cursor INTO @colname
WHILE @@FETCH_STATUS = 0
BEGIN
set @top = (select CASE WHEN Isnumeric(@colname) = 1
THEN CONVERT(int,@colname)
ELSE 0 END AS COLUMNA)
if @top <= '5'
BEGIN
set @query = '(['+cast(@top as nvarchar(10)) + ']) ,'
set @topass = rtrim(ltrim(@topass)) +' '+rtrim(ltrim(@query))
END
FETCH NEXT
FROM @cursor INTO @colName
END
CLOSE @cursor
DEALLOCATE @cursor
set @topass = (SELECT SUBSTRING(@topass,1, len(@topass)-1))
begin
set @TSQL = '
SELECT @R = (MAX(MaxValue) - MIN(MinValue)) FROM ADetails
CROSS APPLY (SELECT MIN(d) MinValue FROM (VALUES '+@topass+' ) AS a(d)) X
CROSS APPLY (SELECT MAX(d) MaxValue FROM (VALUES '+@topass+' ) AS a(d)) Y
where SumNo= @SumNo'
exec sp_executesql @TSQL, N'@DimItem nvarchar(10), @R decimal(18,3), @SumNo nvarchar(15)', @DimItem, @R output, @SumNo
update ADetails set R = @R where SumNo= @SumNo
end
答案 0 :(得分:2)
根据docs,您需要在调用output
时在两者参数声明和参数列表中指定sp_executesql
关键字:
exec sp_executesql @TSQL, N'@DimItem nvarchar(10), @R decimal(18,3) output, @SumNo nvarchar(15)',
@DimItem, @R output, @SumNo;