Professional Documents
Culture Documents
Master subtitle
style
Module Overview
Views
Temporary Tables
Table Variables
Table-Valued Functions
Derived Tables
Common Table Expressions
Querying Views
Views are named queries with definitions stored in a
database
Views can provide abstraction, encapsulation and
simplification
From an administrative perspective, views can provide a
security layer to a database
CREATE
CREATE VIEW Sales.vSalesOrders
AS
AS
SELECT
SELECT oh.OrderID,
oh.OrderID, oh.Orderdate,
oh.Orderdate,
oh.CustomerID,
oh.CustomerID,
od.LineItemNo,
od.LineItemNo, od.ProductID, od.Quantity
SELECT
SELECT OrderID, CustomerID,
FROM
FROM Sales.OrderHeaders AS oh
oh
ProductID
ProductID
JOIN
JOIN Sales.OrderDetails
Sales.OrderDetails AS od
FROM
FROM Sales.vSalesOrder
Sales.vSalesOrder
ON
ON od.OrderID
od.OrderID = oh.OrderID;
ORDER
ORDER BY
BY OrderID;
OrderID;
DEMO
Querying Views
Temporary Tables
CREATE
CREATE TABLE #tmpProducts
(ProductID
(ProductID INTEGER,
INTEGER,
ProductName
ProductName varchar(50));
varchar(50));
GO
GO
SELECT
SELECT ** FROM
FROM #tmpProducts;
#tmpProducts;
Table Variables
DECLARE
DECLARE @varProducts
@varProducts table
table
(ProductID
(ProductID INTEGER,
ProductName
ProductName varchar(50));
varchar(50));
SELECT
SELECT ** FROM @varProducts
DEMO
Temporary Tables and Table Variables
Table-Valued Functions
CREATE
CREATE FUNCTION
FUNCTION Sales.fn_GetOrderItems
Sales.fn_GetOrderItems (@OrderID
(@OrderID AS
AS Integer)
RETURNS
RETURNS TABLE
AS
AS
RETURN
RETURN
(SELECT
(SELECT ProductID,
ProductID, UnitPrice,
UnitPrice, Quantity
Quantity
FROM
FROM Sales.OrderDetails
Sales.OrderDetails
WHERE
WHERE OrderID
OrderID =
= @OrderID);
@OrderID);
SELECT
SELECT * FROM
FROM Sales.fn_GetOrderItems
Sales.fn_GetOrderItems (1025) AS
AS LineItems;
LineItems;
DEMO
Using Table-Valued Functions
Derived Tables
Introduction
SELECT
SELECT orderyear,
orderyear, COUNT(DISTINCT
COUNT(DISTINCT custid)
custid) AS
AS
cust_count
cust_count
FROM
FROM
(SELECT
(SELECT YEAR(orderdate) AS orderyear, custid
custid
FROM
FROM Sales.Orders) AS derived_year
GROUP
GROUP BY orderyear;
Derived Tables
Guidelines
Derived Tables
Or externally:
SELECT
SELECT orderyear,
orderyear, COUNT(DISTINCT
COUNT(DISTINCT custid)
custid) AS
AS cust_count
cust_count
FROM
FROM (( SELECT
SELECT YEAR(orderdate),
YEAR(orderdate), custid
custid
FROM
FROM Sales.Orders)
Sales.Orders) AS
AS
derived_year(orderyear,
derived_year(orderyear, custid)
custid)
GROUP
GROUP BY
BY orderyear;
orderyear;
DEMO
Using Derived Tables
WITH
WITH OrgReport
OrgReport (ManagerID,
(ManagerID, EmployeeID,
EmployeeID, EmployeeName,
EmployeeName, Level)
Level)
AS
AS
((
SELECT
SELECT e.ManagerID,
e.ManagerID, e.EmployeeID,
e.EmployeeID, EmployeeName,
EmployeeName, 0
0
FROM
FROM HR.Employee
HR.Employee AS
AS ee
WHERE
WHERE ManagerID
ManagerID IS
IS NULL
NULL
UNION
UNION ALL
ALL
SELECT
SELECT e.ManagerID,
e.ManagerID, e.EmployeeID,
e.EmployeeID, e.EmployeeName,
e.EmployeeName, Level
Level +
+1
1
FROM
FROM HR.Employee
HR.Employee AS
AS ee
INNER
INNER JOIN
JOIN OrgReport
OrgReport AS
AS oo ON
ON e.ManagerID
e.ManagerID =
= o.EmployeeID
o.EmployeeID
))
SELECT
SELECT ** FROM
FROM OrgReport
OrgReport
OPTION
OPTION (MAXRECURSION
(MAXRECURSION 3);
3);
DEMO
Using Common Table Expressions
2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are or may be registered
trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of
Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a
commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT
MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.