Declare a table type variable and query it : Declare « Transact SQL « SQL Server / T-SQL Tutorial






7> CREATE TABLE Orders (
8>      OrderID int NOT NULL ,
9>      CustomerID nchar (5) NULL ,
10>     EmployeeID int NULL ,
11>     OrderDate datetime NULL ,
12>     RequiredDate datetime NULL ,
13>     ShippedDate datetime NULL ,
14>     ShipVia int NULL ,
15>     Freight money NULL DEFAULT (0),
16>     ShipName nvarchar (40) NULL ,
17>     ShipAddress nvarchar (60) NULL ,
18>     ShipCity nvarchar (15) NULL ,
19>     ShipRegion nvarchar (15) NULL ,
20>     ShipPostalCode nvarchar (10) NULL ,
21>     ShipCountry nvarchar (15) NULL
22> )
23> GO
1>
2>    DECLARE @MyTable Table
3>    (
4>       OrderID      int,
5>       CustomerID   char(5)
6>    )
7>
8>    INSERT INTO @MyTable
9>       SELECT OrderID, CustomerID
10>       FROM Orders
11>       WHERE OrderID BETWEEN 10240 AND 10250
12>
13>    SELECT *
14>    FROM @MyTable
15> GO

(0 rows affected)
OrderID     CustomerID
----------- ----------

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








20.1.Declare
20.1.1.The DECLARE statement has a pretty simple syntax:
20.1.2.It is possible to define several variables in a single Declare statement.
20.1.3.The names of variables must begin with @
20.1.4.Simple SELECT query written using variables for field names.
20.1.5.Declare a table type variable and query it
20.1.6.DECLARE @Out Int (get value out of a procedure)
20.1.7.DECLARE @Out Int