Here is an example of selecting data from a table with a query directly into a variable:
DECLARE @TaxRateMax smallmoney,
@TaxRateMin smallmoney,
@TaxRateAvg smallmoney;
SELECT @TaxRateMax = MAX (TaxRate),
@TaxRateMin = MIN (TaxRate),
@TaxRateAvg = AVG (TaxRate)
FROM Sales.SalesTaxRate;
SELECT @TaxRateMax AS TaxRateMax,
@TaxRateMin AS TaxRateMin,
@TaxRateAvg AS TaxRateAvg;
To insert data into a table variable:
DECLARE @TempTaxRate TABLE (
StateProvinceID int,
TaxType tinyint,
TaxRate smallmoney,
Name nvarchar(50));
INSERT INTO
@TempTaxRate
SELECT
StateProvinceID,
TaxType,
TaxRate,
Name
FROM
Sales.SalesTaxRate
WHERE
TaxRate > 10;
SELECT * FROM @TempTaxRate