这就是我想要做的事情
Declare @Var varchar(50)
Declare @Num1 varchar(50)
Declare @Num2 varchar(50)
Declare @Counter smallint
Set @Counter=1
Set @Num1='Hello'
Set @Num2='Hi'
while (@Counter<2)
begin
Set @Var=N'@Num'+convert(varchar,@Counter)
//Now I want to get the value of '@Num1' that is stored in @Var when @Counter=1
//Help Needed
Set @Counter=@Counter+1
end
答案 0 :(得分:2)
这对你的问题不是一个非常优雅的解决方案,但至少它解决了它:)
Declare @Var varchar(50)
Declare @Num1 varchar(50)
Declare @Num2 varchar(50)
Declare @Counter smallint
Declare @text varchar(50)
Set @Counter=1
Set @Num1='Hello'
Set @Num2='Hi'
while (@Counter<3)
begin
SET @text =
CASE @Counter
WHEN 1 THEN @Num1
WHEN 2 THEN @Num2
END
Set @Counter=@Counter+1
print @text
end
答案 1 :(得分:1)
AFAIK,你不能直接这样做;有什么不妥之处:
SET @Var = SELECT CASE @Counter WHEN 1 THEN @Num1 ELSE @Num2 END
如果您有很多值,请考虑使用#table
(临时表)或@table
(表变量) - 即
DECLARE @Data TABLE ([Key] int NOT NULL, [Value] varchar(50))
然后只需INSERT
/ UPDATE
进入@DATA
SELECT @Var = [Value] FROM @Data WHERE [Key] = @Counter
答案 2 :(得分:-1)
@ Num1的值将始终为“Hello”。 原因是你只在@Var中存储一个字符串。