-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-6 Views vs Procedures.sql
More file actions
92 lines (85 loc) · 3.79 KB
/
2-6 Views vs Procedures.sql
File metadata and controls
92 lines (85 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
USE [AdventureWorks2016CTP3];
GO
--View Example
GO
CREATE OR ALTER VIEW [Sales].[vSalesPerson]
AS
SELECT s.[BusinessEntityID] ,
p.[Title] ,
p.[FirstName] ,
p.[MiddleName] ,
p.[LastName] ,
p.[Suffix] ,
e.[JobTitle] ,
pp.[PhoneNumber] ,
pnt.[Name] AS [PhoneNumberType] ,
ea.[EmailAddress] ,
p.[EmailPromotion] ,
a.[AddressLine1] ,
a.[AddressLine2] ,
a.[City] ,
[StateProvinceName] = sp.[Name] ,
a.[PostalCode] ,
[CountryRegionName] = cr.[Name] ,
[TerritoryName] = st.[Name] ,
[TerritoryGroup] = st.[Group] ,
s.[SalesQuota] ,
s.[SalesYTD] ,
s.[SalesLastYear]
FROM [Sales].[SalesPerson] s
INNER JOIN [HumanResources].[Employee] e ON e.[BusinessEntityID] = s.[BusinessEntityID]
INNER JOIN [Person].[Person] p ON p.[BusinessEntityID] = s.[BusinessEntityID]
INNER JOIN [Person].[BusinessEntityAddress] bea ON bea.[BusinessEntityID] = s.[BusinessEntityID]
INNER JOIN [Person].[Address] a ON a.[AddressID] = bea.[AddressID]
INNER JOIN [Person].[StateProvince] sp ON sp.[StateProvinceID] = a.[StateProvinceID]
INNER JOIN [Person].[CountryRegion] cr ON cr.[CountryRegionCode] = sp.[CountryRegionCode]
LEFT OUTER JOIN [Sales].[SalesTerritory] st ON st.[TerritoryID] = s.[TerritoryID]
LEFT OUTER JOIN [Person].[EmailAddress] ea ON ea.[BusinessEntityID] = p.[BusinessEntityID]
LEFT OUTER JOIN [Person].[PersonPhone] pp ON pp.[BusinessEntityID] = p.[BusinessEntityID]
LEFT OUTER JOIN [Person].[PhoneNumberType] pnt ON pnt.[PhoneNumberTypeID] = pp.[PhoneNumberTypeID];
GO
SELECT *
FROM [Sales].[vSalesPerson]
WHERE BusinessEntityID = 290;
GO
--Procedure Example
CREATE OR ALTER PROCEDURE [Sales].[usp_SalesPerson]
@busEntityID INT
AS
SELECT s.[BusinessEntityID] ,
p.[Title] ,
p.[FirstName] ,
p.[MiddleName] ,
p.[LastName] ,
p.[Suffix] ,
e.[JobTitle] ,
pp.[PhoneNumber] ,
pnt.[Name] AS [PhoneNumberType] ,
ea.[EmailAddress] ,
p.[EmailPromotion] ,
a.[AddressLine1] ,
a.[AddressLine2] ,
a.[City] ,
[StateProvinceName] = sp.[Name] ,
a.[PostalCode] ,
[CountryRegionName] = cr.[Name] ,
[TerritoryName] = st.[Name] ,
[TerritoryGroup] = st.[Group] ,
s.[SalesQuota] ,
s.[SalesYTD] ,
s.[SalesLastYear]
FROM [Sales].[SalesPerson] s
INNER JOIN [HumanResources].[Employee] e ON e.[BusinessEntityID] = s.[BusinessEntityID]
INNER JOIN [Person].[Person] p ON p.[BusinessEntityID] = s.[BusinessEntityID]
INNER JOIN [Person].[BusinessEntityAddress] bea ON bea.[BusinessEntityID] = s.[BusinessEntityID]
INNER JOIN [Person].[Address] a ON a.[AddressID] = bea.[AddressID]
INNER JOIN [Person].[StateProvince] sp ON sp.[StateProvinceID] = a.[StateProvinceID]
INNER JOIN [Person].[CountryRegion] cr ON cr.[CountryRegionCode] = sp.[CountryRegionCode]
LEFT OUTER JOIN [Sales].[SalesTerritory] st ON st.[TerritoryID] = s.[TerritoryID]
LEFT OUTER JOIN [Person].[EmailAddress] ea ON ea.[BusinessEntityID] = p.[BusinessEntityID]
LEFT OUTER JOIN [Person].[PersonPhone] pp ON pp.[BusinessEntityID] = p.[BusinessEntityID]
LEFT OUTER JOIN [Person].[PhoneNumberType] pnt ON pnt.[PhoneNumberTypeID] = pp.[PhoneNumberTypeID]
WHERE s.BusinessEntityID = @busEntityID;
GO
EXEC [Sales].[usp_SalesPerson] @busEntityID = 290;
GO