Search This Blog

Monday, July 30, 2012

SQL SELECT multi-columns INTO multi-variable

SQL Server > Scripts >  SELECT multi columns INTO multi variables

Example:


CREATE TABLE #tmp
(
    id       int PRIMARY KEY,
    name     varchar(20),
    age             int
)
GO


INSERT #tmp(id, name, age) VALUES (1, 'John',20)
INSERT #tmp(id, name, age) VALUES (2, 'Dan',40)
GO

declare @minage int,
        @maxage int
SELECT
   @minage = min(age),
   @maxage = max(age)
FROM
   #tmp
select
   @minage as minage, @maxage as maxage

drop table #tmp