var sidebar_align = 'right';
var content_container_margin = parseInt('290px');
var sidebar_width = parseInt('270px');
Unanswered: Construct Variable Name?
Can you contruct a variable name from another variable? For example, I want the following to PRINT 10
This prints the variable name, not the contents of the var. I tried to parse it with square brackets, but no luck.
DECLARE @var1 INT
DECLARE @var2 INT
SET @var1 = 10
SET @var2 = 20
PRINT '@var' + '1'
You 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.
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:
I was hoping to simply:
IF @var1 < @var2
SET @var3 = DATEADD(n,GETDATE(), @var1)
SET @var3 = DATEADD(n,GETDATE(), @var2)
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.
SET @var3 = DATEADD(n,GETDATE(), @var + @CurrentVarIndex)
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.
Originally Posted by carlmal
I know what you mean regarding the dynamic sql and "the sea of red". can make reviewing your code quite miserable.