Local variables used for searches : Variable « Transact SQL « SQL Server / T-SQL Tutorial






19> CREATE TABLE employee(
20>    id          INTEGER NOT NULL PRIMARY KEY,
21>    first_name  VARCHAR(10),
22>    last_name   VARCHAR(10),
23>    salary      DECIMAL(10,2),
24>    start_Date  DATETIME,
25>    region      VARCHAR(10),
26>    city        VARCHAR(20),
27>    managerid   INTEGER
28> );
29> GO
1> INSERT INTO employee VALUES (1, 'Jason' ,  'Martin', 5890,'2005-03-22','North','Vancouver',3);
2> GO

(1 rows affected)
1> INSERT INTO employee VALUES (2, 'Alison',  'Mathews',4789,'2003-07-21','South','Utown',4);
2> GO

(1 rows affected)
1> INSERT INTO employee VALUES (3, 'James' ,  'Smith',  6678,'2001-12-01','North','Paris',5);
2> GO

(1 rows affected)
1> INSERT INTO employee VALUES (4, 'Celia' ,  'Rice',   5567,'2006-03-03','South','London',6);
2> GO

(1 rows affected)
1> INSERT INTO employee VALUES (5, 'Robert',  'Black',  4467,'2004-07-02','East','Newton',7);
2> GO

(1 rows affected)
1> INSERT INTO employee VALUES (6, 'Linda' ,  'Green' , 6456,'2002-05-19','East','Calgary',8);
2> GO

(1 rows affected)
1> INSERT INTO employee VALUES (7, 'David' ,  'Larry',  5345,'2008-03-18','West','New York',9);
2> GO

(1 rows affected)
1> INSERT INTO employee VALUES (8, 'James' ,  'Cat',    4234,'2007-07-17','West','Regina',9);
2> GO

(1 rows affected)
1> INSERT INTO employee VALUES (9, 'Joan'  ,  'Act',    6123,'2001-04-16','North','Toronto',10);
2> GO

(1 rows affected)
1>
2> select * from employee;
3> GO
id          first_name last_name  salary       start_Date              region     city                 managerid
----------- ---------- ---------- ------------ ----------------------- ---------- -------------------- -----------
          1 Jason      Martin          5890.00 2005-03-22 00:00:00.000 North      Vancouver                      3
          2 Alison     Mathews         4789.00 2003-07-21 00:00:00.000 South      Utown                          4
          3 James      Smith           6678.00 2001-12-01 00:00:00.000 North      Paris                          5
          4 Celia      Rice            5567.00 2006-03-03 00:00:00.000 South      London                         6
          5 Robert     Black           4467.00 2004-07-02 00:00:00.000 East       Newton                         7
          6 Linda      Green           6456.00 2002-05-19 00:00:00.000 East       Calgary                        8
          7 David      Larry           5345.00 2008-03-18 00:00:00.000 West       New York                       9
          8 James      Cat             4234.00 2007-07-17 00:00:00.000 West       Regina                         9
          9 Joan       Act             6123.00 2001-04-16 00:00:00.000 North      Toronto                       10

(9 rows affected)
1>
2>
3>
4> DECLARE @ID int
5> DECLARE @MinStandardCost money
6> DECLARE @MaxStandardCost money
7>
8> SET @MinStandardCost = 3396
9> SET @MaxStandardCost = 10000
10>
11> SELECT ID, Start_Date, salary
12> FROM Employee
13> WHERE Salary BETWEEN @MinStandardCost and @MaxStandardCost
14>
15> drop table employee;
16> GO
ID          Start_Date              salary
----------- ----------------------- ------------
          1 2005-03-22 00:00:00.000      5890.00
          2 2003-07-21 00:00:00.000      4789.00
          3 2001-12-01 00:00:00.000      6678.00
          4 2006-03-03 00:00:00.000      5567.00
          5 2004-07-02 00:00:00.000      4467.00
          6 2002-05-19 00:00:00.000      6456.00
          7 2008-03-18 00:00:00.000      5345.00
          8 2007-07-17 00:00:00.000      4234.00
          9 2001-04-16 00:00:00.000      6123.00

(9 rows affected)








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