I have three values and I have to find MAX
value from these values
Example:
DECLARE @r int=10
DECLARE @s int=15
DECLARE @t int=50
SELECT MAX(@r, @s, @t)
I have to find 50 like this
DECLARE @r int=10, @s int=15, @t int=50
SELECT max(val)
from (values(@r),(@s),(@t)) X(val)
You should be using a code like this:
DECLARE @temp TABLE (a INTEGER)
INSERT INTO @temp VALUES(1)
INSERT INTO @temp VALUES(2)
INSERT INTO @temp VALUES(3)
SELECT MAX(a) FROM @temp
You can't run MAX
on more variables. MAX()
takes only 1 parameter, the column name in a table.
But, if you have to use the variables, then:
DECLARE @temp TABLE(col1 INTEGER)
DECLARE @r int=10
DECLARE @s int=15
DECLARE @t int=50
INSERT INTO @temp(col1) values(@r)
INSERT INTO @temp(col1) values(@s)
INSERT INTO @temp(col1) values(@t)
SELECT MAX(col1) FROM @temp
You should not waste resources with temporary table solutions.
Just make an union of all those variables.
DECLARE @r int=10
DECLARE @s int=15
DECLARE @t int=50
SELECT max(t.value)
from (select @r as value
union
select @s as value
union
select @t as value) t
declare @tmpTable table (value integer)
, insert your values to temp table and selectmax(value)