robertkjr3d
Programmer
Ok here is the simple version of what my problem is:
declare @Field int
declare @Field2 int
declare @sql varchar(150)
set @Field = 2
set @Field2 = 2000
set @sql = 'select @Field' + Convert(varchar, @Field) + ' [Value]'
exec(@sql)
I get the error must declare the variable @Field2
It doesn't remember.
Why do I need this really is because of this situation:
(All of these variables are declared)
set @loop = 1
set @visits = 0
set @Field = 11
--Collect visits
while @loop = 1 and @Field <= 68 begin
set @loop = 0
set @command = 'if (@Field' + Convert(varchar, @Field) + ' is not null and @Field' + Convert(varchar, @Field + 1) + ' = ''T'' and @Field' + Convert(varchar, @Field + 2) + ' is not null) begin set @loop = 1 set @visits = @visits + 1 end'
exec(@command)
set @Field = @Field + 3
end
However I get the error, @loop, @visits, @Field not declared.
Is there anyway to handle a @ variable without knowing the complete name of it? Because I have variables passed to the procedure in the form of @Field1, @Field2, @Field3... and so on
declare @Field int
declare @Field2 int
declare @sql varchar(150)
set @Field = 2
set @Field2 = 2000
set @sql = 'select @Field' + Convert(varchar, @Field) + ' [Value]'
exec(@sql)
I get the error must declare the variable @Field2
It doesn't remember.
Why do I need this really is because of this situation:
(All of these variables are declared)
set @loop = 1
set @visits = 0
set @Field = 11
--Collect visits
while @loop = 1 and @Field <= 68 begin
set @loop = 0
set @command = 'if (@Field' + Convert(varchar, @Field) + ' is not null and @Field' + Convert(varchar, @Field + 1) + ' = ''T'' and @Field' + Convert(varchar, @Field + 2) + ' is not null) begin set @loop = 1 set @visits = @visits + 1 end'
exec(@command)
set @Field = @Field + 3
end
However I get the error, @loop, @visits, @Field not declared.
Is there anyway to handle a @ variable without knowing the complete name of it? Because I have variables passed to the procedure in the form of @Field1, @Field2, @Field3... and so on