SQL Server/T-SQL/View/Create View
Содержание
- 1 Column names in a view can be defined in the CREATE statement
- 2 Create a simple view and retrieve data from it
- 3 Create view based on table-join query
- 4 CREATE VIEW myView WITH SCHEMABINDING
- 5 CREATE VIEW statement called SCHEMA BINDING tells SQL Server to check for dependencies and disallow any modifications that would violate them
- 6 More Complex Views: inner join
- 7 Rename column when creating a view
- 8 Simple Views
- 9 Stored Procedures as Parameterized Views
- 10 Use view to filter out the NULL data
- 11 View cannot be sorted with ORDER BY unless the TOP command is also used
- 12 When you create a view, you"re really just naming a SQL SELECT statement
- 13 WITH CHECK OPTION keeps the results synchronized with the table(s)
Column names in a view can be defined in the CREATE statement
1> create table employee(
2> ID int,
3> name nvarchar (10),
4> salary int,
5> start_date datetime,
6> city nvarchar (10),
7> region char (1))
8> GO
1>
2> insert into employee (ID, name, salary, start_date, city, region)
3> values (1, "Jason", 40420, "02/01/94", "New York", "W")
4> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (2, "Robert",14420, "01/02/95", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (4, "Linda", 40620, "11/04/97", "New York", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (5, "David", 80026, "10/05/98", "Vancouver","W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (6, "James", 70060, "09/06/99", "Toronto", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (7, "Alison",90620, "08/07/00", "New York", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W")
3> GO
(1 rows affected)
1>
2> select * from employee
3> GO
ID name salary start_date city region
----------- ---------- ----------- ----------------------- ---------- ------
1 Jason 40420 1994-02-01 00:00:00.000 New York W
2 Robert 14420 1995-01-02 00:00:00.000 Vancouver N
3 Celia 24020 1996-12-03 00:00:00.000 Toronto W
4 Linda 40620 1997-11-04 00:00:00.000 New York N
5 David 80026 1998-10-05 00:00:00.000 Vancouver W
6 James 70060 1999-09-06 00:00:00.000 Toronto N
7 Alison 90620 2000-08-07 00:00:00.000 New York W
8 Chris 26020 2001-07-08 00:00:00.000 Vancouver N
9 Mary 60020 2002-06-09 00:00:00.000 Toronto W
(9 rows affected)
1>
2> --Column names can be defined in the CREATE statement:
3>
4> CREATE VIEW MyView
5> (EmployeeID, EmployeeName)
6> As
7> SELECT ID, Name
8> FROM Employee
9> GO
1>
2> select * from MyView
3> GO
EmployeeID EmployeeName
----------- ------------
1 Jason
2 Robert
3 Celia
4 Linda
5 David
6 James
7 Alison
8 Chris
9 Mary
(9 rows affected)
1>
2> drop view myView
3> drop table employee
4> GO
1>
Create a simple view and retrieve data from it
1> create table employee(
2> ID int,
3> name nvarchar (10),
4> salary int,
5> start_date datetime,
6> city nvarchar (10),
7> region char (1))
8> GO
1>
2> insert into employee (ID, name, salary, start_date, city, region)
3> values (1, "Jason", 40420, "02/01/94", "New York", "W")
4> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (2, "Robert",14420, "01/02/95", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (4, "Linda", 40620, "11/04/97", "New York", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (5, "David", 80026, "10/05/98", "Vancouver","W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (6, "James", 70060, "09/06/99", "Toronto", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (7, "Alison",90620, "08/07/00", "New York", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W")
3> GO
(1 rows affected)
1>
2> select * from employee
3> GO
ID name salary start_date city region
----------- ---------- ----------- ----------------------- ---------- ------
1 Jason 40420 1994-02-01 00:00:00.000 New York W
2 Robert 14420 1995-01-02 00:00:00.000 Vancouver N
3 Celia 24020 1996-12-03 00:00:00.000 Toronto W
4 Linda 40620 1997-11-04 00:00:00.000 New York N
5 David 80026 1998-10-05 00:00:00.000 Vancouver W
6 James 70060 1999-09-06 00:00:00.000 Toronto N
7 Alison 90620 2000-08-07 00:00:00.000 New York W
8 Chris 26020 2001-07-08 00:00:00.000 Vancouver N
9 Mary 60020 2002-06-09 00:00:00.000 Toronto W
(9 rows affected)
1>
2> CREATE VIEW Employee_VW
3> AS
4> SELECT TOP 100 PERCENT Id, Name FROM Employee ORDER BY City DESC
5> GO
1>
2> select * from Employee_VW
3> GO
Id Name
----------- ----------
1 Jason
2 Robert
3 Celia
4 Linda
5 David
6 James
7 Alison
8 Chris
9 Mary
(9 rows affected)
1>
2> drop view Employee_VW
3> GO
1>
2>
3> drop table employee
4> GO
1>
Create view based on table-join query
1>
2> create table employee(
3> ID int,
4> name nvarchar (10),
5> salary int )
6> GO
1>
2> create table job(
3> ID int,
4> title nvarchar (10),
5> averageSalary int)
6> GO
1>
2>
3> insert into employee (ID, name, salary) values (1, "Jason", 1234)
4> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (2, "Robert", 4321)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (3, "Celia", 5432)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (4, "Linda", 3456)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (5, "David", 7654)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (6, "James", 4567)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (7, "Alison", 8744)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (8, "Chris", 9875)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (9, "Mary", 2345)
2> GO
(1 rows affected)
1>
2> insert into job(ID, title, averageSalary) values(1,"Developer",3000)
3> GO
(1 rows affected)
1> insert into job(ID, title, averageSalary) values(2,"Tester", 4000)
2> GO
(1 rows affected)
1> insert into job(ID, title, averageSalary) values(3,"Designer", 5000)
2> GO
(1 rows affected)
1> insert into job(ID, title, averageSalary) values(4,"Programmer", 6000)
2> GO
(1 rows affected)
1>
2>
3> select * from employee;
4> GO
ID name salary
----------- ---------- -----------
1 Jason 1234
2 Robert 4321
3 Celia 5432
4 Linda 3456
5 David 7654
6 James 4567
7 Alison 8744
8 Chris 9875
9 Mary 2345
(9 rows affected)
1> select * from job;
2> GO
ID title averageSalary
----------- ---------- -------------
1 Developer 3000
2 Tester 4000
3 Designer 5000
4 Programmer 6000
(4 rows affected)
1>
2> CREATE VIEW dbo.myView
3> AS
4> SELECT e.ID, e.Name, j.title
5> FROM dbo.Employee e JOIN job j
6> ON e.ID = j.ID
7> GO
1>
2> select * from myView
3> GO
ID Name title
----------- ---------- ----------
1 Jason Developer
2 Robert Tester
3 Celia Designer
4 Linda Programmer
(4 rows affected)
1>
2> drop view myView
3> drop table employee;
4> drop table job;
5> GO
1>
2>
CREATE VIEW myView WITH SCHEMABINDING
1>
2> drop view myview
3> drop table employee
4> GO
1> create table employee(
2> ID int,
3> name nvarchar (10),
4> salary int,
5> start_date datetime,
6> city nvarchar (10),
7> region char (1))
8> GO
1>
2> insert into employee (ID, name, salary, start_date, city, region)
3> values (1, "Jason", 40420, "02/01/94", "New York", "W")
4> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (2, "Robert",14420, "01/02/95", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (4, "Linda", 40620, "11/04/97", "New York", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (5, "David", 80026, "10/05/98", "Vancouver","W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (6, "James", 70060, "09/06/99", "Toronto", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (7, "Alison",90620, "08/07/00", "New York", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W")
3> GO
(1 rows affected)
1>
2> select * from employee
3> GO
ID name salary start_date city region
----------- ---------- ----------- ----------------------- ---------- ------
1 Jason 40420 1994-02-01 00:00:00.000 New York W
2 Robert 14420 1995-01-02 00:00:00.000 Vancouver N
3 Celia 24020 1996-12-03 00:00:00.000 Toronto W
4 Linda 40620 1997-11-04 00:00:00.000 New York N
5 David 80026 1998-10-05 00:00:00.000 Vancouver W
6 James 70060 1999-09-06 00:00:00.000 Toronto N
7 Alison 90620 2000-08-07 00:00:00.000 New York W
8 Chris 26020 2001-07-08 00:00:00.000 Vancouver N
9 Mary 60020 2002-06-09 00:00:00.000 Toronto W
(9 rows affected)
1>
2> -- SCHEMABINDING
3>
4>
5> CREATE VIEW myView
6> WITH SCHEMABINDING
7> As
8> SELECT ID, Name
9> FROM dbo.Employee
10> GO
1>
2>
3> select * from myview
4> GO
ID Name
----------- ----------
1 Jason
2 Robert
3 Celia
4 Linda
5 David
6 James
7 Alison
8 Chris
9 Mary
(9 rows affected)
1>
2> drop view myview
3> drop table employee
4> GO
1>
CREATE VIEW statement called SCHEMA BINDING tells SQL Server to check for dependencies and disallow any modifications that would violate them
1> create table employee(
2> ID int,
3> name nvarchar (10),
4> salary int,
5> start_date datetime,
6> city nvarchar (10),
7> region char (1))
8> GO
1>
2> insert into employee (ID, name, salary, start_date, city, region)
3> values (1, "Jason", 40420, "02/01/94", "New York", "W")
4> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (2, "Robert",14420, "01/02/95", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (4, "Linda", 40620, "11/04/97", "New York", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (5, "David", 80026, "10/05/98", "Vancouver","W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (6, "James", 70060, "09/06/99", "Toronto", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (7, "Alison",90620, "08/07/00", "New York", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W")
3> GO
(1 rows affected)
1>
2> select * from employee
3> GO
ID name salary start_date city region
----------- ---------- ----------- ----------------------- ---------- ------
1 Jason 40420 1994-02-01 00:00:00.000 New York W
2 Robert 14420 1995-01-02 00:00:00.000 Vancouver N
3 Celia 24020 1996-12-03 00:00:00.000 Toronto W
4 Linda 40620 1997-11-04 00:00:00.000 New York N
5 David 80026 1998-10-05 00:00:00.000 Vancouver W
6 James 70060 1999-09-06 00:00:00.000 Toronto N
7 Alison 90620 2000-08-07 00:00:00.000 New York W
8 Chris 26020 2001-07-08 00:00:00.000 Vancouver N
9 Mary 60020 2002-06-09 00:00:00.000 Toronto W
(9 rows affected)
1>
2> -- CREATE VIEW statement called SCHEMA BINDING tells SQL Server to check
3> -- for dependencies and disallow any modifications that would violate them
4>
5>
6> CREATE VIEW MyView WITH SCHEMABINDING
7> AS
8> SELECT ID, Name
9> FROM dbo.Employee
10> GO
1>
2> select * from myview
3> GO
ID Name
----------- ----------
1 Jason
2 Robert
3 Celia
4 Linda
5 David
6 James
7 Alison
8 Chris
9 Mary
(9 rows affected)
1>
2> CREATE VIEW MyView2 WITH SCHEMABINDING
3> AS
4> SELECT Name
5> FROM dbo.MyView
6> GO
1>
2> select * from myview2
3>
4> drop view myview2
5> drop view myview
6> drop table employee
7> GO
Name
----------
Jason
Robert
Celia
Linda
David
James
Alison
Chris
Mary
(9 rows affected)
1>
More Complex Views: inner join
1>
2> create table employee(
3> ID int,
4> name nvarchar (10),
5> salary int )
6> GO
1>
2> create table job(
3> ID int,
4> title nvarchar (10),
5> averageSalary int)
6> GO
1>
2>
3> insert into employee (ID, name, salary) values (1, "Jason", 1234)
4> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (2, "Robert", 4321)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (3, "Celia", 5432)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (4, "Linda", 3456)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (5, "David", 7654)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (6, "James", 4567)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (7, "Alison", 8744)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (8, "Chris", 9875)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (9, "Mary", 2345)
2> GO
(1 rows affected)
1>
2> insert into job(ID, title, averageSalary) values(1,"Developer",3000)
3> GO
(1 rows affected)
1> insert into job(ID, title, averageSalary) values(2,"Tester", 4000)
2> GO
(1 rows affected)
1> insert into job(ID, title, averageSalary) values(3,"Designer", 5000)
2> GO
(1 rows affected)
1> insert into job(ID, title, averageSalary) values(4,"Programmer", 6000)
2> GO
(1 rows affected)
1>
2>
3> select * from employee;
4> GO
ID name salary
----------- ---------- -----------
1 Jason 1234
2 Robert 4321
3 Celia 5432
4 Linda 3456
5 David 7654
6 James 4567
7 Alison 8744
8 Chris 9875
9 Mary 2345
(9 rows affected)
1> select * from job;
2> GO
ID title averageSalary
----------- ---------- -------------
1 Developer 3000
2 Tester 4000
3 Designer 5000
4 Programmer 6000
(4 rows affected)
1>
2>
3> --More Complex Views
4>
5> CREATE VIEW EmployeeJob
6> AS
7> SELECT e.id,
8> e.name,
9> j.title
10> FROM Employee AS e
11> INNER JOIN Job AS j
12> ON e.ID = j.ID
13> GO
1>
2> select * from EmployeeJob
3> GO
id name title
----------- ---------- ----------
1 Jason Developer
2 Robert Tester
3 Celia Designer
4 Linda Programmer
(4 rows affected)
1>
2> drop view EmployeeJob
3> drop table employee;
4> drop table job;
5> GO
1>
2>
Rename column when creating a view
1> create table employee(
2> ID int,
3> name nvarchar (10),
4> salary int )
5> GO
1>
2> create table job(
3> ID int,
4> title nvarchar (10),
5> averageSalary int)
6> GO
1>
2>
3> insert into employee (ID, name, salary) values (1, "Jason", 1234)
4> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (2, "Robert", 4321)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (3, "Celia", 5432)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (4, "Linda", 3456)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (5, "David", 7654)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (6, "James", 4567)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (7, "Alison", 8744)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (8, "Chris", 9875)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (9, "Mary", 2345)
2> GO
(1 rows affected)
1>
2> insert into job(ID, title, averageSalary) values(1,"Developer",3000)
3> GO
(1 rows affected)
1> insert into job(ID, title, averageSalary) values(2,"Tester", 4000)
2> GO
(1 rows affected)
1> insert into job(ID, title, averageSalary) values(3,"Designer", 5000)
2> GO
(1 rows affected)
1> insert into job(ID, title, averageSalary) values(4,"Programmer", 6000)
2> GO
(1 rows affected)
1>
2>
3>
4> select * from employee;
5> GO
ID name salary
----------- ---------- -----------
1 Jason 1234
2 Robert 4321
3 Celia 5432
4 Linda 3456
5 David 7654
6 James 4567
7 Alison 8744
8 Chris 9875
9 Mary 2345
(9 rows affected)
1> select * from job;
2> GO
ID title averageSalary
----------- ---------- -------------
1 Developer 3000
2 Tester 4000
3 Designer 5000
4 Programmer 6000
(4 rows affected)
1>
2> SELECT e.ID "EmployeeID",e.name "EmployeeName", j.title "EmployeeTitle"
3> FROM Employee e INNER JOIN job j
4> ON e.ID = j.ID
5> GO
EmployeeID EmployeeName EmployeeTitle
----------- ------------ -------------
1 Jason Developer
2 Robert Tester
3 Celia Designer
4 Linda Programmer
(4 rows affected)
1>
2>
3> CREATE VIEW ViewOnEmployee
4> AS
5> SELECT e.ID "EmployeeID",e.name "EmployeeName", j.title "EmployeeTitle"
6> FROM Employee e INNER JOIN job j
7> ON e.ID = j.ID
8> GO
1>
2> select * from ViewOnEmployee
3> GO
EmployeeID EmployeeName EmployeeTitle
----------- ------------ -------------
1 Jason Developer
2 Robert Tester
3 Celia Designer
4 Linda Programmer
(4 rows affected)
1>
2> drop table employee;
3> drop table job;
4> GO
1>
Simple Views
1> create table employee(
2> ID int,
3> name nvarchar (10),
4> salary int,
5> start_date datetime,
6> city nvarchar (10),
7> region char (1))
8> GO
1>
2> insert into employee (ID, name, salary, start_date, city, region)
3> values (1, "Jason", 40420, "02/01/94", "New York", "W")
4> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (2, "Robert",14420, "01/02/95", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (4, "Linda", 40620, "11/04/97", "New York", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (5, "David", 80026, "10/05/98", "Vancouver","W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (6, "James", 70060, "09/06/99", "Toronto", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (7, "Alison",90620, "08/07/00", "New York", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W")
3> GO
(1 rows affected)
1>
2> select * from employee
3> GO
ID name salary start_date city region
----------- ---------- ----------- ----------------------- ---------- ------
1 Jason 40420 1994-02-01 00:00:00.000 New York W
2 Robert 14420 1995-01-02 00:00:00.000 Vancouver N
3 Celia 24020 1996-12-03 00:00:00.000 Toronto W
4 Linda 40620 1997-11-04 00:00:00.000 New York N
5 David 80026 1998-10-05 00:00:00.000 Vancouver W
6 James 70060 1999-09-06 00:00:00.000 Toronto N
7 Alison 90620 2000-08-07 00:00:00.000 New York W
8 Chris 26020 2001-07-08 00:00:00.000 Vancouver N
9 Mary 60020 2002-06-09 00:00:00.000 Toronto W
(9 rows affected)
1>
2> -- Simple Views
3>
4> CREATE VIEW myView
5> AS SELECT Name, Salary FROM Employee
6> GO
Msg 2714, Level 16, State 3, Server sqle\SQLEXPRESS, Procedure myView, Line 5
There is already an object named "myView" in the database.
1>
2> SELECT * FROM myView
3> GO
Name Salary
---------- -----------
Jason 40420
Robert 14420
Celia 24020
Linda 40620
David 80026
James 70060
Alison 90620
Chris 26020
Mary 60020
(9 rows affected)
1>
2> Drop view myView
3> GO
1>
2> drop table employee
3> GO
1>
Stored Procedures as Parameterized Views
1> create table employee(
2> ID int,
3> name nvarchar (10),
4> salary int,
5> start_date datetime,
6> city nvarchar (10),
7> region char (1))
8> GO
1>
2> insert into employee (ID, name, salary, start_date, city, region)
3> values (1, "Jason", 40420, "02/01/94", "New York", "W")
4> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (2, "Robert",14420, "01/02/95", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (4, "Linda", 40620, "11/04/97", "New York", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (5, "David", 80026, "10/05/98", "Vancouver","W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (6, "James", 70060, "09/06/99", "Toronto", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (7, "Alison",90620, "08/07/00", "New York", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W")
3> GO
(1 rows affected)
1>
2> select * from employee
3> GO
ID name salary start_date city region
----------- ---------- ----------- ----------------------- ---------- ------
1 Jason 40420 1994-02-01 00:00:00.000 New York W
2 Robert 14420 1995-01-02 00:00:00.000 Vancouver N
3 Celia 24020 1996-12-03 00:00:00.000 Toronto W
4 Linda 40620 1997-11-04 00:00:00.000 New York N
5 David 80026 1998-10-05 00:00:00.000 Vancouver W
6 James 70060 1999-09-06 00:00:00.000 Toronto N
7 Alison 90620 2000-08-07 00:00:00.000 New York W
8 Chris 26020 2001-07-08 00:00:00.000 Vancouver N
9 Mary 60020 2002-06-09 00:00:00.000 Toronto W
(9 rows affected)
1>
2> --Stored Procedures as Parameterized Views
3>
4> CREATE VIEW MyView
5> AS
6> SELECT ID, Name
7> FROM Employee
8> GO
1>
2> CREATE PROCEDURE spMyData
3> AS
4> SELECT ID, Name
5> FROM Employee
6> GO
1>
2> EXECUTE spMyData
3> GO
ID Name
----------- ----------
1 Jason
2 Robert
3 Celia
4 Linda
5 David
6 James
7 Alison
8 Chris
9 Mary
(9 rows affected)
1>
2> drop view myview
3> drop procedure spMyData
4> drop table employee
5> GO
1>
Use view to filter out the NULL data
1> create table employee(
2> ID int,
3> name nvarchar (10),
4> salary int,
5> start_date datetime,
6> city nvarchar (10),
7> region char (1))
8> GO
1>
2> insert into employee (ID, name, salary, start_date, city, region)
3> values (1, "Jason", 40420, "02/01/94", "New York", "W")
4> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (2, "Robert",14420, "01/02/95", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (4, "Linda", 40620, "11/04/97", "New York", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (5, "David", 80026, "10/05/98", "Vancouver","W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (6, "James", 70060, "09/06/99", "Toronto", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (7, "Alison",90620, "08/07/00", "New York", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (9, "Mary", 60020, NULL, "Toronto", "W")
3> GO
(1 rows affected)
1>
2> select * from employee
3> GO
ID name salary start_date city region
----------- ---------- ----------- ----------------------- ---------- ------
1 Jason 40420 1994-02-01 00:00:00.000 New York W
2 Robert 14420 1995-01-02 00:00:00.000 Vancouver N
3 Celia 24020 1996-12-03 00:00:00.000 Toronto W
4 Linda 40620 1997-11-04 00:00:00.000 New York N
5 David 80026 1998-10-05 00:00:00.000 Vancouver W
6 James 70060 1999-09-06 00:00:00.000 Toronto N
7 Alison 90620 2000-08-07 00:00:00.000 New York W
8 Chris 26020 2001-07-08 00:00:00.000 Vancouver N
9 Mary 60020 NULL Toronto W
(9 rows affected)
1>
2>
3> CREATE VIEW myView
4> AS
5> SELECT ID,
6> Name,
7> Salary
8> FROM Employee
9> WHERE Start_Date IS NULL
10> GO
1>
2> select * from myView
3> GO
ID Name Salary
----------- ---------- -----------
9 Mary 60020
(1 rows affected)
1>
2> drop view myView
3> GO
1>
2> drop table employee
3> GO
1>
View cannot be sorted with ORDER BY unless the TOP command is also used
1> create table employee(
2> ID int,
3> name nvarchar (10),
4> salary int,
5> start_date datetime,
6> city nvarchar (10),
7> region char (1))
8> GO
1>
2> insert into employee (ID, name, salary, start_date, city, region)
3> values (1, "Jason", 40420, "02/01/94", "New York", "W")
4> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (2, "Robert",14420, "01/02/95", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (4, "Linda", 40620, "11/04/97", "New York", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (5, "David", 80026, "10/05/98", "Vancouver","W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (6, "James", 70060, "09/06/99", "Toronto", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (7, "Alison",90620, "08/07/00", "New York", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W")
3> GO
(1 rows affected)
1>
2> select * from employee
3> GO
ID name salary start_date city region
----------- ---------- ----------- ----------------------- ---------- ------
1 Jason 40420 1994-02-01 00:00:00.000 New York W
2 Robert 14420 1995-01-02 00:00:00.000 Vancouver N
3 Celia 24020 1996-12-03 00:00:00.000 Toronto W
4 Linda 40620 1997-11-04 00:00:00.000 New York N
5 David 80026 1998-10-05 00:00:00.000 Vancouver W
6 James 70060 1999-09-06 00:00:00.000 Toronto N
7 Alison 90620 2000-08-07 00:00:00.000 New York W
8 Chris 26020 2001-07-08 00:00:00.000 Vancouver N
9 Mary 60020 2002-06-09 00:00:00.000 Toronto W
(9 rows affected)
1>
2> --View cannot be sorted with ORDER BY unless the TOP command is also used.
3>
4> CREATE VIEW MyView
5> As
6> SELECT TOP 5 PERCENT Name
7> FROM Employee
8> ORDER BY Name
9> GO
1>
2> select * from myView
3> GO
Name
----------
Alison
(1 rows affected)
1>
2> drop table employee
3> GO
1>
When you create a view, you"re really just naming a SQL SELECT statement
1>
2> create table employee(
3> ID int,
4> name nvarchar (10),
5> salary int )
6> GO
1>
2> create table job(
3> ID int,
4> title nvarchar (10),
5> averageSalary int)
6> GO
1>
2>
3> insert into employee (ID, name, salary) values (1, "Jason", 1234)
4> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (2, "Robert", 4321)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (3, "Celia", 5432)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (4, "Linda", 3456)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (5, "David", 7654)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (6, "James", 4567)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (7, "Alison", 8744)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (8, "Chris", 9875)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (9, "Mary", 2345)
2> GO
(1 rows affected)
1>
2> insert into job(ID, title, averageSalary) values(1,"Developer",3000)
3> GO
(1 rows affected)
1> insert into job(ID, title, averageSalary) values(2,"Tester", 4000)
2> GO
(1 rows affected)
1> insert into job(ID, title, averageSalary) values(3,"Designer", 5000)
2> GO
(1 rows affected)
1> insert into job(ID, title, averageSalary) values(4,"Programmer", 6000)
2> GO
(1 rows affected)
1>
2>
3> select * from employee;
4> GO
ID name salary
----------- ---------- -----------
1 Jason 1234
2 Robert 4321
3 Celia 5432
4 Linda 3456
5 David 7654
6 James 4567
7 Alison 8744
8 Chris 9875
9 Mary 2345
(9 rows affected)
1> select * from job;
2> GO
ID title averageSalary
----------- ---------- -------------
1 Developer 3000
2 Tester 4000
3 Designer 5000
4 Programmer 6000
(4 rows affected)
1>
2>
3> -- Creating a View
4>
5> -- When you create a view, you"re really just naming a SQL SELECT statement
6>
7> CREATE VIEW vwEmployeeJobDetails
8> AS
9> SELECT e.ID
10> , e.name
11> , j.title
12> FROM Employee e
13> INNER JOIN Job j
14> ON e.ID = j.ID
15> GO
1>
2> select * from vwEmployeeJobDetails
3> GO
ID name title
----------- ---------- ----------
1 Jason Developer
2 Robert Tester
3 Celia Designer
4 Linda Programmer
(4 rows affected)
1>
2> drop view vwEmployeeJobDetails
3> drop table employee;
4> drop table job;
5> GO
1>
2>
WITH CHECK OPTION keeps the results synchronized with the table(s)
1> create table employee(
2> ID int,
3> name nvarchar (10),
4> salary int,
5> start_date datetime,
6> city nvarchar (10),
7> region char (1))
8> GO
1>
2> insert into employee (ID, name, salary, start_date, city, region)
3> values (1, "Jason", 40420, "02/01/94", "New York", "W")
4> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (2, "Robert",14420, "01/02/95", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (4, "Linda", 40620, "11/04/97", "New York", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (5, "David", 80026, "10/05/98", "Vancouver","W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (6, "James", 70060, "09/06/99", "Toronto", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (7, "Alison",90620, "08/07/00", "New York", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W")
3> GO
(1 rows affected)
1>
2> select * from employee
3> GO
ID name salary start_date city region
----------- ---------- ----------- ----------------------- ---------- ------
1 Jason 40420 1994-02-01 00:00:00.000 New York W
2 Robert 14420 1995-01-02 00:00:00.000 Vancouver N
3 Celia 24020 1996-12-03 00:00:00.000 Toronto W
4 Linda 40620 1997-11-04 00:00:00.000 New York N
5 David 80026 1998-10-05 00:00:00.000 Vancouver W
6 James 70060 1999-09-06 00:00:00.000 Toronto N
7 Alison 90620 2000-08-07 00:00:00.000 New York W
8 Chris 26020 2001-07-08 00:00:00.000 Vancouver N
9 Mary 60020 2002-06-09 00:00:00.000 Toronto W
(9 rows affected)
1>
2> --WITH CHECK OPTION keeps the results synchronized with the table(s)
3>
4> ALTER VIEW MyView
5> As
6> SELECT Name
7> FROM Employee
8> WITH CHECK OPTION
9> GO
1>
2> select * from myview
3> GO
Name
----------
Jason
Robert
Celia
Linda
David
James
Alison
Chris
Mary
(9 rows affected)
1>
2> drop view myview
3> drop table employee
4> GO
1>