SQL Server/T-SQL Tutorial/Transact SQL/Declare

Материал из SQL эксперт
Версия от 10:22, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Declare a table type variable and query it

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>


DECLARE @Out Int

3>
4>
5>
6> CREATE TABLE Product(
7>     ProductID               int                NOT NULL,
8>     Name                    nvarchar(25)       NOT NULL,
9>     ProductNumber           nvarchar(25)               ,
10>     Color                   nvarchar(15)       NULL,
11>     StandardCost            money              NOT NULL,
12>     Size                    nvarchar(5)        NULL,
13>     Weight                  decimal(8, 2)      NULL,
14>     ProductLine             nchar(20)           NULL,
15>     SellStartDate           datetime           NOT NULL,
16>     SellEndDate             datetime           NULL
17> )
18> GO
1> insert into Product values(1,"Product A", "1","Red",123.123,"1",1,"ProductLine A","1999-03-22","2000-03-22");
2> GO
(1 rows affected)
1> insert into Product values(2,"Product B", "2","Yellow",234.234,"1",3,"ProductLine B","2000-03-22","2001-03-22");
2> GO
(1 rows affected)
1> insert into Product values(3,"Product C", "3","Pink",345.345,"1",3,"ProductLine V","2001-09-22","2006-02-22");
2> GO
(1 rows affected)
1> insert into Product values(4,"Product D", "4","White",456.456,"1",4,"ProductLine D","2002-08-22","2006-03-22");
2> GO
(1 rows affected)
1> insert into Product values(5,"Product E", "5","Black",567.567,"1",5,"ProductLine E","2003-01-22","2003-04-22");
2> GO
(1 rows affected)
1> insert into Product values(6,"Product F", "6","Blue",678.678,"1",6,"ProductLine W","2004-02-22","2005-05-22");
2> GO
(1 rows affected)
1> insert into Product values(7,"Product G", "7","Drak",789.789,"1",7,"ProductLine Q","2005-03-22","2006-03-22");
2> GO
(1 rows affected)
1> insert into Product values(8,"Product H", "8","Gray",234.123,"1",8,"ProductLine F","2006-04-22","2006-09-22");
2> GO
(1 rows affected)
1> insert into Product values(9,"Product I", "9","Red",543.123,"1",9,"ProductLine R","2007-05-22","2008-03-22");
2> GO
(1 rows affected)
1> insert into Product values(0,"Product J", "0","Gold",765.123,"1",0,"ProductLine J","2008-06-22","2009-03-22");
2> GO
(1 rows affected)
1>
2>
3>
4> CREATE PROCEDURE spProductCountBySubCategory
5>  @SubID Int,
6>  @ProdCount Int OUTPUT
7> AS
8>  SELECT @ProdCount = COUNT(*)
9>  FROM Product
10>  WHERE ProductID = @SubID
11> GO
1>
2> DECLARE @Out Int
3>
4>   EXECUTE spProductCountBySubCategory
5> @SubID = 2,
6> @ProdCount = @Out
7>
8>
9> SELECT @Out AS ProductCountBySubCategory
10> GO
ProductCountBySubCategory
-------------------------
                     NULL
(1 rows affected)
1>
2> drop PROCEDURE spProductCountBySubCategory;
3> GO
1>
2>
3> drop table Product;
4> GO
1>


DECLARE @Out Int (get value out of a procedure)

6> CREATE TABLE Product(
7>     ProductID               int                NOT NULL,
8>     Name                    nvarchar(25)       NOT NULL,
9>     ProductNumber           nvarchar(25)               ,
10>      Color                   nvarchar(15)       NULL,
11>      StandardCost            money              NOT NULL,
12>      Size                    nvarchar(5)        NULL,
13>      Weight                  decimal(8, 2)      NULL,
14>      ProductLine             nchar(20)           NULL,
15>      SellStartDate           datetime           NOT NULL,
16>      SellEndDate             datetime           NULL
17>  )
18>  GO
1> insert into Product values(1,"Product A", "1","Red",123.123,"1",1,"ProductLine A","1999-03-22","2000-03-22");
2> GO
(1 rows affected)
1>
2>
3> insert into Product values(2,"Product B", "2","Yellow",234.234,"1",3,"ProductLine B","2000-03-22","2001-03-22");
4> GO
(1 rows affected)
1>
2>
3> insert into Product values(3,"Product C", "3","Pink",345.345,"1",3,"ProductLine V","2001-09-22","2006-02-22");
4> GO
(1 rows affected)
1>
2>
3> insert into Product values(4,"Product D", "4","White",456.456,"1",4,"ProductLine D","2002-08-22","2006-03-22");
4> GO
(1 rows affected)
1>
2>
3> insert into Product values(5,"Product E", "5","Black",567.567,"1",5,"ProductLine E","2003-01-22","2003-04-22");
4> GO
(1 rows affected)
1>
2>
3> insert into Product values(6,"Product F", "6","Blue",678.678,"1",6,"ProductLine W","2004-02-22","2005-05-22");
4> GO
(1 rows affected)
1>
2>
3> insert into Product values(7,"Product G", "7","Drak",789.789,"1",7,"ProductLine Q","2005-03-22","2006-03-22");
4> GO
(1 rows affected)
1>
2>
3> insert into Product values(8,"Product H", "8","Gray",234.123,"1",8,"ProductLine F","2006-04-22","2006-09-22");
4> GO
(1 rows affected)
1>
2>
3> insert into Product values(9,"Product I", "9","Red",543.123,"1",9,"ProductLine R","2007-05-22","2008-03-22");
4> GO
(1 rows affected)
1>
2>
3> insert into Product values(0,"Product J", "0","Gold",765.123,"1",0,"ProductLine J","2008-06-22","2009-03-22");
4> GO
(1 rows affected)
1>
2>
3>
4>
5>
6> CREATE PROCEDURE spProductCountBySubCategory
7>  @SubID Int,
8>  @ProdCount Int OUTPUT
9> AS
10>  SELECT @ProdCount = COUNT(*)
11>  FROM Product
12>   WHERE ProductID = @SubID
13>  GO
1>
2> DECLARE @Out Int
3>
4>   EXECUTE spProductCountBySubCategory
5> @SubID = 2,
6> @ProdCount = @Out
7>
8>
9> SELECT @Out AS ProductCountBySubCategory
10>  GO
ProductCountBySubCategory
-------------------------
                     NULL
