Use aggregate function with variables : Variable « Transact SQL « SQL Server / T-SQL Tutorial






3> CREATE TABLE roysched(
4>    title_id       varchar(20),
5>    lorange        int                   NULL,
6>    hirange        int                   NULL,
7>    royalty        int                   NULL
8> )
9> GO
1>
2>
3> insert roysched values('1', 0, 10000, 10)
4> insert roysched values('2', 10001, 20000, 12)
5> insert roysched values('3', 20001, 30000, 14)
6> insert roysched values('4', 30001, 40000, 16)
7> insert roysched values('5', 40001, 50000, 18)
8>
9> GO

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)
1>
2>
3> DECLARE @min_range int, @hi_range int          -- Variables declared
4> SELECT  @min_range=MIN(lorange),
5>         @hi_range=MAX(hirange) FROM roysched   -- Variables assigned
6> SELECT  @min_range, @hi_range                  -- Values of variables
7>                                                --  returned as result
8>
9> GO

----------- -----------
          0       50000

(1 rows affected)
1>
2> drop table roysched;
3> GO








20.2.Variable
20.2.1.Working with Variables
20.2.2.Use right function with a variable
20.2.3.Testing the ISNULL function
20.2.4.SELECT LEFT(@FullName, 5)
20.2.5.Global variable names begin with an @@ prefix.
20.2.6.XML type variable
20.2.7.Varchar type variable
20.2.8.Use if statement to check a variable
20.2.9.A variable can be assigned a value from a subquery
20.2.10.Use aggregate function with variables
20.2.11.Use Local Variables in where clause
20.2.12.A SQL script that uses variables
20.2.13.Local variables used for searches
20.2.14.Pass variable to a user-defined function
20.2.15.Use one variables in like function