I have a stored procedure to which I pass 4 variables, @DCUId,@SlaveAddress,@DateFrom,@DateTo
Within the SP I have the following:
DECLARE @MinAmbient decimal (4,1),@AverageAmbient decimal (4,1)
DECLARE @MaxAmbient decimal (4,1)
SELECT @MaxAmbient = MAX(CAST(T4 AS DECIMAL))/10,
@MinAmbient = MIN(CAST(T4 AS DECIMAL))/10,
@AverageAmbient = AVG(CAST(T4 AS DECIMAL))/10
FROM RECORDEDDETAIL WHERE DCUId = CONVERT(nvarchar(4),@DCUId)
AND SlaveId = @SlaveAddress
AND timestamp BETWEEN convert(nvarchar(20), @DateFrom,113)
AND convert(nvarchar(20), @DateTo,113)
which works fine.
I want to make the column value 'T4' a variable which I can pass in ie.
DECLARE @TLink nvarchar(3)
SET @TLink = 'T4'
then do something like
SELECT @MaxAmbient = MAX(CAST(@TLink AS DECIMAL))/10,
@MinAmbient = MIN(CAST(T4 AS DECIMAL))/10,.....
.......... etc
When I do this I get an error ...
What am I doing wrong?
Thanks
Roger
To further clarify exactly what I need to achieve.
The column T4 refers to a temperature - this temperature value can come from one of ten probes. The "mapping" of the probe to temperature column is done via another table. So the column value could be Tx where x is between 1 and 10.
So before this I determine the mapping value
DECLARE @TLink nvarchar(3) SELECT @TLink = TempLinks FROM DCUConfigurations WHERE DCUID = @DCUId