Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Apress
committed
Oct 11, 2016
0 parents
commit 93fcf6a
Showing
82 changed files
with
10,925 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Freeware License, some rights reserved | ||
|
||
Copyright (c) 2007 Adam Machanic, Lara Rubbelke, and Hugo Kornelis | ||
|
||
Permission is hereby granted, free of charge, to anyone obtaining a copy | ||
of this software and associated documentation files (the "Software"), | ||
to work with the Software within the limits of freeware distribution and fair use. | ||
This includes the rights to use, copy, and modify the Software for personal use. | ||
Users are also allowed and encouraged to submit corrections and modifications | ||
to the Software for the benefit of other users. | ||
|
||
It is not allowed to reuse, modify, or redistribute the Software for | ||
commercial use in any way, or for a user�s educational materials such as books | ||
or blog articles without prior permission from the copyright holder. | ||
|
||
The above copyright notice and this permission notice need to be included | ||
in all copies or substantial portions of the software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#Apress Source Code | ||
|
||
This repository accompanies [*Expert SQL Server 2005 Development*](http://www.apress.com/9781590597293) by Adam Machanic, Lara Rubbelke, and Hugo Kornelis (Apress, 2007). | ||
|
||
![Cover image](9781590597293.jpg) | ||
|
||
Download the files as a zip using the green button, or clone the repository to your machine using Git. | ||
|
||
##Releases | ||
|
||
Release v1.0 corresponds to the code in the published book, without corrections or updates. | ||
|
||
##Contributions | ||
|
||
See the file Contributing.md for more information on how you can contribute to this repository. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Source Code for Expert SQL Server 2005 Development | ||
-------------------------------------------------- | ||
|
||
Check http://www.expertsqlserver2005.com for updates and errata. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--Subtype modeling: Not so good | ||
CREATE TABLE Products | ||
( | ||
UPC INT NOT NULL PRIMARY KEY, | ||
Weight DECIMAL NOT NULL, | ||
Price DECIMAL NOT NULL | ||
) | ||
|
||
CREATE TABLE Books | ||
( | ||
UPC INT NOT NULL PRIMARY KEY | ||
REFERENCES Products (UPC), | ||
PageCount INT NOT NULL | ||
) | ||
|
||
CREATE TABLE DVDs | ||
( | ||
UPC INT NOT NULL PRIMARY KEY | ||
REFERENCES Products (UPC), | ||
LengthInMinutes DECIMAL NOT NULL, | ||
Format VARCHAR(4) NOT NULL | ||
CHECK (Format IN ('NTSC', 'PAL')) | ||
) | ||
GO | ||
|
||
|
||
|
||
--Better | ||
CREATE TABLE Products | ||
( | ||
UPC INT NOT NULL PRIMARY KEY, | ||
Weight DECIMAL NOT NULL, | ||
Price DECIMAL NOT NULL, | ||
ProductType CHAR(1) NOT NULL | ||
CHECK (ProductType IN ('B', 'D')), | ||
UNIQUE (UPC, ProductType) | ||
) | ||
|
||
CREATE TABLE Books | ||
( | ||
UPC INT NOT NULL PRIMARY KEY, | ||
ProductType CHAR(1) NOT NULL | ||
CHECK (ProductType = 'B'), | ||
PageCount INT NOT NULL, | ||
FOREIGN KEY (UPC, ProductType) REFERENCES Products (UPC, ProductType) | ||
) | ||
|
||
CREATE TABLE DVDs | ||
( | ||
UPC INT NOT NULL PRIMARY KEY, | ||
ProductType CHAR(1) NOT NULL | ||
CHECK (ProductType = 'D'), | ||
LengthInMinutes DECIMAL NOT NULL, | ||
Format VARCHAR(4) NOT NULL | ||
CHECK (Format IN ('NTSC', 'PAL')), | ||
FOREIGN KEY (UPC, ProductType) REFERENCES Products (UPC, ProductType) | ||
) | ||
GO | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
--Implied contract? | ||
CREATE PROCEDURE GetAggregateTransactionHistory | ||
@CustomerId INT | ||
AS | ||
BEGIN | ||
SET NOCOUNT ON | ||
|
||
SELECT | ||
SUM | ||
( | ||
CASE TransactionType | ||
WHEN 'Deposit' THEN Amount | ||
ELSE 0 | ||
END | ||
) AS TotalDeposits, | ||
SUM | ||
( | ||
CASE TransactionType | ||
WHEN 'Withdrawal' THEN Amount | ||
ELSE 0 | ||
END | ||
) AS TotalWithdrawals | ||
FROM TransactionHistory | ||
WHERE | ||
CustomerId = @CustomerId | ||
END | ||
GO | ||
|
||
|
||
|
||
--A slightly different implied contract | ||
CREATE PROCEDURE GetAggregateTransactionHistory | ||
@CustomerId INT | ||
AS | ||
BEGIN | ||
SET NOCOUNT ON | ||
|
||
SELECT | ||
SUM | ||
( | ||
CASE TH.TransactionType | ||
WHEN 'Deposit' THEN TH.Amount | ||
ELSE 0 | ||
END | ||
) AS TotalDeposits, | ||
SUM | ||
( | ||
CASE TH.TransactionType | ||
WHEN 'Withdrawal' THEN TH.Amount | ||
ELSE 0 | ||
END | ||
) AS TotalWithdrawals | ||
FROM Customers AS C | ||
LEFT JOIN TransactionHistory AS TH ON C.CustomerId = TH.CustomerId | ||
WHERE | ||
C.CustomerId = @CustomerId | ||
END | ||
GO | ||
|
||
|
||
|
||
/* | ||
//C# NUnit test for these stored procedures | ||
[TestMethod] | ||
public void TestAggregateTransactionHistory() | ||
{ | ||
//Set up a command object | ||
SqlCommand comm = new SqlCommand(); | ||
//Set up the connection | ||
comm.Connection = new SqlConnection( | ||
@"server=serverName; trusted_connection=true;"); | ||
//Define the procedure call | ||
comm.CommandText = "GetAggregateTransactionHistory"; | ||
comm.CommandType = CommandType.StoredProcedure; | ||
comm.Parameters.AddWithValue("@CustomerId", 123); | ||
//Create a DataSet for the results | ||
DataSet ds = new DataSet(); | ||
//Define a DataAdapter to fill a DataSet | ||
SqlDataAdapter adapter = new SqlDataAdapter(); | ||
adapter.SelectCommand = comm; | ||
try | ||
{ | ||
//Fill the dataset | ||
adapter.Fill(ds); | ||
} | ||
catch | ||
{ | ||
Assert.Fail("Exception occurred!"); | ||
} | ||
//Now we have the results -- validate them... | ||
//There must be exactly one returned result set | ||
Assert.IsTrue( | ||
ds.Tables.Count == 1, | ||
"Result set count != 1"); | ||
DataTable dt = ds.Tables[0]; | ||
//There must be exactly two columns returned | ||
Assert.IsTrue( | ||
dt.Columns.Count == 2, | ||
"Column count != 2"); | ||
//There must be columns called TotalDeposits and TotalWithdrawals | ||
Assert.IsTrue( | ||
dt.Columns.IndexOf("TotalDeposits") > -1, | ||
"Column TotalDeposits does not exist"); | ||
Assert.IsTrue( | ||
dt.Columns.IndexOf("TotalWithdrawals") > -1, | ||
"Column TotalWithdrawals does not exist"); | ||
//Both columns must be decimal | ||
Assert.IsTrue( | ||
dt.Columns["TotalDeposits"].DataType == typeof(decimal), | ||
"TotalDeposits data type is incorrect"); | ||
Assert.IsTrue( | ||
dt.Columns["TotalWithdrawals"].DataType == typeof(decimal), | ||
"TotalWithdrawals data type is incorrect"); | ||
//There must be zero or one rows returned | ||
Assert.IsTrue( | ||
dt.Rows.Count <= 1, | ||
"Too many rows returned"); | ||
} | ||
*/ | ||
|
||
|
Oops, something went wrong.