(1 rows affected)
1>
2> drop PROCEDURE spProductCountBySubCategory;
3> GO
1>
2>
3> drop table Product;
4> GO
1>


It is possible to define several variables in a single Declare statement.

4>
5>
6> Declare     @LastName varchar(50),
7>             @FirstName varchar(20),
8>             @BirthDate smalldatetime
9> GO
1>


Simple SELECT query written using variables for field names.

3>
4>
5> CREATE TABLE authors(
6>    au_id          varchar(11),
7>    au_lname       varchar(40)       NOT NULL,
8>    au_fname       varchar(20)       NOT NULL,
9>    phone          char(12)          NOT NULL DEFAULT ("UNKNOWN"),
10>    address        varchar(40)           NULL,
11>    city           varchar(20)           NULL,
12>    state          char(2)               NULL,
13>    zip            char(5)               NULL,
14>    contract       bit               NOT NULL
15> )
16> GO
1> insert authors values("1",  "Joe",   "Abra",   "111 111-1111", "6 St.", "Berkeley",  "CA", "11111", 1)
2> insert authors values("2",  "Jack",  "Majo",   "222 222-2222", "3 St.", "Oakland" ,  "CA", "22222", 1)
3> insert authors values("3",  "Pink",  "Cherry", "333 333-3333", "5 Ln.", "Vancouver", "BC", "33333", 1)
4> insert authors values("4",  "Blue",  "Albert", "444 444-4444", "7 Av.", "Vancouver", "BC", "44444", 1)
5> insert authors values("5",  "Red",   "Anne",   "555 555-5555", "6 Av.", "Regina",    "SK", "55555", 1)
6> insert authors values("6",  "Black", "Michel", "666 666-6666", "3 Pl.", "Regina",    "SK", "66666", 1)
7> insert authors values("7",  "White", "Sylvia", "777 777-7777", "1 Pl.", "Rockville", "MD", "77777", 1)
8> insert authors values("8",  "Yellow","Heather","888 888-8888", "3 Pu",  "Vacaville", "CA", "88888", 0)
9> insert authors values("9",  "Gold",  "Dep",    "999 999-9999", "5 Av.", "Oakland",   "CA", "99999", 0)
10> insert authors values("10", "Siler", "Dean",   "000 000-0000", "4 Av.", "Oakland",   "CA", "00000", 1)
11> GO
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
1>
2>
3>     DECLARE @chvField1 VARCHAR(30),
4>             @chvField2 VARCHAR(30),
5>             @chvSQL VARCHAR(255)
6>     SELECT @chvField1 = "au_fname"
7>     SELECT @chvField2 = "au_lname"
8>     SELECT @chvSQL = "SELECT " + @chvField1 + ", " + @chvField2 + " FROM authors"
9>     EXEC (@chvSQL)
10>     GO
au_fname             au_lname
-------------------- ----------------------------------------
Abra                 Joe
Majo                 Jack
Cherry               Pink
Albert               Blue
Anne                 Red
Michel               Black
Sylvia               White
Heather              Yellow
Dep                  Gold
Dean                 Siler
(10 rows affected)
1>
2> drop table authors;
3> GO
1>


The DECLARE statement has a pretty simple syntax:

5> --DECLARE @<variable name> <variable type>[,
6> --        @<variable name> <variable type>[,
7> --        @<variable name> <variable type>]]
8> --
9> --Setting Variables Using SET
10>
11>
12> CREATE TABLE OrderDetails (
13>     OrderID int NOT NULL ,
14>     ProductID int NOT NULL ,
15>     UnitPrice money NOT NULL DEFAULT (0),
16>     Quantity smallint NOT NULL DEFAULT (1),
17>     Discount real NOT NULL DEFAULT (0)
18> )
19> GO
1> INSERT OrderDetails VALUES(10248,11,14,12,0)
2> INSERT OrderDetails VALUES(10248,42,9.8,10,0)
3> INSERT OrderDetails VALUES(10248,72,34.8,5,0)
4> INSERT OrderDetails VALUES(10249,14,18.6,9,0)
5> INSERT OrderDetails VALUES(10249,51,42.4,40,0)
6> INSERT OrderDetails VALUES(10250,41,7.7,10,0)
7> INSERT OrderDetails VALUES(10250,51,42.4,35,0.15)
8> INSERT OrderDetails VALUES(10250,65,16.8,15,0.15)
9> INSERT OrderDetails VALUES(10251,22,16.8,6,0.05)
10> INSERT OrderDetails VALUES(10251,57,15.6,15,0.05)
11> go
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
1>
2>    DECLARE @TotalCost money
3>
4>    SET @TotalCost = 10
5>    SET @TotalCost = @TotalCost * 1.1
6>
7>
8>    DECLARE @Test money
9>
10>    select @Test = MAX(UnitPrice) FROM OrderDetails
11>    SELECT @Test
12> GO
---------------------
              42.4000
(1 rows affected)
1>
2> drop table OrderDetails;
3> GO


The names of variables must begin with @

5>
6> Declare @LastName varchar(50)
7> GO
1>