您好我有一个SP,我在其中创建一个临时表来存储一些值。
ALTER PROCEDURE [dbo].[test]
@id int,
@funds_limit money
AS
BEGIN
SET NOCOUNT ON;
DECLARE @threshold money;
CREATE TABLE #ConfigurationTemp
(id int,
name varchar(100) not null,
type varchar(100),
value varchar(100))
INSERT #ConfigurationTemp EXEC get_config @id, 'testType', null
select @threshold = value
from #ConfigurationTemp
where id=@id and name='testLimit'
print @threshold
IF (@funds_limit IS NOT NULL) AND (@threshold < @funds_limit)
BEGIN
DROP TABLE #ConfigurationTemp;
RETURN 1000;
END
select @threshold = value
from #ConfigurationTemp
where id=@id and name='testLimit1'
print @threshold
IF (@funds_limit IS NOT NULL) AND (@threshold < @funds_limit)
BEGIN
DROP TABLE #ConfigurationTemp;
RETURN 1001;
END
END
RETURN 0;
END
临时表有多行。 例如:
1, fund_limit, testType, 10
2, fund_min_limit, testType, 20
我需要首先使用用户输入值(它将是SP的输入参数)验证 fund_limit (10)的值。如果验证失败,我将返回错误代码。如果没有,我会去下一次检查。即 fund_min_limit 。我对它做同样的事情并返回一个不同的错误代码。如果没有验证失败,我将返回0,这被认为是成功的。
在这种情况下,我总是得到相同的阈值。即,第一行的值...... 10.
如何从临时表中获取与名称不同的阈值?
答案 0 :(得分:1)
当您使用select
分配标量变量时 - 如果此选择返回零行,则可能未分配(未更改 - 可能保留先前分配的值)。为确保您的变量已更改,它的值会将其重写为set
表达式。
因此,如果您拼写错误的第二个阈值名称,您可能会&#34;得到&#34;相同的@threshold值,因为第二个语句没有为您的变量赋值,即变量包含先前赋值(select)的值。您可以使用附加变量测试第二个阈值 - 如果我猜到了问题原因,它将始终为NULL。
此外,您正在应用相同的@id
过滤器,这是一个标量变量。但是你的行有不同的ID。因此,现在没有机会获得任何其他阈值的值,而不是@id给出的值。
set @threshold = (select t.value
from #ConfigurationTemp t
where t.name='testLimit')
print @threshold
IF @threshold < @funds_limit
RETURN 1000;
set @threshold = (select t.value
from #ConfigurationTemp t
where t.name='testLimit 2')
print @threshold
IF @threshold < @funds_limit
RETURN 1001;
只有当两个参数都是非NULL时, If
才会成功。
另一种方法:
declare
@threshold_a int,
@threshold_b int,
@threshold_c int
;with test as
(
select 'a' as name, 25 as value
union all
select 'b', 3
union all
select 'c', 100
union all
select 'd', -1
)
select
@threshold_a = case when t.name = 'a' then t.value else @threshold_a end,
@threshold_b = case when t.name = 'b' then t.value else @threshold_b end,
@threshold_c = case when t.name = 'c' then t.value else @threshold_c end
from test t
select
@threshold_a as [a],
@threshold_b as [b],
@threshold_c as [c]
GO
单选,多个变量。
答案 1 :(得分:0)
您的RETURN
声明中有IF
。
... RETURN 1000
and
... RETURN 1001
插入一行后,程序结束。
也许您想将结果分配给变量
@return_Value = ''
@return_Value = @return_Value + '1000, '
....
@return_Value = @return_Value + '1001, '
RETURN @return_Value