-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSales.GetSalesOrders.sql
More file actions
62 lines (58 loc) · 1.72 KB
/
Sales.GetSalesOrders.sql
File metadata and controls
62 lines (58 loc) · 1.72 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
USE AdventureWorks2014
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID ('[dbo].[SalesGetSalesOrders]', 'P') IS NOT NULL
DROP PROCEDURE [dbo].[SalesGetSalesOrders];
GO
CREATE PROCEDURE [dbo].[SalesGetSalesOrders] (@customerID int)
AS
BEGIN
SELECT TOP (10) Orders.SalesOrderID,
Orders.OrderDate,
Orders.DueDate,
Orders.ShipDate,
Orders.Status,
Orders.SalesOrderNumber,
Orders.PurchaseOrderNumber,
Orders.AccountNumber,
Orders.CustomerID,
Customer.PersonID,
Person.FirstName,
Person.LastName,
Orders.ShipToAddressID,
ShipToAddress.AddressLine1,
ShipToAddress.AddressLine2,
ShipToAddress.City,
ShipToAddress.StateProvinceID,
ProvinceCountryRegion.StateProvinceCode,
ProvinceCountryRegion.StateProvinceName,
ProvinceCountryRegion.CountryRegionCode,
ProvinceCountryRegion.CountryRegionName,
ShipToAddress.PostalCode,
Orders.ShipMethodID,
ShipMethod.[Name] AS ShipMethodName,
ShipMethod.ShipBase,
ShipMethod.ShipRate,
Orders.CurrencyRateID,
Orders.SubTotal,
Orders.TaxAmt,
Orders.Freight,
Orders.TotalDue,
Orders.rowguid,
Orders.ModifiedDate
FROM Sales.SalesOrderHeader AS Orders
INNER JOIN Sales.Customer AS Customer
ON Orders.CustomerID = Customer.CustomerID
INNER JOIN Person.Person AS Person
ON Customer.PersonID = Person.BusinessEntityID
INNER JOIN Person.[Address] As ShipToAddress
ON Orders.ShipToAddressID = ShipToAddress.AddressID
INNER JOIN Person.vStateProvinceCountryRegion as ProvinceCountryRegion
ON ShipToAddress.StateProvinceID = ProvinceCountryRegion.StateProvinceID
INNER JOIN Purchasing.ShipMethod AS ShipMethod
ON Orders.ShipMethodID = ShipMethod.ShipMethodID
WHERE (@customerID IS NULL) OR Orders.CustomerID = @customerID
END