Can you contruct a variable name from another variable? For example, I want the following to PRINT 10
DECLARE @.var1 INT
DECLARE @.var2 INT
SET @.var1 = 10
SET @.var2 = 20
PRINT '@.var' + '1'
This prints the variable name, not the contents of the var. I tried to parse it with square brackets, but no luck.
Thanks,
CarlYou can do this using dynamic SQL, though I'm not sure it would work for the specific PRINT statement in your example.|||Ya, I need a varName in a var. A datatype of VARNAME. in some langs you just parse it correctly and it works, some it won't.
if you substitute EXEC for PRINT, it says you must declare variable 'var1', as you can see it obviously is being declared and interpretted the way it was typed.
Carl|||Actually, dynamic SQL will not work for this example, because the scope of the variables is limited to the procedure in which they are created.
Please explain what you are trying to do, and maybe we can come up with a satisfactory solution.|||I am just trying to simplify the code.
In stead of:
IF @.var1 < @.var2
SET @.var3 = DATEADD(n,GETDATE(), @.var1)
ELSE
SET @.var3 = DATEADD(n,GETDATE(), @.var2)
I was hoping to simply:
SET @.var3 = DATEADD(n,GETDATE(), @.var + @.CurrentVarIndex)
I already know which one I want, @.var1 or @.var2, it's just that I have to IF and have 2 lines to do the work of 1.|||... it's just that I have to IF and have 2 lines to do the work of 1.I've seen worse. Using dynamic SQL or some other hack work around to do this is going to end up making your code more complicated, not simpler.|||I know what you mean regarding the dynamic sql and "the sea of red". can make reviewing your code quite miserable.
Carl
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment