Difference between revisions of "Sales.SalesOrderHeader (table)"
Line 12: | Line 12: | ||
+ | === Columns === | ||
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse" | {| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse" | ||
|- style="background:silver" | |- style="background:silver" | ||
Line 18: | Line 19: | ||
| '''Nullable | | '''Nullable | ||
| '''Default | | '''Default | ||
− | | '''Description | + | | '''Description |
|- valign="top" | |- valign="top" | ||
Line 25: | Line 26: | ||
| not null | | not null | ||
| | | | ||
− | | Primary key. | + | | Primary key. |
|- valign="top" | |- valign="top" | ||
| RevisionNumber | | RevisionNumber | ||
Line 31: | Line 32: | ||
| not null | | not null | ||
| ((0)) | | ((0)) | ||
− | | Incremental number to track changes to the sales order over time. | + | | Incremental number to track changes to the sales order over time. |
|- valign="top" | |- valign="top" | ||
| OrderDate | | OrderDate | ||
Line 37: | Line 38: | ||
| not null | | not null | ||
| (getdate()) | | (getdate()) | ||
− | | Dates the sales order was created. | + | | Dates the sales order was created. |
|- valign="top" | |- valign="top" | ||
| DueDate | | DueDate | ||
Line 43: | Line 44: | ||
| not null | | not null | ||
| | | | ||
− | | Date the order is due to the customer. | + | | Date the order is due to the customer. |
|- valign="top" | |- valign="top" | ||
| ShipDate | | ShipDate | ||
Line 49: | Line 50: | ||
| null | | null | ||
| | | | ||
− | | Date the order was shipped to the customer. | + | | Date the order was shipped to the customer. |
|- valign="top" | |- valign="top" | ||
| Status | | Status | ||
Line 55: | Line 56: | ||
| not null | | not null | ||
| ((1)) | | ((1)) | ||
− | | Order current status. 1 = In process; 2 = Approved; 3 = Backordered; 4 = Rejected; 5 = Shipped; 6 = Cancelled | + | | Order current status. 1 = In process; 2 = Approved; 3 = Backordered; 4 = Rejected; 5 = Shipped; 6 = Cancelled |
|- valign="top" | |- valign="top" | ||
| OnlineOrderFlag | | OnlineOrderFlag | ||
Line 61: | Line 62: | ||
| not null | | not null | ||
| ((1)) | | ((1)) | ||
− | | 0 = Order placed by sales person. 1 = Order placed online by customer. | + | | 0 = Order placed by sales person. 1 = Order placed online by customer. |
|- valign="top" | |- valign="top" | ||
| SalesOrderNumber | | SalesOrderNumber | ||
Line 67: | Line 68: | ||
| | | | ||
| | | | ||
− | | Unique sales order identification number. | + | | Unique sales order identification number. |
|- valign="top" | |- valign="top" | ||
| PurchaseOrderNumber | | PurchaseOrderNumber | ||
Line 73: | Line 74: | ||
| null | | null | ||
| | | | ||
− | | Customer purchase order number reference. | + | | Customer purchase order number reference. |
|- valign="top" | |- valign="top" | ||
| AccountNumber | | AccountNumber | ||
Line 79: | Line 80: | ||
| null | | null | ||
| | | | ||
− | | Financial accounting number reference. | + | | Financial accounting number reference. |
|- valign="top" | |- valign="top" | ||
| CustomerID | | CustomerID | ||
Line 85: | Line 86: | ||
| not null | | not null | ||
| | | | ||
− | | Customer identification number. Foreign key to Customer.CustomerID. | + | | Customer identification number. Foreign key to Customer.CustomerID. |
|- valign="top" | |- valign="top" | ||
| ContactID | | ContactID | ||
Line 91: | Line 92: | ||
| not null | | not null | ||
| | | | ||
− | | Customer contact identification number. Foreign key to Contact.ContactID. | + | | Customer contact identification number. Foreign key to Contact.ContactID. |
|- valign="top" | |- valign="top" | ||
| SalesPersonID | | SalesPersonID | ||
Line 97: | Line 98: | ||
| null | | null | ||
| | | | ||
− | | Sales person who created the sales order. Foreign key to SalesPerson.SalePersonID. | + | | Sales person who created the sales order. Foreign key to SalesPerson.SalePersonID. |
|- valign="top" | |- valign="top" | ||
| TerritoryID | | TerritoryID | ||
Line 103: | Line 104: | ||
| null | | null | ||
| | | | ||
− | | Territory in which the sale was made. Foreign key to SalesTerritory.SalesTerritoryID. | + | | Territory in which the sale was made. Foreign key to SalesTerritory.SalesTerritoryID. |
|- valign="top" | |- valign="top" | ||
| BillToAddressID | | BillToAddressID | ||
Line 109: | Line 110: | ||
| not null | | not null | ||
| | | | ||
− | | Customer billing address. Foreign key to Address.AddressID. | + | | Customer billing address. Foreign key to Address.AddressID. |
|- valign="top" | |- valign="top" | ||
| ShipToAddressID | | ShipToAddressID | ||
Line 115: | Line 116: | ||
| not null | | not null | ||
| | | | ||
− | | Customer shipping address. Foreign key to Address.AddressID. | + | | Customer shipping address. Foreign key to Address.AddressID. |
|- valign="top" | |- valign="top" | ||
| ShipMethodID | | ShipMethodID | ||
Line 121: | Line 122: | ||
| not null | | not null | ||
| | | | ||
− | | Shipping method. Foreign key to ShipMethod.ShipMethodID. | + | | Shipping method. Foreign key to ShipMethod.ShipMethodID. |
|- valign="top" | |- valign="top" | ||
| CreditCardID | | CreditCardID | ||
Line 127: | Line 128: | ||
| null | | null | ||
| | | | ||
− | | Credit card identification number. Foreign key to CreditCard.CreditCardID. | + | | Credit card identification number. Foreign key to CreditCard.CreditCardID. |
|- valign="top" | |- valign="top" | ||
| CreditCardApprovalCode | | CreditCardApprovalCode | ||
Line 133: | Line 134: | ||
| null | | null | ||
| | | | ||
− | | Approval code provided by the credit card company. | + | | Approval code provided by the credit card company. |
|- valign="top" | |- valign="top" | ||
| CurrencyRateID | | CurrencyRateID | ||
Line 139: | Line 140: | ||
| null | | null | ||
| | | | ||
− | | Currency exchange rate used. Foreign key to CurrencyRate.CurrencyRateID. | + | | Currency exchange rate used. Foreign key to CurrencyRate.CurrencyRateID. |
|- valign="top" | |- valign="top" | ||
| SubTotal | | SubTotal | ||
Line 145: | Line 146: | ||
| not null | | not null | ||
| ((0.00)) | | ((0.00)) | ||
− | | Sales subtotal. Computed as SUM(SalesOrderDetail.LineTotal)for the appropriate SalesOrderID. | + | | Sales subtotal. Computed as SUM(SalesOrderDetail.LineTotal)for the appropriate SalesOrderID. |
|- valign="top" | |- valign="top" | ||
| TaxAmt | | TaxAmt | ||
Line 151: | Line 152: | ||
| not null | | not null | ||
| ((0.00)) | | ((0.00)) | ||
− | | Tax amount. | + | | Tax amount. |
|- valign="top" | |- valign="top" | ||
| Freight | | Freight | ||
Line 157: | Line 158: | ||
| not null | | not null | ||
| ((0.00)) | | ((0.00)) | ||
− | | Shipping cost. | + | | Shipping cost. |
|- valign="top" | |- valign="top" | ||
| TotalDue | | TotalDue | ||
Line 163: | Line 164: | ||
| | | | ||
| | | | ||
− | | Total due from customer. Computed as Subtotal + TaxAmt + Freight. | + | | Total due from customer. Computed as Subtotal + TaxAmt + Freight. |
|- valign="top" | |- valign="top" | ||
| Comment | | Comment | ||
Line 169: | Line 170: | ||
| null | | null | ||
| | | | ||
− | | Sales representative comments. | + | | Sales representative comments. |
|- valign="top" | |- valign="top" | ||
| rowguid | | rowguid | ||
Line 175: | Line 176: | ||
| not null | | not null | ||
| (newid()) | | (newid()) | ||
− | | ROWGUIDCOL number uniquely identifying the record. Used to support a merge replication sample. | + | | ROWGUIDCOL number uniquely identifying the record. Used to support a merge replication sample. |
|- valign="top" | |- valign="top" | ||
| ModifiedDate | | ModifiedDate | ||
Line 181: | Line 182: | ||
| not null | | not null | ||
| (getdate()) | | (getdate()) | ||
− | | Date and time the record was last updated. | + | | Date and time the record was last updated. |
|} | |} | ||
+ | === Primary Key === | ||
+ | {| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse" | ||
+ | |- style="background:silver" | ||
+ | | '''Primary Key | ||
+ | | '''Columns | ||
+ | |- | ||
+ | | PK_SalesOrderHeader_SalesOrderID | ||
+ | | SalesOrderID | ||
+ | |} | ||
+ | |||
+ | === Indexes === | ||
+ | {| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse" | ||
+ | |- style="background:silver" | ||
+ | | '''Index | ||
+ | | '''Type | ||
+ | | '''Columns | ||
+ | |||
+ | |- | ||
+ | | AK_SalesOrderHeader_rowguid | ||
+ | | Unique | ||
+ | | rowguid | ||
+ | |- | ||
+ | | AK_SalesOrderHeader_SalesOrderNumber | ||
+ | | Unique | ||
+ | | SalesOrderNumber | ||
+ | |- | ||
+ | | IX_SalesOrderHeader_CustomerID | ||
+ | | | ||
+ | | CustomerID | ||
+ | |- | ||
+ | | IX_SalesOrderHeader_SalesPersonID | ||
+ | | | ||
+ | | SalesPersonID | ||
+ | |} | ||
+ | === Check Constraints === | ||
+ | {| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse" | ||
+ | |- style="background:silver" | ||
+ | | '''Check Constraint | ||
+ | | '''Expression | ||
+ | | '''Description | ||
+ | |||
+ | |- | ||
+ | | CK_SalesOrderHeader_DueDate | ||
+ | | ([DueDate]>=[OrderDate]) | ||
+ | | Check constraint [DueDate] >= [OrderDate] | ||
+ | |- | ||
+ | | CK_SalesOrderHeader_Freight | ||
+ | | ([Freight]>=(0.00)) | ||
+ | | Check constraint [Freight] >= (0.00) | ||
+ | |- | ||
+ | | CK_SalesOrderHeader_ShipDate | ||
+ | | ([ShipDate]>=[OrderDate] OR [ShipDate] IS NULL) | ||
+ | | Check constraint [ShipDate] >= [OrderDate] OR [ShipDate] IS NULL | ||
+ | |- | ||
+ | | CK_SalesOrderHeader_Status | ||
+ | | ([Status]>=(0) AND [Status]<=(8)) | ||
+ | | Check constraint [Status] BETWEEN (0) AND (8) | ||
+ | |- | ||
+ | | CK_SalesOrderHeader_SubTotal | ||
+ | | ([SubTotal]>=(0.00)) | ||
+ | | Check constraint [SubTotal] >= (0.00) | ||
+ | |- | ||
+ | | CK_SalesOrderHeader_TaxAmt | ||
+ | | ([TaxAmt]>=(0.00)) | ||
+ | | Check constraint [TaxAmt] >= (0.00) | ||
+ | |} | ||
+ | === Foreign Keys === | ||
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse" | {| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse" | ||
|- style="background:silver" | |- style="background:silver" | ||
Line 230: | Line 298: | ||
|} | |} | ||
− | + | === Detail Tables === | |
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse" | {| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse" | ||
|- style="background:silver" | |- style="background:silver" | ||
Line 247: | Line 315: | ||
|} | |} | ||
− | + | === Triggers === | |
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse" | {| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse" | ||
|- style="background:silver" | |- style="background:silver" | ||
Line 258: | Line 326: | ||
|} | |} | ||
+ | Trigger uSalesOrderHeader | ||
+ | <pre> | ||
+ | CREATE TRIGGER [Sales].[uSalesOrderHeader] ON [Sales].[SalesOrderHeader] | ||
+ | AFTER UPDATE NOT FOR REPLICATION AS | ||
+ | BEGIN | ||
+ | DECLARE @Count int; | ||
+ | |||
+ | SET @Count = @@ROWCOUNT; | ||
+ | IF @Count = 0 | ||
+ | RETURN; | ||
+ | |||
+ | SET NOCOUNT ON; | ||
+ | |||
+ | BEGIN TRY | ||
+ | -- Update RevisionNumber for modification of any field EXCEPT the Status. | ||
+ | IF NOT UPDATE([Status]) | ||
+ | BEGIN | ||
+ | UPDATE [Sales].[SalesOrderHeader] | ||
+ | SET [Sales].[SalesOrderHeader].[RevisionNumber] = | ||
+ | [Sales].[SalesOrderHeader].[RevisionNumber] + 1 | ||
+ | WHERE [Sales].[SalesOrderHeader].[SalesOrderID] IN | ||
+ | (SELECT inserted.[SalesOrderID] FROM inserted); | ||
+ | END; | ||
+ | |||
+ | -- Update the SalesPerson SalesYTD when SubTotal is updated | ||
+ | IF UPDATE([SubTotal]) | ||
+ | BEGIN | ||
+ | DECLARE @StartDate datetime, | ||
+ | @EndDate datetime | ||
+ | |||
+ | SET @StartDate = [dbo].[ufnGetAccountingStartDate](); | ||
+ | SET @EndDate = [dbo].[ufnGetAccountingEndDate](); | ||
+ | |||
+ | UPDATE [Sales].[SalesPerson] | ||
+ | SET [Sales].[SalesPerson].[SalesYTD] = | ||
+ | (SELECT SUM([Sales].[SalesOrderHeader].[SubTotal]) | ||
+ | FROM [Sales].[SalesOrderHeader] | ||
+ | WHERE [Sales].[SalesPerson].[SalesPersonID] = [Sales].[SalesOrderHeader].[SalesPersonID] | ||
+ | AND ([Sales].[SalesOrderHeader].[Status] = 5) -- Shipped | ||
+ | AND [Sales].[SalesOrderHeader].[OrderDate] BETWEEN @StartDate AND @EndDate) | ||
+ | WHERE [Sales].[SalesPerson].[SalesPersonID] | ||
+ | IN (SELECT DISTINCT inserted.[SalesPersonID] FROM inserted | ||
+ | WHERE inserted.[OrderDate] BETWEEN @StartDate AND @EndDate); | ||
+ | |||
+ | -- Update the SalesTerritory SalesYTD when SubTotal is updated | ||
+ | UPDATE [Sales].[SalesTerritory] | ||
+ | SET [Sales].[SalesTerritory].[SalesYTD] = | ||
+ | (SELECT SUM([Sales].[SalesOrderHeader].[SubTotal]) | ||
+ | FROM [Sales].[SalesOrderHeader] | ||
+ | WHERE [Sales].[SalesTerritory].[TerritoryID] = [Sales].[SalesOrderHeader].[TerritoryID] | ||
+ | AND ([Sales].[SalesOrderHeader].[Status] = 5) -- Shipped | ||
+ | AND [Sales].[SalesOrderHeader].[OrderDate] BETWEEN @StartDate AND @EndDate) | ||
+ | WHERE [Sales].[SalesTerritory].[TerritoryID] | ||
+ | IN (SELECT DISTINCT inserted.[TerritoryID] FROM inserted | ||
+ | WHERE inserted.[OrderDate] BETWEEN @StartDate AND @EndDate); | ||
+ | END; | ||
+ | END TRY | ||
+ | BEGIN CATCH | ||
+ | EXECUTE [dbo].[uspPrintError]; | ||
+ | -- Rollback any active or uncommittable transactions before | ||
+ | -- inserting information in the ErrorLog | ||
+ | IF @@TRANCOUNT > 0 | ||
+ | BEGIN | ||
+ | ROLLBACK TRANSACTION; | ||
+ | END | ||
+ | |||
+ | EXECUTE [dbo].[uspLogError]; | ||
+ | END CATCH; | ||
+ | END; | ||
+ | </pre> | ||
+ | |||
+ | === References === | ||
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse" | {| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse" | ||
|- style="background:silver" | |- style="background:silver" | ||
Line 286: | Line 426: | ||
| Type | | Type | ||
| [[dbo.OrderNumber_(type)|dbo.OrderNumber]] | | [[dbo.OrderNumber_(type)|dbo.OrderNumber]] | ||
+ | |||
+ | | | ||
+ | | | ||
+ | |- | ||
+ | | Schema | ||
+ | | Schema | ||
+ | | [[Sales_(schema)|Sales]] | ||
| | | | ||
Line 347: | Line 494: | ||
|} | |} | ||
− | + | === Dependencies === | |
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse" | {| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse" | ||
|- style="background:silver" | |- style="background:silver" | ||
Line 393: | Line 540: | ||
| uSalesOrderHeader | | uSalesOrderHeader | ||
|} | |} | ||
− | |||
Revision as of 23:06, 2 February 2010
Contents
wikibot
Table | Sales.SalesOrderHeader |
Description | General sales order information. |
Columns
Column | Data Type | Nullable | Default | Description |
SalesOrderID | int | not null | Primary key. | |
RevisionNumber | tinyint | not null | ((0)) | Incremental number to track changes to the sales order over time. |
OrderDate | datetime | not null | (getdate()) | Dates the sales order was created. |
DueDate | datetime | not null | Date the order is due to the customer. | |
ShipDate | datetime | null | Date the order was shipped to the customer. | |
Status | tinyint | not null | ((1)) | Order current status. 1 = In process; 2 = Approved; 3 = Backordered; 4 = Rejected; 5 = Shipped; 6 = Cancelled |
OnlineOrderFlag | dbo.Flag | not null | ((1)) | 0 = Order placed by sales person. 1 = Order placed online by customer. |
SalesOrderNumber | Unique sales order identification number. | |||
PurchaseOrderNumber | dbo.OrderNumber | null | Customer purchase order number reference. | |
AccountNumber | dbo.AccountNumber | null | Financial accounting number reference. | |
CustomerID | int | not null | Customer identification number. Foreign key to Customer.CustomerID. | |
ContactID | int | not null | Customer contact identification number. Foreign key to Contact.ContactID. | |
SalesPersonID | int | null | Sales person who created the sales order. Foreign key to SalesPerson.SalePersonID. | |
TerritoryID | int | null | Territory in which the sale was made. Foreign key to SalesTerritory.SalesTerritoryID. | |
BillToAddressID | int | not null | Customer billing address. Foreign key to Address.AddressID. | |
ShipToAddressID | int | not null | Customer shipping address. Foreign key to Address.AddressID. | |
ShipMethodID | int | not null | Shipping method. Foreign key to ShipMethod.ShipMethodID. | |
CreditCardID | int | null | Credit card identification number. Foreign key to CreditCard.CreditCardID. | |
CreditCardApprovalCode | varchar(15) | null | Approval code provided by the credit card company. | |
CurrencyRateID | int | null | Currency exchange rate used. Foreign key to CurrencyRate.CurrencyRateID. | |
SubTotal | money | not null | ((0.00)) | Sales subtotal. Computed as SUM(SalesOrderDetail.LineTotal)for the appropriate SalesOrderID. |
TaxAmt | money | not null | ((0.00)) | Tax amount. |
Freight | money | not null | ((0.00)) | Shipping cost. |
TotalDue | Total due from customer. Computed as Subtotal + TaxAmt + Freight. | |||
Comment | nvarchar(128) | null | Sales representative comments. | |
rowguid | uniqueidentifier | not null | (newid()) | ROWGUIDCOL number uniquely identifying the record. Used to support a merge replication sample. |
ModifiedDate | datetime | not null | (getdate()) | Date and time the record was last updated. |
Primary Key
Primary Key | Columns |
PK_SalesOrderHeader_SalesOrderID | SalesOrderID |
Indexes
Index | Type | Columns |
AK_SalesOrderHeader_rowguid | Unique | rowguid |
AK_SalesOrderHeader_SalesOrderNumber | Unique | SalesOrderNumber |
IX_SalesOrderHeader_CustomerID | CustomerID | |
IX_SalesOrderHeader_SalesPersonID | SalesPersonID |
Check Constraints
Check Constraint | Expression | Description |
CK_SalesOrderHeader_DueDate | ([DueDate]>=[OrderDate]) | Check constraint [DueDate] >= [OrderDate] |
CK_SalesOrderHeader_Freight | ([Freight]>=(0.00)) | Check constraint [Freight] >= (0.00) |
CK_SalesOrderHeader_ShipDate | ([ShipDate]>=[OrderDate] OR [ShipDate] IS NULL) | Check constraint [ShipDate] >= [OrderDate] OR [ShipDate] IS NULL |
CK_SalesOrderHeader_Status | ([Status]>=(0) AND [Status]<=(8)) | Check constraint [Status] BETWEEN (0) AND (8) |
CK_SalesOrderHeader_SubTotal | ([SubTotal]>=(0.00)) | Check constraint [SubTotal] >= (0.00) |
CK_SalesOrderHeader_TaxAmt | ([TaxAmt]>=(0.00)) | Check constraint [TaxAmt] >= (0.00) |
Foreign Keys
Relation | Column | Referenced Column |
Person.Address | BillToAddressID | AddressID |
Person.Address | ShipToAddressID | AddressID |
Person.Contact | ContactID | ContactID |
Sales.CreditCard | CreditCardID | CreditCardID |
Sales.CurrencyRate | CurrencyRateID | CurrencyRateID |
Sales.Customer | CustomerID | CustomerID |
Sales.SalesPerson | SalesPersonID | SalesPersonID |
Sales.SalesTerritory | TerritoryID | TerritoryID |
Purchasing.ShipMethod | ShipMethodID | ShipMethodID |
Detail Tables
Detail Table | Column | Referencing Column |
Sales.SalesOrderDetail | SalesOrderID | SalesOrderID |
Sales.SalesOrderHeaderSalesReason | SalesOrderID | SalesOrderID |
Triggers
Triggers | Type |
uSalesOrderHeader | ON UPDATE |
Trigger uSalesOrderHeader
CREATE TRIGGER [Sales].[uSalesOrderHeader] ON [Sales].[SalesOrderHeader] AFTER UPDATE NOT FOR REPLICATION AS BEGIN DECLARE @Count int; SET @Count = @@ROWCOUNT; IF @Count = 0 RETURN; SET NOCOUNT ON; BEGIN TRY -- Update RevisionNumber for modification of any field EXCEPT the Status. IF NOT UPDATE([Status]) BEGIN UPDATE [Sales].[SalesOrderHeader] SET [Sales].[SalesOrderHeader].[RevisionNumber] = [Sales].[SalesOrderHeader].[RevisionNumber] + 1 WHERE [Sales].[SalesOrderHeader].[SalesOrderID] IN (SELECT inserted.[SalesOrderID] FROM inserted); END; -- Update the SalesPerson SalesYTD when SubTotal is updated IF UPDATE([SubTotal]) BEGIN DECLARE @StartDate datetime, @EndDate datetime SET @StartDate = [dbo].[ufnGetAccountingStartDate](); SET @EndDate = [dbo].[ufnGetAccountingEndDate](); UPDATE [Sales].[SalesPerson] SET [Sales].[SalesPerson].[SalesYTD] = (SELECT SUM([Sales].[SalesOrderHeader].[SubTotal]) FROM [Sales].[SalesOrderHeader] WHERE [Sales].[SalesPerson].[SalesPersonID] = [Sales].[SalesOrderHeader].[SalesPersonID] AND ([Sales].[SalesOrderHeader].[Status] = 5) -- Shipped AND [Sales].[SalesOrderHeader].[OrderDate] BETWEEN @StartDate AND @EndDate) WHERE [Sales].[SalesPerson].[SalesPersonID] IN (SELECT DISTINCT inserted.[SalesPersonID] FROM inserted WHERE inserted.[OrderDate] BETWEEN @StartDate AND @EndDate); -- Update the SalesTerritory SalesYTD when SubTotal is updated UPDATE [Sales].[SalesTerritory] SET [Sales].[SalesTerritory].[SalesYTD] = (SELECT SUM([Sales].[SalesOrderHeader].[SubTotal]) FROM [Sales].[SalesOrderHeader] WHERE [Sales].[SalesTerritory].[TerritoryID] = [Sales].[SalesOrderHeader].[TerritoryID] AND ([Sales].[SalesOrderHeader].[Status] = 5) -- Shipped AND [Sales].[SalesOrderHeader].[OrderDate] BETWEEN @StartDate AND @EndDate) WHERE [Sales].[SalesTerritory].[TerritoryID] IN (SELECT DISTINCT inserted.[TerritoryID] FROM inserted WHERE inserted.[OrderDate] BETWEEN @StartDate AND @EndDate); END; END TRY BEGIN CATCH EXECUTE [dbo].[uspPrintError]; -- Rollback any active or uncommittable transactions before -- inserting information in the ErrorLog IF @@TRANCOUNT > 0 BEGIN ROLLBACK TRANSACTION; END EXECUTE [dbo].[uspLogError]; END CATCH; END;
References
Dependency Type | Object Type | Referenced Object | Child Type | Child Object |
Data Type | Type | dbo.AccountNumber | ||
Data Type | Type | dbo.Flag | ||
Data Type | Type | dbo.OrderNumber | ||
Schema | Schema | Sales | ||
Select | Table | Sales.SalesOrderHeader | Trigger | uSalesOrderHeader |
Update | Table | Sales.SalesOrderHeader | Trigger | uSalesOrderHeader |
Update | Table | Sales.SalesPerson | Trigger | uSalesOrderHeader |
Update | Table | Sales.SalesTerritory | Trigger | uSalesOrderHeader |
Execute | Procedure | dbo.uspLogError | Trigger | uSalesOrderHeader |
Execute | Procedure | dbo.uspPrintError | Trigger | uSalesOrderHeader |
Execute | Function | dbo.ufnGetAccountingEndDate | Trigger | uSalesOrderHeader |
Execute | Function | dbo.ufnGetAccountingStartDate | Trigger | uSalesOrderHeader |
Dependencies
Reference Type | Object Type | Referencing Object | Child Type | Child Object |
Select | View | Sales.vSalesPersonSalesByFiscalYears | ||
Select | Table | Sales.SalesOrderDetail | Trigger | iduSalesOrderDetail |
Update | Table | Sales.SalesOrderDetail | Trigger | iduSalesOrderDetail |
Select | Table | Sales.SalesOrderHeader | Trigger | uSalesOrderHeader |
Update | Table | Sales.SalesOrderHeader | Trigger | uSalesOrderHeader |
automatically generated
Table | Sales.SalesOrderHeader |
Description | General sales order information. |
Column | Data Type | Nullable | Default | Description / PK / Index |
SalesOrderID | int | not null | Primary key. PK_SalesOrderHeader_SalesOrderID | |
RevisionNumber | tinyint | not null | (0) | Incremental number to track changes to the sales order over time. |
OrderDate | datetime | not null | (GETDATE()) | Dates the sales order was created. |
DueDate | datetime | not null | Date the order is due to the customer. | |
ShipDate | datetime | null | Date the order was shipped to the customer. | |
Status | tinyint | not null | (1) | Order current status. 1 = In process; 2 = Approved; 3 = Backordered; 4 = Rejected; 5 = Shipped; 6 = Cancelled |
OnlineOrderFlag | Flag | not null | (1) | 0 = Order placed by sales person. 1 = Order placed online by customer. |
SalesOrderNumber | Unique sales order identification number. AK_SalesOrderHeader_SalesOrderNumber | |||
PurchaseOrderNumber | OrderNumber | null | Customer purchase order number reference. | |
AccountNumber | AccountNumber | null | Financial accounting number reference. | |
CustomerID | int | not null | Customer identification number. Foreign key to Customer.CustomerID. IX_SalesOrderHeader_CustomerID | |
ContactID | int | not null | Customer contact identification number. Foreign key to Contact.ContactID. | |
SalesPersonID | int | null | Sales person who created the sales order. Foreign key to SalesPerson.SalePersonID. IX_SalesOrderHeader_SalesPersonID | |
TerritoryID | int | null | Territory in which the sale was made. Foreign key to SalesTerritory.SalesTerritoryID. | |
BillToAddressID | int | not null | Customer billing address. Foreign key to Address.AddressID. | |
ShipToAddressID | int | not null | Customer shipping address. Foreign key to Address.AddressID. | |
ShipMethodID | int | not null | Shipping method. Foreign key to ShipMethod.ShipMethodID. | |
CreditCardID | int | null | Credit card identification number. Foreign key to CreditCard.CreditCardID. | |
CreditCardApprovalCode | varchar(15) | null | Approval code provided by the credit card company. | |
CurrencyRateID | int | null | Currency exchange rate used. Foreign key to CurrencyRate.CurrencyRateID. | |
SubTotal | money | not null | (0.00) | Sales subtotal. Computed as SUM(SalesOrderDetail.LineTotal)for the appropriate SalesOrderID. |
TaxAmt | money | not null | (0.00) | Tax amount. |
Freight | money | not null | (0.00) | Shipping cost. |
TotalDue | Total due from customer. Computed as Subtotal + TaxAmt + Freight. | |||
Comment | nvarchar(128) | null | Sales representative comments. | |
rowguid | uniqueidentifier | not null | (NEWID()) | ROWGUIDCOL number uniquely identifying the record. Used to support a merge replication sample. AK_SalesOrderHeader_rowguid |
ModifiedDate | datetime | not null | (GETDATE()) | Date and time the record was last updated. |
Relation | Column | Referenced Column |
Person.Address | BillToAddressID | AddressID |
Person.Address | ShipToAddressID | AddressID |
Person.Contact | ContactID | ContactID |
Sales.CreditCard | CreditCardID | CreditCardID |
Sales.CurrencyRate | CurrencyRateID | CurrencyRateID |
Sales.Customer | CustomerID | CustomerID |
Sales.SalesPerson | SalesPersonID | SalesPersonID |
Sales.SalesTerritory | TerritoryID | TerritoryID |
Purchasing.ShipMethod | ShipMethodID | ShipMethodID |
Detail Table | Column | Referencing Column |
Sales.SalesOrderDetail | SalesOrderID | SalesOrderID |
Sales.SalesOrderHeaderSalesReason | SalesOrderID | SalesOrderID |
Triggers | Type |
uSalesOrderHeader | ON UPDATE |
Dependency Type | Object Type | Referenced Object | Child Type | Child Object |
Data Type | Type | dbo.AccountNumber | ||
Data Type | Type | dbo.Flag | ||
Data Type | Type | dbo.OrderNumber | ||
Schema | Schema | Sales | ||
Select | Table | Sales.SalesOrderHeader | Trigger | uSalesOrderHeader |
Update | Table | Sales.SalesOrderHeader | Trigger | uSalesOrderHeader |
Update | Table | Sales.SalesPerson | Trigger | uSalesOrderHeader |
Update | Table | Sales.SalesTerritory | Trigger | uSalesOrderHeader |
Execute | Procedure | dbo.uspLogError | Trigger | uSalesOrderHeader |
Execute | Procedure | dbo.uspPrintError | Trigger | uSalesOrderHeader |
Execute | Function | dbo.ufnGetAccountingEndDate | Trigger | uSalesOrderHeader |
Execute | Function | dbo.ufnGetAccountingStartDate | Trigger | uSalesOrderHeader |
Reference Type | Object Type | Referencing Object | Child Type | Child Object |
Select | View | Sales.vSalesPersonSalesByFiscalYears | ||
Select | Table | Sales.SalesOrderDetail | Trigger | iduSalesOrderDetail |
Update | Table | Sales.SalesOrderDetail | Trigger | iduSalesOrderDetail |
Select | Table | Sales.SalesOrderHeader | Trigger | uSalesOrderHeader |
Update | Table | Sales.SalesOrderHeader | Trigger | uSalesOrderHeader |