1+ /* ****************************************************************************
2+ * FileName: Create Enlarged AdventureWorks Tables.sql
3+ *
4+ * Summary: Creates an enlarged version of the AdventureWorks database
5+ * for use in demonstrating SQL Server performance tuning and
6+ * execution plan issues.
7+ *
8+ * Date: November 14, 2011
9+ *
10+ * SQL Server Versions:
11+ * 2008, 2008R2, 2012
12+ *
13+ ******************************************************************************
14+ * Copyright (C) 2011 Jonathan M. Kehayias, SQLskills.com
15+ * All rights reserved.
16+ *
17+ * For more scripts and sample code, check out
18+ * https://www.sqlskills.com/blogs/jonathan/enlarging-the-adventureworks-sample-databases/
19+ *
20+ * You may alter this code for your own *non-commercial* purposes. You may
21+ * republish altered code as long as you include this copyright and give
22+ * due credit.
23+ *
24+ *
25+ * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
26+ * ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
27+ * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
28+ * PARTICULAR PURPOSE.
29+ *
30+ ******************************************************************************/
31+
32+
33+
34+ USE AdventureWorks2008R2;
35+ GO
36+
37+ IF OBJECT_ID (' Sales.SalesOrderHeaderEnlarged' ) IS NOT NULL
38+ DROP TABLE Sales .SalesOrderHeaderEnlarged ;
39+ GO
40+
41+ CREATE TABLE Sales .SalesOrderHeaderEnlarged
42+ (
43+ SalesOrderID int NOT NULL IDENTITY (1 , 1 ) NOT FOR REPLICATION,
44+ RevisionNumber tinyint NOT NULL ,
45+ OrderDate datetime NOT NULL ,
46+ DueDate datetime NOT NULL ,
47+ ShipDate datetime NULL ,
48+ Status tinyint NOT NULL ,
49+ OnlineOrderFlag dbo .Flag NOT NULL ,
50+ SalesOrderNumber AS (isnull (N ' SO'+ CONVERT ([nvarchar](23 ),[SalesOrderID],0 ),N ' *** ERROR ***' )),
51+ PurchaseOrderNumber dbo .OrderNumber NULL ,
52+ AccountNumber dbo .AccountNumber NULL ,
53+ CustomerID int NOT NULL ,
54+ SalesPersonID int NULL ,
55+ TerritoryID int NULL ,
56+ BillToAddressID int NOT NULL ,
57+ ShipToAddressID int NOT NULL ,
58+ ShipMethodID int NOT NULL ,
59+ CreditCardID int NULL ,
60+ CreditCardApprovalCode varchar (15 ) NULL ,
61+ CurrencyRateID int NULL ,
62+ SubTotal money NOT NULL ,
63+ TaxAmt money NOT NULL ,
64+ Freight money NOT NULL ,
65+ TotalDue AS (isnull (([SubTotal]+ [TaxAmt])+ [Freight],(0 ))),
66+ Comment nvarchar (128 ) NULL ,
67+ rowguid uniqueidentifier NOT NULL ROWGUIDCOL ,
68+ ModifiedDate datetime NOT NULL
69+ ) ON [PRIMARY]
70+ GO
71+
72+ SET IDENTITY_INSERT Sales .SalesOrderHeaderEnlarged ON
73+ GO
74+ INSERT INTO Sales .SalesOrderHeaderEnlarged (SalesOrderID, RevisionNumber, OrderDate, DueDate, ShipDate, Status , OnlineOrderFlag, PurchaseOrderNumber, AccountNumber, CustomerID, SalesPersonID, TerritoryID, BillToAddressID, ShipToAddressID, ShipMethodID, CreditCardID, CreditCardApprovalCode, CurrencyRateID, SubTotal, TaxAmt, Freight, Comment, rowguid, ModifiedDate)
75+ SELECT SalesOrderID, RevisionNumber, OrderDate, DueDate, ShipDate, Status , OnlineOrderFlag, PurchaseOrderNumber, AccountNumber, CustomerID, SalesPersonID, TerritoryID, BillToAddressID, ShipToAddressID, ShipMethodID, CreditCardID, CreditCardApprovalCode, CurrencyRateID, SubTotal, TaxAmt, Freight, Comment, rowguid, ModifiedDate
76+ FROM Sales .SalesOrderHeader WITH (HOLDLOCK TABLOCKX )
77+ GO
78+ SET IDENTITY_INSERT Sales .SalesOrderHeaderEnlarged OFF
79+
80+ GO
81+ ALTER TABLE Sales .SalesOrderHeaderEnlarged ADD CONSTRAINT
82+ PK_SalesOrderHeaderEnlarged_SalesOrderID PRIMARY KEY CLUSTERED
83+ (
84+ SalesOrderID
85+ ) WITH ( STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
86+
87+ GO
88+
89+ CREATE UNIQUE NONCLUSTERED INDEX AK_SalesOrderHeaderEnlarged_rowguid ON Sales .SalesOrderHeaderEnlarged
90+ (
91+ rowguid
92+ ) WITH ( STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
93+ GO
94+
95+ CREATE UNIQUE NONCLUSTERED INDEX AK_SalesOrderHeaderEnlarged_SalesOrderNumber ON Sales .SalesOrderHeaderEnlarged
96+ (
97+ SalesOrderNumber
98+ ) WITH ( STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
99+ GO
100+
101+ CREATE NONCLUSTERED INDEX IX_SalesOrderHeaderEnlarged_CustomerID ON Sales .SalesOrderHeaderEnlarged
102+ (
103+ CustomerID
104+ ) WITH ( STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
105+ GO
106+
107+ CREATE NONCLUSTERED INDEX IX_SalesOrderHeaderEnlarged_SalesPersonID ON Sales .SalesOrderHeaderEnlarged
108+ (
109+ SalesPersonID
110+ ) WITH ( STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
111+ GO
112+
113+ IF OBJECT_ID (' Sales.SalesOrderDetailEnlarged' ) IS NOT NULL
114+ DROP TABLE Sales .SalesOrderDetailEnlarged ;
115+ GO
116+ CREATE TABLE Sales .SalesOrderDetailEnlarged
117+ (
118+ SalesOrderID int NOT NULL ,
119+ SalesOrderDetailID int NOT NULL IDENTITY (1 , 1 ),
120+ CarrierTrackingNumber nvarchar (25 ) NULL ,
121+ OrderQty smallint NOT NULL ,
122+ ProductID int NOT NULL ,
123+ SpecialOfferID int NOT NULL ,
124+ UnitPrice money NOT NULL ,
125+ UnitPriceDiscount money NOT NULL ,
126+ LineTotal AS (isnull (([UnitPrice]* ((1 .0 )- [UnitPriceDiscount]))* [OrderQty],(0 .0 ))),
127+ rowguid uniqueidentifier NOT NULL ROWGUIDCOL ,
128+ ModifiedDate datetime NOT NULL
129+ ) ON [PRIMARY]
130+ GO
131+
132+ SET IDENTITY_INSERT Sales .SalesOrderDetailEnlarged ON
133+ GO
134+ INSERT INTO Sales .SalesOrderDetailEnlarged (SalesOrderID, SalesOrderDetailID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, rowguid, ModifiedDate)
135+ SELECT SalesOrderID, SalesOrderDetailID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, rowguid, ModifiedDate
136+ FROM Sales .SalesOrderDetail WITH (HOLDLOCK TABLOCKX )
137+ GO
138+ SET IDENTITY_INSERT Sales .SalesOrderDetailEnlarged OFF
139+ GO
140+ ALTER TABLE Sales .SalesOrderDetailEnlarged ADD CONSTRAINT
141+ PK_SalesOrderDetailEnlarged_SalesOrderID_SalesOrderDetailID PRIMARY KEY CLUSTERED
142+ (
143+ SalesOrderID,
144+ SalesOrderDetailID
145+ ) WITH ( STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
146+
147+ GO
148+ CREATE UNIQUE NONCLUSTERED INDEX AK_SalesOrderDetailEnlarged_rowguid ON Sales .SalesOrderDetailEnlarged
149+ (
150+ rowguid
151+ ) WITH ( STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
152+ GO
153+ CREATE NONCLUSTERED INDEX IX_SalesOrderDetailEnlarged_ProductID ON Sales .SalesOrderDetailEnlarged
154+ (
155+ ProductID
156+ ) WITH ( STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
157+ GO
158+
159+
160+ BEGIN TRANSACTION
161+
162+
163+ DECLARE @TableVar TABLE
164+ (OrigSalesOrderID int , NewSalesOrderID int )
165+
166+ INSERT INTO Sales .SalesOrderHeaderEnlarged
167+ (RevisionNumber, OrderDate, DueDate, ShipDate, Status , OnlineOrderFlag,
168+ PurchaseOrderNumber, AccountNumber, CustomerID, SalesPersonID, TerritoryID,
169+ BillToAddressID, ShipToAddressID, ShipMethodID, CreditCardID,
170+ CreditCardApprovalCode, CurrencyRateID, SubTotal, TaxAmt, Freight, Comment,
171+ rowguid, ModifiedDate)
172+ OUTPUT inserted .Comment , inserted .SalesOrderID
173+ INTO @TableVar
174+ SELECT RevisionNumber, DATEADD (dd, number , OrderDate) AS OrderDate,
175+ DATEADD (dd, number , DueDate), DATEADD (dd, number , ShipDate),
176+ Status , OnlineOrderFlag,
177+ PurchaseOrderNumber,
178+ AccountNumber,
179+ CustomerID, SalesPersonID, TerritoryID, BillToAddressID,
180+ ShipToAddressID, ShipMethodID, CreditCardID, CreditCardApprovalCode,
181+ CurrencyRateID, SubTotal, TaxAmt, Freight, SalesOrderID,
182+ NEWID (), DATEADD (dd, number , ModifiedDate)
183+ FROM Sales .SalesOrderHeader AS soh WITH (HOLDLOCK TABLOCKX )
184+ CROSS JOIN (
185+ SELECT number
186+ FROM (SELECT TOP 10 number
187+ FROM master .dbo .spt_values
188+ WHERE type = N ' P'
189+ AND number < 1000
190+ ORDER BY NEWID () DESC
191+ UNION
192+ SELECT TOP 10 number
193+ FROM master .dbo .spt_values
194+ WHERE type = N ' P'
195+ AND number < 1000
196+ ORDER BY NEWID () DESC
197+ UNION
198+ SELECT TOP 10 number
199+ FROM master .dbo .spt_values
200+ WHERE type = N ' P'
201+ AND number < 1000
202+ ORDER BY NEWID () DESC
203+ UNION
204+ SELECT TOP 10 number
205+ FROM master .dbo .spt_values
206+ WHERE type = N ' P'
207+ AND number < 1000
208+ ORDER BY NEWID () DESC
209+ ) AS tab
210+ ) AS Randomizer
211+ ORDER BY OrderDate, number
212+
213+ INSERT INTO Sales .SalesOrderDetailEnlarged
214+ (SalesOrderID, CarrierTrackingNumber, OrderQty, ProductID,
215+ SpecialOfferID, UnitPrice, UnitPriceDiscount, rowguid, ModifiedDate)
216+ SELECT
217+ tv .NewSalesOrderID , CarrierTrackingNumber, OrderQty, ProductID,
218+ SpecialOfferID, UnitPrice, UnitPriceDiscount, NEWID (), ModifiedDate
219+ FROM Sales .SalesOrderDetail AS sod
220+ JOIN @TableVar AS tv
221+ ON sod .SalesOrderID = tv .OrigSalesOrderID
222+ ORDER BY sod .SalesOrderDetailID
223+
224+ COMMIT
0 commit comments