diff --git a/1-59059-720-6/Chapter01/readme.txt b/1-59059-720-6/Chapter01/readme.txt new file mode 100644 index 0000000..d90b020 --- /dev/null +++ b/1-59059-720-6/Chapter01/readme.txt @@ -0,0 +1,6 @@ +This chapter installs SQL Server and creates additional logon users + +There is no code within this chapter + +Robin Dewson +www.fat-belly.com \ No newline at end of file diff --git a/1-59059-720-6/Chapter02/ReadMe.txt b/1-59059-720-6/Chapter02/ReadMe.txt new file mode 100644 index 0000000..b179d7b --- /dev/null +++ b/1-59059-720-6/Chapter02/ReadMe.txt @@ -0,0 +1 @@ +No code \ No newline at end of file diff --git a/1-59059-720-6/Chapter03/CREATE DATABASE.sql b/1-59059-720-6/Chapter03/CREATE DATABASE.sql new file mode 100644 index 0000000..69bf4ac --- /dev/null +++ b/1-59059-720-6/Chapter03/CREATE DATABASE.sql @@ -0,0 +1,10 @@ +CREATE DATABASE ApressFinancial ON PRIMARY +( NAME = N'ApressFinancial', +FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\ApressFinancial.mdf' , SIZE = 3072KB , +MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) +LOG ON +( NAME = 'ApressFinancial_log', +FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\ApressFinancial_log.ldf' , +SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) +COLLATE SQL_Latin1_General_CP1_CI_AS +GO \ No newline at end of file diff --git a/1-59059-720-6/Chapter04/1-CREATE AJMASON.sql b/1-59059-720-6/Chapter04/1-CREATE AJMASON.sql new file mode 100644 index 0000000..ee58dd3 --- /dev/null +++ b/1-59059-720-6/Chapter04/1-CREATE AJMASON.sql @@ -0,0 +1,5 @@ +USE [master] +GO +CREATE LOGIN [XP-HOME\AJMason] +FROM WINDOWS +GO \ No newline at end of file diff --git a/1-59059-720-6/Chapter04/2-ALTER LOGIN.sql b/1-59059-720-6/Chapter04/2-ALTER LOGIN.sql new file mode 100644 index 0000000..584ad13 --- /dev/null +++ b/1-59059-720-6/Chapter04/2-ALTER LOGIN.sql @@ -0,0 +1,3 @@ +ALTER LOGIN [XP-HOME\AJMason] +WITH DEFAULT_DATABASE=ApressFinancial +GO \ No newline at end of file diff --git a/1-59059-720-6/Chapter04/3-CREATE USER.sql b/1-59059-720-6/Chapter04/3-CREATE USER.sql new file mode 100644 index 0000000..38e4a11 --- /dev/null +++ b/1-59059-720-6/Chapter04/3-CREATE USER.sql @@ -0,0 +1,2 @@ +CREATE USER [XP-HOME\AJMason] +FOR LOGIN [XP-HOME\AJMason] \ No newline at end of file diff --git a/1-59059-720-6/Chapter04/4-CREATE SCHEMA Transactions.sql b/1-59059-720-6/Chapter04/4-CREATE SCHEMA Transactions.sql new file mode 100644 index 0000000..6320e3a --- /dev/null +++ b/1-59059-720-6/Chapter04/4-CREATE SCHEMA Transactions.sql @@ -0,0 +1,3 @@ +USE ApressFinancial +GO +CREATE SCHEMA TransactionDetails AUTHORIZATION dbo \ No newline at end of file diff --git a/1-59059-720-6/Chapter05/1.CustomerDetails.Customers.sql b/1-59059-720-6/Chapter05/1.CustomerDetails.Customers.sql new file mode 100644 index 0000000..50b0ebd Binary files /dev/null and b/1-59059-720-6/Chapter05/1.CustomerDetails.Customers.sql differ diff --git a/1-59059-720-6/Chapter05/2-TransactionDetails.sql b/1-59059-720-6/Chapter05/2-TransactionDetails.sql new file mode 100644 index 0000000..1498c8c --- /dev/null +++ b/1-59059-720-6/Chapter05/2-TransactionDetails.sql @@ -0,0 +1,10 @@ +CREATE TABLE TransactionDetails.Transactions +(TransactionId bigint IDENTITY(1,1) NOT NULL, +CustomerId bigint NOT NULL, +TransactionType int NOT NULL, +DateEntered datetime NOT NULL, +Amount numeric(18, 5) NOT NULL, +ReferenceDetails nvarchar(50) NULL, +Notes nvarchar(max) NULL, +RelatedShareId bigint NULL, +RelatedProductId bigint NOT NULL) \ No newline at end of file diff --git a/1-59059-720-6/Chapter05/3-TransactionTypes.sql b/1-59059-720-6/Chapter05/3-TransactionTypes.sql new file mode 100644 index 0000000..46f51f9 --- /dev/null +++ b/1-59059-720-6/Chapter05/3-TransactionTypes.sql @@ -0,0 +1,14 @@ +-- ========================================= +-- Create table template +-- ========================================= +USE ApressFinancial +GO +IF OBJECT_ID('TransactionDetails.TransactionTypes', 'U') IS NOT NULL +DROP TABLE TransactionDetails.TransactionTypes +GO +CREATE TABLE TransactionDetails.TransactionTypes( +TransactionTypeId int IDENTITY(1,1) NOT NULL, +TransactionDescription nvarchar(30) NOT NULL, +CreditType bit NOT NULL +) +GO \ No newline at end of file diff --git a/1-59059-720-6/Chapter05/4-AlterTable.sql b/1-59059-720-6/Chapter05/4-AlterTable.sql new file mode 100644 index 0000000..869e249 --- /dev/null +++ b/1-59059-720-6/Chapter05/4-AlterTable.sql @@ -0,0 +1,3 @@ +ALTER TABLE TransactionDetails.TransactionTypes +ADD AffectCashBalance bit NULL +GO diff --git a/1-59059-720-6/Chapter05/5-RemainingTables.sql b/1-59059-720-6/Chapter05/5-RemainingTables.sql new file mode 100644 index 0000000..0efc86d --- /dev/null +++ b/1-59059-720-6/Chapter05/5-RemainingTables.sql @@ -0,0 +1,33 @@ +USE ApressFinancial +GO +CREATE TABLE CustomerDetails.CustomerProducts( +CustomerFinancialProductId bigint NOT NULL, +CustomerId bigint NOT NULL, +FinancialProductId bigint NOT NULL, +AmountToCollect money NOT NULL, +Frequency smallint NOT NULL, +LastCollected datetime NOT NULL, +LastCollection datetime NOT NULL, +Renewable bit NOT NULL +) +ON [PRIMARY] +GO +CREATE TABLE CustomerDetails.FinancialProducts( +ProductId bigint NOT NULL, +ProductName nvarchar(50) NOT NULL +) ON [PRIMARY] +GO +CREATE TABLE ShareDetails.SharePrices( +SharePriceId bigint IDENTITY(1,1) NOT NULL, +ShareId bigint NOT NULL, +Price numeric(18, 5) NOT NULL, +PriceDate datetime NOT NULL +) ON [PRIMARY] +GO +CREATE TABLE ShareDetails.Shares( +ShareId bigint IDENTITY(1,1) NOT NULL, +ShareDesc nvarchar(50) NOT NULL, +ShareTickerId nvarchar(50) NULL, +CurrentPrice numeric(18, 5) NOT NULL +) ON [PRIMARY] +GO \ No newline at end of file diff --git a/1-59059-720-6/Chapter05/6-PrimaryKey.sql b/1-59059-720-6/Chapter05/6-PrimaryKey.sql new file mode 100644 index 0000000..9b0f93d --- /dev/null +++ b/1-59059-720-6/Chapter05/6-PrimaryKey.sql @@ -0,0 +1,10 @@ +ALTER TABLE CustomerDetails.Customers +ADD CONSTRAINT +PK_Customers PRIMARY KEY NONCLUSTERED +( +CustomerId +) +WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, +ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) +ON [PRIMARY] +GO \ No newline at end of file diff --git a/1-59059-720-6/Chapter05/7-foreignKey.sql b/1-59059-720-6/Chapter05/7-foreignKey.sql new file mode 100644 index 0000000..0c6fe5f --- /dev/null +++ b/1-59059-720-6/Chapter05/7-foreignKey.sql @@ -0,0 +1,7 @@ +USE ApressFinancial +GO +ALTER TABLE TransactionDetails.Transactions +WITH NOCHECK +ADD CONSTRAINT FK_Transactions_Shares +FOREIGN KEY(RelatedShareId) +REFERENCES ShareDetails.Shares(ShareId) \ No newline at end of file diff --git a/1-59059-720-6/Chapter06/1-CustomerIndex.sql b/1-59059-720-6/Chapter06/1-CustomerIndex.sql new file mode 100644 index 0000000..7d7666e --- /dev/null +++ b/1-59059-720-6/Chapter06/1-CustomerIndex.sql @@ -0,0 +1,8 @@ +USE ApressFinancial +GO +CREATE INDEX IX_CustomerProducts +ON CustomerDetails.CustomerProducts +( +CustomerId +) +GO \ No newline at end of file diff --git a/1-59059-720-6/Chapter06/2-OtherIndexes.sql b/1-59059-720-6/Chapter06/2-OtherIndexes.sql new file mode 100644 index 0000000..0654381 --- /dev/null +++ b/1-59059-720-6/Chapter06/2-OtherIndexes.sql @@ -0,0 +1,30 @@ +USE ApressFinancial +GO +CREATE UNIQUE CLUSTERED INDEX IX_TransactionTypes +ON TransactionDetails.TransactionTypes +( +TransactionTypeId ASC ) +WITH (STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, +DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, +ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = OFF) +ON [PRIMARY] +GO +CREATE NONCLUSTERED INDEX IX_Transactions_TType +ON TransactionDetails.Transactions +( +TransactionType ASC) +WITH (STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, +DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, +ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = OFF) +ON [PRIMARY] +GO +ALTER TABLE TransactionDetails.TransactionTypes +ADD CONSTRAINT +PK_TransactionTypes PRIMARY KEY NONCLUSTERED +( +TransactionTypeId +) +WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, +ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) +ON [PRIMARY] +GO diff --git a/1-59059-720-6/Chapter06/3.Drop-Index.sql b/1-59059-720-6/Chapter06/3.Drop-Index.sql new file mode 100644 index 0000000..89228f9 --- /dev/null +++ b/1-59059-720-6/Chapter06/3.Drop-Index.sql @@ -0,0 +1,13 @@ +USE ApressFinancial +GO +DROP INDEX IX_TransactionTypes ON TransactionDetails.TransactionTypes +GO +CREATE UNIQUE CLUSTERED INDEX IX_TransactionTypes +ON TransactionDetails.TransactionTypes +( + TransactionTypeId ASC +) WITH (STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, +DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, +ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = OFF) +ON [PRIMARY] +GO diff --git a/1-59059-720-6/Chapter06/4.ShareDetails.SharePrices-Index.sql b/1-59059-720-6/Chapter06/4.ShareDetails.SharePrices-Index.sql new file mode 100644 index 0000000..5eb35b1 --- /dev/null +++ b/1-59059-720-6/Chapter06/4.ShareDetails.SharePrices-Index.sql @@ -0,0 +1,12 @@ +USE ApressFinancial +GO +CREATE UNIQUE CLUSTERED INDEX IX_SharePrices +ON ShareDetails.SharePrices +( + ShareId ASC, + PriceDate DESC, + Price +) WITH (STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, +DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, +ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = +OFF, DROP_EXISTING = OFF) ON [PRIMARY] diff --git a/1-59059-720-6/Chapter07/1.SetDBOffline.sql b/1-59059-720-6/Chapter07/1.SetDBOffline.sql new file mode 100644 index 0000000..f817318 --- /dev/null +++ b/1-59059-720-6/Chapter07/1.SetDBOffline.sql @@ -0,0 +1,4 @@ +USE master +GO +ALTER DATABASE ApressFinancial +SET OFFLINE diff --git a/1-59059-720-6/Chapter07/10.RestoreLogT-SQL.sql b/1-59059-720-6/Chapter07/10.RestoreLogT-SQL.sql new file mode 100644 index 0000000..b73d655 --- /dev/null +++ b/1-59059-720-6/Chapter07/10.RestoreLogT-SQL.sql @@ -0,0 +1,4 @@ +-- Dont forget the FILE = 5 may be different +RESTORE LOG [ApressFinancial] +FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\ApressFinancial.bak' WITH FILE = 5, +NOUNLOAD, STATS = 10 diff --git a/1-59059-720-6/Chapter07/11.DetachTSQL.sql b/1-59059-720-6/Chapter07/11.DetachTSQL.sql new file mode 100644 index 0000000..b505eb0 Binary files /dev/null and b/1-59059-720-6/Chapter07/11.DetachTSQL.sql differ diff --git a/1-59059-720-6/Chapter07/12.AttachTSQL.sql b/1-59059-720-6/Chapter07/12.AttachTSQL.sql new file mode 100644 index 0000000..b79f7a0 Binary files /dev/null and b/1-59059-720-6/Chapter07/12.AttachTSQL.sql differ diff --git a/1-59059-720-6/Chapter07/2.SetDBOnline.sql b/1-59059-720-6/Chapter07/2.SetDBOnline.sql new file mode 100644 index 0000000..1b0cf8b --- /dev/null +++ b/1-59059-720-6/Chapter07/2.SetDBOnline.sql @@ -0,0 +1,4 @@ +USE master +go +ALTER DATABASE ApressFinancial +SET ONLINE diff --git a/1-59059-720-6/Chapter07/3.FullBackup.sql b/1-59059-720-6/Chapter07/3.FullBackup.sql new file mode 100644 index 0000000..a9765cc --- /dev/null +++ b/1-59059-720-6/Chapter07/3.FullBackup.sql @@ -0,0 +1,6 @@ +BACKUP DATABASE ApressFinancial +TO DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\ApressFinancial.bak' +WITH NAME = 'ApressFinancial-Full Database Backup', +SKIP, +NOUNLOAD, +STATS = 10 diff --git a/1-59059-720-6/Chapter07/4.DifferentialBackup.sql b/1-59059-720-6/Chapter07/4.DifferentialBackup.sql new file mode 100644 index 0000000..d1d8cb0 --- /dev/null +++ b/1-59059-720-6/Chapter07/4.DifferentialBackup.sql @@ -0,0 +1,10 @@ +BACKUP DATABASE ApressFinancial +TO DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\ApressFinancial.bak' +WITH DIFFERENTIAL , +DESCRIPTION = 'This is a differential backup', +RETAINDAYS = 60, +NAME = 'ApressFinancial-Differential Database Backup', +STATS = 10, +CHECKSUM, +CONTINUE_AFTER_ERROR +GO diff --git a/1-59059-720-6/Chapter07/5.VerifyBackup.sql b/1-59059-720-6/Chapter07/5.VerifyBackup.sql new file mode 100644 index 0000000..909da90 --- /dev/null +++ b/1-59059-720-6/Chapter07/5.VerifyBackup.sql @@ -0,0 +1,17 @@ +DECLARE @BackupSet AS INT +SELECT @BackupSet = position + FROM msdb..backupset + WHERE database_name='ApressFinancial' + AND backup_set_id= + (SELECT MAX(backup_set_id) + FROM msdb..backupset + WHERE database_name='ApressFinancial' ) +IF @BackupSet IS NULL +BEGIN + RAISERROR('Verify failed. Backup information for database''ApressFinancial'' not found.', 16, 1) +END +RESTORE VERIFYONLY +FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\ApressFinancial.bak' +WITH FILE = @BackupSet, +NOUNLOAD, +NOREWIND diff --git a/1-59059-720-6/Chapter07/6.BackupTransactionLog.sql b/1-59059-720-6/Chapter07/6.BackupTransactionLog.sql new file mode 100644 index 0000000..55c0fee --- /dev/null +++ b/1-59059-720-6/Chapter07/6.BackupTransactionLog.sql @@ -0,0 +1,6 @@ +BACKUP LOG ApressFinancial +TO DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\ApressFinancial.bak' +WITH NAME = 'ApressFinancial-Transaction Log Backup', +SKIP, +NOUNLOAD, +STATS = 10 diff --git a/1-59059-720-6/Chapter07/7.SetRecoveryMode.sql b/1-59059-720-6/Chapter07/7.SetRecoveryMode.sql new file mode 100644 index 0000000..ef2728d --- /dev/null +++ b/1-59059-720-6/Chapter07/7.SetRecoveryMode.sql @@ -0,0 +1,15 @@ +ALTER DATABASE ApressFinancial +SET RECOVERY FULL +GO +BACKUP DATABASE ApressFinancial TO DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\ApressFinancial.bak' +WITH NAME = 'ApressFinancial-Full Database Backup' , +SKIP, +NOUNLOAD, +STATS = 10 +GO +BACKUP LOG ApressFinancial +TO DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\ApressFinancial.bak' +WITH NAME = 'ApressFinancial-Transaction Log Backup' , +SKIP, +NOUNLOAD, +STATS = 10 \ No newline at end of file diff --git a/1-59059-720-6/Chapter07/8.AlterTablePriorToTSQLRestore.sql b/1-59059-720-6/Chapter07/8.AlterTablePriorToTSQLRestore.sql new file mode 100644 index 0000000..ca07d14 --- /dev/null +++ b/1-59059-720-6/Chapter07/8.AlterTablePriorToTSQLRestore.sql @@ -0,0 +1,4 @@ +USE ApressFinancial +GO +ALTER TABLE ShareDetails.Shares +ADD DummyColumn varchar(30) diff --git a/1-59059-720-6/Chapter07/9.RestoreT-SQLPart1.sql b/1-59059-720-6/Chapter07/9.RestoreT-SQLPart1.sql new file mode 100644 index 0000000..d12607a --- /dev/null +++ b/1-59059-720-6/Chapter07/9.RestoreT-SQLPart1.sql @@ -0,0 +1,7 @@ +USE Master +GO +-- Dont forget the FILE = 3 may be different +RESTORE DATABASE [ApressFinancial] +FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\ApressFinancial.bak' WITH FILE = 3, +NORECOVERY, NOUNLOAD, REPLACE, STATS = 10 +GO diff --git a/1-59059-720-6/Chapter08/1.InsertShareDetails.Shares.sql b/1-59059-720-6/Chapter08/1.InsertShareDetails.Shares.sql new file mode 100644 index 0000000..b304e33 --- /dev/null +++ b/1-59059-720-6/Chapter08/1.InsertShareDetails.Shares.sql @@ -0,0 +1,10 @@ +SET QUOTED_IDENTIFIER OFF +GO +INSERT INTO [ApressFinancial].[ShareDetails].[Shares] + ([ShareDesc] + ,[ShareTickerId] + ,[CurrentPrice]) + VALUES + ("ACME'S HOMEBAKE COOKIES INC", + 'AHCI', + 2.34125) diff --git a/1-59059-720-6/Chapter08/10.SELECT 3 Columns.sql b/1-59059-720-6/Chapter08/10.SELECT 3 Columns.sql new file mode 100644 index 0000000..d428751 --- /dev/null +++ b/1-59059-720-6/Chapter08/10.SELECT 3 Columns.sql @@ -0,0 +1,2 @@ +SELECT CustomerFirstName,CustomerLastName,ClearedBalance +FROM CustomerDetails.Customers diff --git a/1-59059-720-6/Chapter08/11.SELECTAlias.sql b/1-59059-720-6/Chapter08/11.SELECTAlias.sql new file mode 100644 index 0000000..fe7ea10 --- /dev/null +++ b/1-59059-720-6/Chapter08/11.SELECTAlias.sql @@ -0,0 +1,4 @@ +SELECT CustomerFirstName As 'First Name', +CustomerLastName AS 'Surname', +ClearedBalance Balance +FROM CustomerDetails.Customers diff --git a/1-59059-720-6/Chapter08/12.InsertShareDetails.Shares.sql b/1-59059-720-6/Chapter08/12.InsertShareDetails.Shares.sql new file mode 100644 index 0000000..2a862f1 --- /dev/null +++ b/1-59059-720-6/Chapter08/12.InsertShareDetails.Shares.sql @@ -0,0 +1,12 @@ +INSERT INTO ShareDetails.Shares +(ShareDesc, ShareTickerId,CurrentPrice) +VALUES ('FAT-BELLY.COM','FBC',45.20) +INSERT INTO ShareDetails.Shares +(ShareDesc, ShareTickerId,CurrentPrice) +VALUES ('NetRadio Inc','NRI',29.79) +INSERT INTO ShareDetails.Shares +(ShareDesc, ShareTickerId,CurrentPrice) +VALUES ('Texas Oil Industries','TOI',0.455) +INSERT INTO ShareDetails.Shares +(ShareDesc, ShareTickerId,CurrentPrice) +VALUES ('London Bridge Club','LBC',1.46) diff --git a/1-59059-720-6/Chapter08/13.SELECTSpecificCompany.sql b/1-59059-720-6/Chapter08/13.SELECTSpecificCompany.sql new file mode 100644 index 0000000..5e81ea1 --- /dev/null +++ b/1-59059-720-6/Chapter08/13.SELECTSpecificCompany.sql @@ -0,0 +1,3 @@ +SELECT ShareDesc,CurrentPrice +FROM ShareDetails.Shares +WHERE ShareDesc = 'FAT-BELLY.COM' diff --git a/1-59059-720-6/Chapter08/14.SELECTRanger.sql b/1-59059-720-6/Chapter08/14.SELECTRanger.sql new file mode 100644 index 0000000..ff76fc5 --- /dev/null +++ b/1-59059-720-6/Chapter08/14.SELECTRanger.sql @@ -0,0 +1,7 @@ +SELECT ShareDesc,CurrentPrice +FROM ShareDetails.Shares +WHERE ShareDesc <> 'FAT-BELLY.COM' + +SELECT ShareDesc,CurrentPrice +FROM ShareDetails.Shares +WHERE NOT ShareDesc = 'FAT-BELLY.COM' diff --git a/1-59059-720-6/Chapter08/15.SELECTRowcount.sql b/1-59059-720-6/Chapter08/15.SELECTRowcount.sql new file mode 100644 index 0000000..75098e0 --- /dev/null +++ b/1-59059-720-6/Chapter08/15.SELECTRowcount.sql @@ -0,0 +1,4 @@ +SET ROWCOUNT 3 +SELECT * FROM ShareDetails.Shares +SET ROWCOUNT 0 +SELECT * FROM ShareDetails.Shares diff --git a/1-59059-720-6/Chapter08/16.SELECTTopVRowcount.sql b/1-59059-720-6/Chapter08/16.SELECTTopVRowcount.sql new file mode 100644 index 0000000..4c38d36 --- /dev/null +++ b/1-59059-720-6/Chapter08/16.SELECTTopVRowcount.sql @@ -0,0 +1,5 @@ +SELECT TOP 3 * FROM ShareDetails.Shares +SET ROWCOUNT 3 +SELECT TOP 2 * FROM ShareDetails.Shares +SET ROWCOUNT 2 +SELECT TOP 3 * FROM ShareDetails.Shares diff --git a/1-59059-720-6/Chapter08/17.SELECTConcatenation.sql b/1-59059-720-6/Chapter08/17.SELECTConcatenation.sql new file mode 100644 index 0000000..2dd8512 --- /dev/null +++ b/1-59059-720-6/Chapter08/17.SELECTConcatenation.sql @@ -0,0 +1,3 @@ +SELECT CustomerFirstName + ' ' + CustomerLastName AS 'Name', +ClearedBalance Balance +FROM CustomerDetails.Customers diff --git a/1-59059-720-6/Chapter08/18.SELECTConcatenationALIAS.sql b/1-59059-720-6/Chapter08/18.SELECTConcatenationALIAS.sql new file mode 100644 index 0000000..b825ada --- /dev/null +++ b/1-59059-720-6/Chapter08/18.SELECTConcatenationALIAS.sql @@ -0,0 +1,3 @@ +SELECT LEFT(CustomerFirstName + ' ' + CustomerLastName,50) AS 'Name', +ClearedBalance Balance +FROM CustomerDetails.Customers diff --git a/1-59059-720-6/Chapter08/19.SELECTWithOrder.sql b/1-59059-720-6/Chapter08/19.SELECTWithOrder.sql new file mode 100644 index 0000000..2bef6fc --- /dev/null +++ b/1-59059-720-6/Chapter08/19.SELECTWithOrder.sql @@ -0,0 +1,4 @@ +SELECT LEFT(CustomerFirstName + ' ' + CustomerLastName,50) AS 'Name', +ClearedBalance Balance +FROM CustomerDetails.Customers +ORDER BY Balance diff --git a/1-59059-720-6/Chapter08/2.FixupCustomerDetails.Customers.sql b/1-59059-720-6/Chapter08/2.FixupCustomerDetails.Customers.sql new file mode 100644 index 0000000..e32325b --- /dev/null +++ b/1-59059-720-6/Chapter08/2.FixupCustomerDetails.Customers.sql @@ -0,0 +1,12 @@ +DELETE FROM CustomerDetails.Customers +DBCC CHECKIDENT('CustomerDetails.Customers',RESEED,1) +INSERT INTO CustomerDetails.Customers +(CustomerTitleId,CustomerFirstName,CustomerOtherInitials, +CustomerLastName,AddressId,AccountNumber,AccountTypeId, +ClearedBalance,UnclearedBalance) +VALUES (1,'Vic',NULL,'McGlynn',111,87612311,1,4311.22,213.11) +INSERT INTO CustomerDetails.Customers +(CustomerTitleId,CustomerLastName,CustomerFirstName, +CustomerOtherInitials,AddressId,AccountNumber,AccountTypeId, +ClearedBalance,UnclearedBalance) +VALUES (3,'Mason','Jack',NULL,145,53431993,1,437.97,-10.56) diff --git a/1-59059-720-6/Chapter08/20.SELECTWithOrderDESC.sql b/1-59059-720-6/Chapter08/20.SELECTWithOrderDESC.sql new file mode 100644 index 0000000..9cbcf2f --- /dev/null +++ b/1-59059-720-6/Chapter08/20.SELECTWithOrderDESC.sql @@ -0,0 +1,4 @@ +SELECT LEFT(CustomerFirstName + ' ' + CustomerLastName,50) AS 'Name', +ClearedBalance Balance +FROM CustomerDetails.Customers +ORDER BY Balance DESC diff --git a/1-59059-720-6/Chapter08/21.SELECTWithLIKE.sql b/1-59059-720-6/Chapter08/21.SELECTWithLIKE.sql new file mode 100644 index 0000000..9015aaa --- /dev/null +++ b/1-59059-720-6/Chapter08/21.SELECTWithLIKE.sql @@ -0,0 +1,3 @@ +SELECT CustomerFirstName + ' ' + CustomerLastName +FROM CustomerDetails.Customers +WHERE CustomerLastName LIKE '%Glynn' diff --git a/1-59059-720-6/Chapter08/22.SELECTWithLIKE2.sql b/1-59059-720-6/Chapter08/22.SELECTWithLIKE2.sql new file mode 100644 index 0000000..44bbaa2 --- /dev/null +++ b/1-59059-720-6/Chapter08/22.SELECTWithLIKE2.sql @@ -0,0 +1,3 @@ +SELECT CustomerFirstName + ' ' + CustomerLastName AS [Name] +FROM CustomerDetails.Customers +WHERE CustomerFirstName + ' ' + CustomerLastName LIKE '%n%' diff --git a/1-59059-720-6/Chapter08/23.SELECT-ERROR.sql b/1-59059-720-6/Chapter08/23.SELECT-ERROR.sql new file mode 100644 index 0000000..fe708e9 --- /dev/null +++ b/1-59059-720-6/Chapter08/23.SELECT-ERROR.sql @@ -0,0 +1,3 @@ +SELECT CustomerFirstName + ' ' + CustomerLastName AS [Name] +FROM CustomerDetails.Customers +WHERE [Name] LIKE '%n%' diff --git a/1-59059-720-6/Chapter08/24.SELECTINTO.sql b/1-59059-720-6/Chapter08/24.SELECTINTO.sql new file mode 100644 index 0000000..4d2808b --- /dev/null +++ b/1-59059-720-6/Chapter08/24.SELECTINTO.sql @@ -0,0 +1,4 @@ +SELECT CustomerFirstName + ' ' + CustomerLastName AS [Name], +ClearedBalance,UnclearedBalance +INTO CustTemp +FROM CustomerDetails.Customers diff --git a/1-59059-720-6/Chapter08/25-UpdateToBrodie.sql b/1-59059-720-6/Chapter08/25-UpdateToBrodie.sql new file mode 100644 index 0000000..d2f6ccb --- /dev/null +++ b/1-59059-720-6/Chapter08/25-UpdateToBrodie.sql @@ -0,0 +1,3 @@ +UPDATE CustomerDetails.Customers +SET CustomerLastName = 'Brodie' +WHERE CustomerId = 1 diff --git a/1-59059-720-6/Chapter08/26-RetrieveBrodie.sql b/1-59059-720-6/Chapter08/26-RetrieveBrodie.sql new file mode 100644 index 0000000..3d0a8e8 --- /dev/null +++ b/1-59059-720-6/Chapter08/26-RetrieveBrodie.sql @@ -0,0 +1,2 @@ +SELECT * FROM CustomerDetails.Customers +WHERE CustomerId = 1 diff --git a/1-59059-720-6/Chapter08/27-UPDATEbackToMcGlynn.sql b/1-59059-720-6/Chapter08/27-UPDATEbackToMcGlynn.sql new file mode 100644 index 0000000..472cb0f --- /dev/null +++ b/1-59059-720-6/Chapter08/27-UPDATEbackToMcGlynn.sql @@ -0,0 +1,8 @@ +DECLARE @ValueToUpdate VARCHAR(30) +SET @ValueToUpdate = 'McGlynn' +UPDATE CustomerDetails.Customers + SET CustomerLastName = @ValueToUpdate, + ClearedBalance = ClearedBalance + UnclearedBalance , + UnclearedBalance = 0 + WHERE CustomerLastName = 'Brodie' + diff --git a/1-59059-720-6/Chapter08/28-SELECTMcGlynnAgain.sql b/1-59059-720-6/Chapter08/28-SELECTMcGlynnAgain.sql new file mode 100644 index 0000000..9777ee5 --- /dev/null +++ b/1-59059-720-6/Chapter08/28-SELECTMcGlynnAgain.sql @@ -0,0 +1,4 @@ +SELECT CustomerFirstName, CustomerLastName, +ClearedBalance, UnclearedBalance +FROM CustomerDetails.Customers +WHERE CustomerId = 1 diff --git a/1-59059-720-6/Chapter08/29-UpdateClearedBalance.sql b/1-59059-720-6/Chapter08/29-UpdateClearedBalance.sql new file mode 100644 index 0000000..161f6c7 --- /dev/null +++ b/1-59059-720-6/Chapter08/29-UpdateClearedBalance.sql @@ -0,0 +1,5 @@ +DECLARE @WrongDataType VARCHAR(20) +SET @WrongDataType = '4311.22' +UPDATE CustomerDetails.Customers + SET ClearedBalance = @WrongDataType +WHERE CustomerId = 1 diff --git a/1-59059-720-6/Chapter08/3.ConstraintCustomerDetails.CustomerProducts.sql b/1-59059-720-6/Chapter08/3.ConstraintCustomerDetails.CustomerProducts.sql new file mode 100644 index 0000000..063b640 --- /dev/null +++ b/1-59059-720-6/Chapter08/3.ConstraintCustomerDetails.CustomerProducts.sql @@ -0,0 +1,7 @@ +USE ApressFinancial +GO +ALTER TABLE CustomerDetails.CustomerProducts +ADD CONSTRAINT PK_CustomerProducts +PRIMARY KEY CLUSTERED +(CustomerFinancialProductId) ON [PRIMARY] +GO diff --git a/1-59059-720-6/Chapter08/30-SelectCustomer.sql b/1-59059-720-6/Chapter08/30-SelectCustomer.sql new file mode 100644 index 0000000..0b01a55 --- /dev/null +++ b/1-59059-720-6/Chapter08/30-SelectCustomer.sql @@ -0,0 +1,5 @@ +SELECT CustomerFirstName, CustomerLastName, +ClearedBalance, UnclearedBalance +FROM CustomerDetails.Customers +WHERE CustomerId = 1 + diff --git a/1-59059-720-6/Chapter08/31-DataTypeError.sql b/1-59059-720-6/Chapter08/31-DataTypeError.sql new file mode 100644 index 0000000..be07c18 --- /dev/null +++ b/1-59059-720-6/Chapter08/31-DataTypeError.sql @@ -0,0 +1,7 @@ +DECLARE @WrongDataType VARCHAR(20) +SET @WrongDataType = '2.0' +UPDATE CustomerDetails.Customers + SET AddressId = @WrongDataType +WHERE CustomerId = 1 + + diff --git a/1-59059-720-6/Chapter08/32-UPDATESharesInTran.sql b/1-59059-720-6/Chapter08/32-UPDATESharesInTran.sql new file mode 100644 index 0000000..1c216a4 --- /dev/null +++ b/1-59059-720-6/Chapter08/32-UPDATESharesInTran.sql @@ -0,0 +1,14 @@ +SELECT 'Before',ShareId,ShareDesc,CurrentPrice + FROM ShareDetails.Shares + WHERE ShareId = 3 +BEGIN TRAN ShareUpd +UPDATE ShareDetails.Shares + SET CurrentPrice = CurrentPrice * 1.1 + WHERE ShareId = 3 +COMMIT TRAN +SELECT 'After',ShareId,ShareDesc,CurrentPrice + FROM ShareDetails.Shares + WHERE ShareId = 3 + + + diff --git a/1-59059-720-6/Chapter08/33-UPDATERollbackTran.sql b/1-59059-720-6/Chapter08/33-UPDATERollbackTran.sql new file mode 100644 index 0000000..83b9f49 --- /dev/null +++ b/1-59059-720-6/Chapter08/33-UPDATERollbackTran.sql @@ -0,0 +1,17 @@ +SELECT 'Before',ShareId,ShareDesc,CurrentPrice + FROM ShareDetails.Shares +-- WHERE ShareId = 3 +BEGIN TRAN ShareUpd +UPDATE ShareDetails.Shares + SET CurrentPrice = CurrentPrice * 1.1 +-- WHERE ShareId = 3 +SELECT 'Within the transaction',ShareId,ShareDesc,CurrentPrice + FROM ShareDetails.Shares +ROLLBACK TRAN +SELECT 'After',ShareId,ShareDesc,CurrentPrice + FROM ShareDetails.Shares +-- WHERE ShareId = 3 + + + + diff --git a/1-59059-720-6/Chapter08/34 - NestedTransTrancount.sql b/1-59059-720-6/Chapter08/34 - NestedTransTrancount.sql new file mode 100644 index 0000000..37d3dec --- /dev/null +++ b/1-59059-720-6/Chapter08/34 - NestedTransTrancount.sql @@ -0,0 +1,13 @@ +BEGIN TRAN ShareUpd + SELECT '1st TranCount',@@TRANCOUNT + BEGIN TRAN ShareUpd2 + SELECT '2nd TranCount',@@TRANCOUNT + COMMIT TRAN ShareUpd2 + SELECT '3rd TranCount',@@TRANCOUNT +COMMIT TRAN -- It is at this point that data modifications will be committed +SELECT 'Last TranCount',@@TRANCOUNT + + + + + diff --git a/1-59059-720-6/Chapter08/35-RollbackDELETE.sql b/1-59059-720-6/Chapter08/35-RollbackDELETE.sql new file mode 100644 index 0000000..dcf4c3f --- /dev/null +++ b/1-59059-720-6/Chapter08/35-RollbackDELETE.sql @@ -0,0 +1,13 @@ +BEGIN TRAN + SELECT * FROM CustTemp + DELETE CustTemp + SELECT * FROM CustTemp +ROLLBACK TRAN +SELECT * FROM CustTemp + + + + + + + diff --git a/1-59059-720-6/Chapter08/36-Delete3Rollback.sql b/1-59059-720-6/Chapter08/36-Delete3Rollback.sql new file mode 100644 index 0000000..486202b --- /dev/null +++ b/1-59059-720-6/Chapter08/36-Delete3Rollback.sql @@ -0,0 +1,6 @@ +BEGIN TRAN + SELECT * FROM CustTemp + DELETE TOP (3) CustTemp + SELECT * FROM CustTemp +ROLLBACK TRAN +SELECT * FROM CustTemp diff --git a/1-59059-720-6/Chapter08/37-TruncateCustomers.sql b/1-59059-720-6/Chapter08/37-TruncateCustomers.sql new file mode 100644 index 0000000..9551dc4 --- /dev/null +++ b/1-59059-720-6/Chapter08/37-TruncateCustomers.sql @@ -0,0 +1 @@ +TRUNCATE TABLE CustomerDetails.Customers \ No newline at end of file diff --git a/1-59059-720-6/Chapter08/4.CheckConstraint.CustomerDetails.CustomerProducts.sql b/1-59059-720-6/Chapter08/4.CheckConstraint.CustomerDetails.CustomerProducts.sql new file mode 100644 index 0000000..d8886ee --- /dev/null +++ b/1-59059-720-6/Chapter08/4.CheckConstraint.CustomerDetails.CustomerProducts.sql @@ -0,0 +1,5 @@ +ALTER TABLE CustomerDetails.CustomerProducts +WITH NOCHECK +ADD CONSTRAINT CK_CustProds_AmtCheck +CHECK ((AmountToCollect > 0)) +GO diff --git a/1-59059-720-6/Chapter08/5.DefaultConstraint.CustomerDetails.CustomerProducts.sql b/1-59059-720-6/Chapter08/5.DefaultConstraint.CustomerDetails.CustomerProducts.sql new file mode 100644 index 0000000..dc10a60 --- /dev/null +++ b/1-59059-720-6/Chapter08/5.DefaultConstraint.CustomerDetails.CustomerProducts.sql @@ -0,0 +1,4 @@ +ALTER TABLE CustomerDetails.CustomerProducts WITH NOCHECK + ADD CONSTRAINT DF_CustProd_Renewable + DEFAULT (0) + FOR Renewable diff --git a/1-59059-720-6/Chapter08/6.CHECKCustomerDetails.CustomerProducts.sql b/1-59059-720-6/Chapter08/6.CHECKCustomerDetails.CustomerProducts.sql new file mode 100644 index 0000000..ed4d39f --- /dev/null +++ b/1-59059-720-6/Chapter08/6.CHECKCustomerDetails.CustomerProducts.sql @@ -0,0 +1,4 @@ +ALTER TABLE CustomerDetails.CustomerProducts ADD CONSTRAINT + CK_CustProd_LastColl CHECK ((LastCollection>=LastCollected)) +GO + diff --git a/1-59059-720-6/Chapter08/7.MultipleINSERTs.sql b/1-59059-720-6/Chapter08/7.MultipleINSERTs.sql new file mode 100644 index 0000000..7ae3f44 --- /dev/null +++ b/1-59059-720-6/Chapter08/7.MultipleINSERTs.sql @@ -0,0 +1,17 @@ +INSERT INTO CustomerDetails.Customers +(CustomerTitleId,CustomerFirstName,CustomerOtherInitials, +CustomerLastName,AddressId,AccountNumber,AccountTypeId, +ClearedBalance,UnclearedBalance) +VALUES (3,'Bernie','I','McGee',314,65368765,1,6653.11,0.00) +GO +INSERT INTO CustomerDetails.Customers +(CustomerTitleId,CustomerFirstName,CustomerOtherInitials, +CustomerLastName,AddressId,AccountNumber,AccountTypeId, +ClearedBalance,UnclearedBalance) +VALUES (2,'Julie','A','Dewson',2134,81625422,1,53.32,-12.21) +GO +INSERT INTO CustomerDetails.Customers +(CustomerTitleId,CustomerFirstName,CustomerOtherInitials, +CustomerLastName,AddressId,AccountNumber,AccountTypeId, +ClearedBalance,UnclearedBalance) +VALUES (1,'Kirsty',NULL,'Hull',4312,96565334,1,1266.00,10.32) diff --git a/1-59059-720-6/Chapter08/8.SelectTOP3.sql b/1-59059-720-6/Chapter08/8.SelectTOP3.sql new file mode 100644 index 0000000..eab8dc1 --- /dev/null +++ b/1-59059-720-6/Chapter08/8.SelectTOP3.sql @@ -0,0 +1,2 @@ +SELECT TOP (3) * +FROM CustomerDetails.Customers diff --git a/1-59059-720-6/Chapter08/9.SELECTALL.sql b/1-59059-720-6/Chapter08/9.SELECTALL.sql new file mode 100644 index 0000000..6ae6064 --- /dev/null +++ b/1-59059-720-6/Chapter08/9.SELECTALL.sql @@ -0,0 +1 @@ +SELECT * FROM CustomerDetails.Customers \ No newline at end of file diff --git a/1-59059-720-6/Chapter08/readme.txt b/1-59059-720-6/Chapter08/readme.txt new file mode 100644 index 0000000..a6c5e64 --- /dev/null +++ b/1-59059-720-6/Chapter08/readme.txt @@ -0,0 +1,4 @@ +This large chapter looked at how to inserting, updating, selecting and deleting data + +Robin Dewson +www.fat-belly.com \ No newline at end of file diff --git a/1-59059-720-6/Chapter09/1.ShareDetails.Shares.view.sql b/1-59059-720-6/Chapter09/1.ShareDetails.Shares.view.sql new file mode 100644 index 0000000..5c107b5 Binary files /dev/null and b/1-59059-720-6/Chapter09/1.ShareDetails.Shares.view.sql differ diff --git a/1-59059-720-6/Chapter09/10.CREATE INDEX on view.sql b/1-59059-720-6/Chapter09/10.CREATE INDEX on view.sql new file mode 100644 index 0000000..94b616d --- /dev/null +++ b/1-59059-720-6/Chapter09/10.CREATE INDEX on view.sql @@ -0,0 +1,3 @@ +CREATE UNIQUE CLUSTERED INDEX ix_CustFinProds +ON CustomerDetails.vw_CustFinProducts (AccountNumber, ProductName) + diff --git a/1-59059-720-6/Chapter09/2.ShareDetails.vw_SharePrices.sql b/1-59059-720-6/Chapter09/2.ShareDetails.vw_SharePrices.sql new file mode 100644 index 0000000..efedc2b Binary files /dev/null and b/1-59059-720-6/Chapter09/2.ShareDetails.vw_SharePrices.sql differ diff --git a/1-59059-720-6/Chapter09/3-insert-shareprices.sql b/1-59059-720-6/Chapter09/3-insert-shareprices.sql new file mode 100644 index 0000000..854435d --- /dev/null +++ b/1-59059-720-6/Chapter09/3-insert-shareprices.sql @@ -0,0 +1,18 @@ +INSERT INTO ShareDetails.SharePrices (ShareId, Price, PriceDate) +VALUES (1,2.155,'1 Aug 2005 10:10AM') +INSERT INTO ShareDetails.SharePrices (ShareId, Price, PriceDate) +VALUES (1,2.2125,'1 Aug 2005 10:12AM') +INSERT INTO ShareDetails.SharePrices (ShareId, Price, PriceDate) +VALUES (1,2.4175,'1 Aug 2005 10:16AM') +INSERT INTO ShareDetails.SharePrices (ShareId, Price, PriceDate) +VALUES (1,2.21,'1 Aug 2005 11:22AM') +INSERT INTO ShareDetails.SharePrices (ShareId, Price, PriceDate) +VALUES (1,2.17,'1 Aug 2005 14:54') +INSERT INTO ShareDetails.SharePrices (ShareId, Price, PriceDate) +VALUES (1,2.34125,'1 Aug 2005 16:10') +INSERT INTO ShareDetails.SharePrices (ShareId, Price, PriceDate) +VALUES (2,41.10,'1 Aug 2005 10:10AM') +INSERT INTO ShareDetails.SharePrices (ShareId, Price, PriceDate) +VALUES (2,43.22,'2 Aug 2005 10:10AM') +INSERT INTO ShareDetails.SharePrices (ShareId, Price, PriceDate) +VALUES (2,45.20,'3 Aug 2005 10:10AM') \ No newline at end of file diff --git a/1-59059-720-6/Chapter09/4.CustTrans-TSQL-Test.sql b/1-59059-720-6/Chapter09/4.CustTrans-TSQL-Test.sql new file mode 100644 index 0000000..e031a5f Binary files /dev/null and b/1-59059-720-6/Chapter09/4.CustTrans-TSQL-Test.sql differ diff --git a/1-59059-720-6/Chapter09/5.CustomerDetails.vw_CustTrans.sql b/1-59059-720-6/Chapter09/5.CustomerDetails.vw_CustTrans.sql new file mode 100644 index 0000000..12e8d2c Binary files /dev/null and b/1-59059-720-6/Chapter09/5.CustomerDetails.vw_CustTrans.sql differ diff --git a/1-59059-720-6/Chapter09/6.Predata required for SCHEMA view.sql b/1-59059-720-6/Chapter09/6.Predata required for SCHEMA view.sql new file mode 100644 index 0000000..42a8f93 Binary files /dev/null and b/1-59059-720-6/Chapter09/6.Predata required for SCHEMA view.sql differ diff --git a/1-59059-720-6/Chapter09/7.SCHEMABINDING TestCode.sql b/1-59059-720-6/Chapter09/7.SCHEMABINDING TestCode.sql new file mode 100644 index 0000000..5b7ed7c --- /dev/null +++ b/1-59059-720-6/Chapter09/7.SCHEMABINDING TestCode.sql @@ -0,0 +1,6 @@ +SELECT c.CustomerFirstName + ' ' + c.CustomerLastName AS CustomerName, +c.AccountNumber, fp.ProductName, cp.AmountToCollect, cp.Frequency, +cp.LastCollected +FROM CustomerDetails.Customers c +JOIN CustomerDetails.CustomerProducts cp ON cp.CustomerId = c.CustomerId +JOIN CustomerDetails.FinancialProducts fp ON fp.ProductId = cp.FinancialProductId diff --git a/1-59059-720-6/Chapter09/8.SCHEMABINDING View vw_CustFinProducts.sql b/1-59059-720-6/Chapter09/8.SCHEMABINDING View vw_CustFinProducts.sql new file mode 100644 index 0000000..60eef76 --- /dev/null +++ b/1-59059-720-6/Chapter09/8.SCHEMABINDING View vw_CustFinProducts.sql @@ -0,0 +1,13 @@ +IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS + WHERE TABLE_NAME = N'vw_CustFinProducts' + AND TABLE_SCHEMA = N'CustomerDetails') + DROP VIEW CustomerDetails.vw_CustFinProducts +GO +CREATE VIEW CustomerDetails.vw_CustFinProducts WITH SCHEMABINDING +AS +SELECT c.CustomerFirstName + ' ' + c.CustomerLastName AS CustomerName, +c.AccountNumber, fp.ProductName, cp.AmountToCollect, cp.Frequency, +cp.LastCollected +FROM CustomerDetails.Customers c +JOIN CustomerDetails.CustomerProducts cp ON cp.CustomerId = c.CustomerId +JOIN CustomerDetails.FinancialProducts fp ON fp.ProductId = cp.FinancialProductId diff --git a/1-59059-720-6/Chapter09/9.DROPandRECREATE of view.sql b/1-59059-720-6/Chapter09/9.DROPandRECREATE of view.sql new file mode 100644 index 0000000..aa0cb05 Binary files /dev/null and b/1-59059-720-6/Chapter09/9.DROPandRECREATE of view.sql differ diff --git a/1-59059-720-6/Chapter09/readme.txt b/1-59059-720-6/Chapter09/readme.txt new file mode 100644 index 0000000..2de0408 --- /dev/null +++ b/1-59059-720-6/Chapter09/readme.txt @@ -0,0 +1,4 @@ +Here we deal with working with views + +Robin Dewson +www.fat-belly.com \ No newline at end of file diff --git a/1-59059-720-6/Chapter10/00-DemonstratingValueInOUTPUT.sql b/1-59059-720-6/Chapter10/00-DemonstratingValueInOUTPUT.sql new file mode 100644 index 0000000..e5ce019 --- /dev/null +++ b/1-59059-720-6/Chapter10/00-DemonstratingValueInOUTPUT.sql @@ -0,0 +1,23 @@ +use tempdb +go +create proc testparam(@a int,@b int output) +as +print 'Value of @b at start :' + cast(@b as varchar(10)) +select @b = @a + ISNULL(@b,0) +print 'Value of @b at end :' + cast(@b as varchar(10)) +return +go + +declare @a int ; set @a = 1 +declare @b int ; set @b = 5 +exec testparam @a,@b output +select @b + +/* +Value of @b at start :5 +Value of @b at end :6 + +----------- +6 + +*/ diff --git a/1-59059-720-6/Chapter10/1.FirstStoredProcedureCreation.sql b/1-59059-720-6/Chapter10/1.FirstStoredProcedureCreation.sql new file mode 100644 index 0000000..a95866d --- /dev/null +++ b/1-59059-720-6/Chapter10/1.FirstStoredProcedureCreation.sql @@ -0,0 +1,23 @@ +CREATE PROCEDURE CustomerDetails.apf_InsertCustomer + -- Add the parameters for the function here + @FirstName varchar(50) , + @LastName varchar(50), + @CustTitle int, + @CustInitials nvarchar(10), + @AddressId int, + @AccountNumber nvarchar(15), + @AccountTypeId int +AS +BEGIN + -- SET NOCOUNT ON added to prevent extra result sets from + -- interfering with SELECT statements. + SET NOCOUNT ON; + + INSERT INTO CustomerDetails.Customers + (CustomerTitleId,CustomerFirstName,CustomerOtherInitials,CustomerLastName, + AddressId,AccountNumber,AccountTypeId,ClearedBalance,UnclearedBalance) + VALUES (@CustTitle,@FirstName,@CustInitials,@LastName, + @AddressId,@AccountNumber,@AccountTypeId,0,0) + +END +GO diff --git a/1-59059-720-6/Chapter10/2.RunningFirstProcedure.sql b/1-59059-720-6/Chapter10/2.RunningFirstProcedure.sql new file mode 100644 index 0000000..aeb0d85 --- /dev/null +++ b/1-59059-720-6/Chapter10/2.RunningFirstProcedure.sql @@ -0,0 +1 @@ +CustomerDetails.apf_InsertCustomer 'Henry','Williams',1,NULL,431,'22067531',1 \ No newline at end of file diff --git a/1-59059-720-6/Chapter10/3.RunningSecondProcedure.sql b/1-59059-720-6/Chapter10/3.RunningSecondProcedure.sql new file mode 100644 index 0000000..053c537 --- /dev/null +++ b/1-59059-720-6/Chapter10/3.RunningSecondProcedure.sql @@ -0,0 +1,3 @@ +CustomerDetails.apf_InsertCustomer @CustTitle=1,@FirstName='Julie', +@CustInitials='A',@LastName='Dewson',@AddressId=6643, +@AccountNumber='SS865',@AccountTypeId=6 diff --git a/1-59059-720-6/Chapter10/4.SecondProcedureWithOUTPUT.sql b/1-59059-720-6/Chapter10/4.SecondProcedureWithOUTPUT.sql new file mode 100644 index 0000000..582e5cd --- /dev/null +++ b/1-59059-720-6/Chapter10/4.SecondProcedureWithOUTPUT.sql @@ -0,0 +1,37 @@ +-- =============================================== +-- Create stored procedure with OUTPUT parameters +-- =============================================== +-- Drop stored procedure if it already exists +IF EXISTS ( + SELECT * + FROM INFORMATION_SCHEMA.ROUTINES + WHERE SPECIFIC_SCHEMA = N'CustomerDetails' + AND SPECIFIC_NAME = N'apf_CustBalances' +) + DROP PROCEDURE CustomerDetails.apf_CustBalances +GO + +CREATE PROCEDURE CustomerDetails.apf_CustBalances + @CustId int, + @ClearedBalance money OUTPUT, + @UnclearedBalance money OUTPUT +AS +SELECT @ClearedBalance = ClearedBalance, @UnclearedBalance = UnclearedBalance + FROM Customers + WHERE CustomerId = @CustId + RETURN @@Error +GO + +-- ============================================= +-- Example to execute the stored procedure +-- ============================================= +DECLARE @ClearedBalance Money, @UnclearedBalance Money +DECLARE @RetVal int + +EXECUTE @RetVal=CustomerDetails.apf_CustBalances 1, @ClearedBalance OUTPUT, +@UnclearedBalance OUTPUT + +SELECT @RetVal AS ReturnValue, @ClearedBalance AS ClearedBalance, +@UnclearedBalance AS UnclearedBalance +GO + diff --git a/1-59059-720-6/Chapter10/5.BREAK.sql b/1-59059-720-6/Chapter10/5.BREAK.sql new file mode 100644 index 0000000..df4d2dc --- /dev/null +++ b/1-59059-720-6/Chapter10/5.BREAK.sql @@ -0,0 +1,14 @@ +DECLARE @LoopCount int, @TestCount int +SET @LoopCount = 0 +SET @TestCount = 0 +WHILE @LoopCount < 20 +BEGIN + SET @LoopCount = @LoopCount + 1 + SET @TestCount = @TestCount + 1 + SELECT @LoopCount, @TestCount + IF @TestCount > 10 + BREAK + ELSE + CONTINUE + SELECT @LoopCount, @TestCount +END diff --git a/1-59059-720-6/Chapter10/6.CASE-2-Example.sql b/1-59059-720-6/Chapter10/6.CASE-2-Example.sql new file mode 100644 index 0000000..c940aee --- /dev/null +++ b/1-59059-720-6/Chapter10/6.CASE-2-Example.sql @@ -0,0 +1,7 @@ +SELECT CustomerId, +CASE +WHEN ClearedBalance < 0 THEN 'OverDrawn' +WHEN ClearedBalance > 0 THEN ' In Credit' +ELSE 'Flat' +END, ClearedBalance +FROM CustomerDetails.Customers diff --git a/1-59059-720-6/Chapter10/6.CASEExample.sql b/1-59059-720-6/Chapter10/6.CASEExample.sql new file mode 100644 index 0000000..d7d5499 --- /dev/null +++ b/1-59059-720-6/Chapter10/6.CASEExample.sql @@ -0,0 +1,15 @@ +INSERT INTO TransactionDetails.TransactionTypes +(TransactionDescription,CreditType,AffectCashBalance) +VALUES ('Deposit',1,1) +INSERT INTO TransactionDetails.TransactionTypes +(TransactionDescription,CreditType,AffectCashBalance) +VALUES ('Withdrawal',0,1) +INSERT INTO TransactionDetails.TransactionTypes +(TransactionDescription,CreditType,AffectCashBalance) +VALUES ('BoughtShares',1,0) +SELECT TransactionDescription, +CASE CreditType +WHEN 0 THEN 'Debiting the account' +WHEN 1 THEN 'Crediting the account' +END +FROM TransactionDetails.TransactionTypes diff --git a/1-59059-720-6/Chapter10/7.FinalExampleAllTogether.sql b/1-59059-720-6/Chapter10/7.FinalExampleAllTogether.sql new file mode 100644 index 0000000..456cf7a --- /dev/null +++ b/1-59059-720-6/Chapter10/7.FinalExampleAllTogether.sql @@ -0,0 +1,44 @@ +CREATE PROCEDURE CustomerDetails.apf_CustMovement @CustId bigint, +@FromDate datetime, @ToDate datetime +AS +BEGIN +DECLARE @RunningBal money, @StillCalc Bit, @LastTran bigint + +SELECT @StillCalc = 1, @LastTran = 0, @RunningBal = 0 +WHILE @StillCalc = 1 +BEGIN + SELECT TOP 1 @RunningBal = @RunningBal + CASE + WHEN tt.CreditType = 1 THEN t.Amount + ELSE t.Amount * -1 END, + @LastTran = t.TransactionId + FROM CustomerDetails.Customers c + JOIN TransactionDetails.Transactions t ON t.CustomerId = c.CustomerId + JOIN TransactionDetails.TransactionTypes tt ON tt.TransactionTypeId = t.TransactionType + WHERE t.TransactionId > @LastTran + AND tt.AffectCashBalance = 1 + AND DateEntered BETWEEN @FromDate AND @ToDate + ORDER BY DateEntered + IF @@ROWCOUNT > 0 + -- Perform some interest calculation here… + CONTINUE + ELSE + BREAK +END + +SELECT @RunningBal AS 'End Balance' +END +GO +INSERT INTO TransactionDetails.Transactions +(CustomerId,TransactionType,DateEntered,Amount,RelatedProductId) +VALUES (1,1,'1 Aug 2005',100.00,1) +INSERT INTO TransactionDetails.Transactions +(CustomerId,TransactionType,DateEntered,Amount,RelatedProductId) +VALUES (1,1,'3 Aug 2005',75.67,1) +INSERT INTO TransactionDetails.Transactions +(CustomerId,TransactionType,DateEntered,Amount,RelatedProductId) +VALUES (1,2,'5 Aug 2005',35.20,1) +INSERT INTO TransactionDetails.Transactions +(CustomerId,TransactionType,DateEntered,Amount,RelatedProductId) +VALUES (1,2,'6 Aug 2005',20.00,1) +EXEC CustomerDetails.apf_CustMovement 1,'1 Aug 2005','31 Aug 2005' + diff --git a/1-59059-720-6/Chapter10/readme.txt b/1-59059-720-6/Chapter10/readme.txt new file mode 100644 index 0000000..9f3d4fa --- /dev/null +++ b/1-59059-720-6/Chapter10/readme.txt @@ -0,0 +1,4 @@ +Here we are dealing with stored procedures + +Robin Dewson +www.fat-belly.com \ No newline at end of file diff --git a/1-59059-720-6/Chapter11/1-JOIN.sql b/1-59059-720-6/Chapter11/1-JOIN.sql new file mode 100644 index 0000000..e0dd629 --- /dev/null +++ b/1-59059-720-6/Chapter11/1-JOIN.sql @@ -0,0 +1,3 @@ +SELECT s.ShareDesc,sp.Price,sp.PriceDate + FROM ShareDetails.Shares s + JOIN ShareDetails.SharePrices sp ON sp.ShareId = s.ShareId diff --git a/1-59059-720-6/Chapter11/10-Variables-2batches.sql b/1-59059-720-6/Chapter11/10-Variables-2batches.sql new file mode 100644 index 0000000..59c5447 --- /dev/null +++ b/1-59059-720-6/Chapter11/10-Variables-2batches.sql @@ -0,0 +1,7 @@ +DECLARE @MyDate datetime, @CurrPriceInCents money +SET @MyDate = GETDATE() +SELECT @CurrPriceInCents = CurrentPrice * 100 + FROM ShareDetails.Shares + WHERE ShareId = 2 +GO +SELECT @MyDate,@CurrPriceInCents diff --git a/1-59059-720-6/Chapter11/11-Variables-MultipleRows.sql b/1-59059-720-6/Chapter11/11-Variables-MultipleRows.sql new file mode 100644 index 0000000..82bbe2a --- /dev/null +++ b/1-59059-720-6/Chapter11/11-Variables-MultipleRows.sql @@ -0,0 +1,7 @@ +DECLARE @MyDate datetime, @CurrPriceInCents money +SET @MyDate = GETDATE() +SELECT @CurrPriceInCents = CurrentPrice * 100 + FROM ShareDetails.Shares +-- WHERE ShareId = 2 +--GO +SELECT @MyDate,@CurrPriceInCents diff --git a/1-59059-720-6/Chapter11/12-TempTable.sql b/1-59059-720-6/Chapter11/12-TempTable.sql new file mode 100644 index 0000000..c78de8e --- /dev/null +++ b/1-59059-720-6/Chapter11/12-TempTable.sql @@ -0,0 +1,9 @@ +CREATE TABLE #SharesTmp +(ShareDesc varchar(50), +Price numeric(18,5), +PriceDate datetime) +INSERT INTO #SharesTmp +SELECT s.ShareDesc,sp.Price,sp.PriceDate + FROM ShareDetails.Shares s + JOIN ShareDetails.SharePrices sp ON sp.ShareId = s.ShareId +SELECT * FROM #SharesTmp \ No newline at end of file diff --git a/1-59059-720-6/Chapter11/13-GlobalTempTable.sql b/1-59059-720-6/Chapter11/13-GlobalTempTable.sql new file mode 100644 index 0000000..ff848da --- /dev/null +++ b/1-59059-720-6/Chapter11/13-GlobalTempTable.sql @@ -0,0 +1,9 @@ +CREATE TABLE ##SharesTmp +(ShareDesc varchar(50), +Price numeric(18,5), +PriceDate datetime) +INSERT INTO ##SharesTmp +SELECT s.ShareDesc,sp.Price,sp.PriceDate + FROM ShareDetails.Shares s + JOIN ShareDetails.SharePrices sp ON sp.ShareId = s.ShareId +SELECT * FROM ##SharesTmp diff --git a/1-59059-720-6/Chapter11/13.Count.sql b/1-59059-720-6/Chapter11/13.Count.sql new file mode 100644 index 0000000..3f51825 --- /dev/null +++ b/1-59059-720-6/Chapter11/13.Count.sql @@ -0,0 +1,2 @@ +SELECT COUNT(*) AS 'Number of Rows' +FROM ShareDetails.Shares diff --git a/1-59059-720-6/Chapter11/14.CountFilter.sql b/1-59059-720-6/Chapter11/14.CountFilter.sql new file mode 100644 index 0000000..a5c4f1e --- /dev/null +++ b/1-59059-720-6/Chapter11/14.CountFilter.sql @@ -0,0 +1,3 @@ +SELECT COUNT(*) AS 'Number of Rows' +FROM ShareDetails.Shares +WHERE CurrentPrice > 10 diff --git a/1-59059-720-6/Chapter11/15-Sum.sql b/1-59059-720-6/Chapter11/15-Sum.sql new file mode 100644 index 0000000..1e59e6c --- /dev/null +++ b/1-59059-720-6/Chapter11/15-Sum.sql @@ -0,0 +1,4 @@ +SELECT SUM(Amount) AS 'Amount Deposited' + FROM TransactionDetails.Transactions + WHERE CustomerId = 1 + AND TransactionType = 1 diff --git a/1-59059-720-6/Chapter11/16-MaxMin.sql b/1-59059-720-6/Chapter11/16-MaxMin.sql new file mode 100644 index 0000000..cb25132 --- /dev/null +++ b/1-59059-720-6/Chapter11/16-MaxMin.sql @@ -0,0 +1,3 @@ +SELECT MAX(Price) MaxPrice,MIN(Price) MinPrice +FROM ShareDetails.SharePrices +WHERE ShareId = 1 diff --git a/1-59059-720-6/Chapter11/17-AVG.sql b/1-59059-720-6/Chapter11/17-AVG.sql new file mode 100644 index 0000000..f7a53f1 --- /dev/null +++ b/1-59059-720-6/Chapter11/17-AVG.sql @@ -0,0 +1,3 @@ +SELECT AVG(Price) AvgPrice +FROM ShareDetails.SharePrices +WHERE ShareId = 1 diff --git a/1-59059-720-6/Chapter11/18-GROUPBY.sql b/1-59059-720-6/Chapter11/18-GROUPBY.sql new file mode 100644 index 0000000..b81db28 --- /dev/null +++ b/1-59059-720-6/Chapter11/18-GROUPBY.sql @@ -0,0 +1,4 @@ + SELECT ShareId, MIN(Price) MinPrice, Max(Price) MaxPrice + FROM ShareDetails.SharePrices + WHERE ShareId < 9999 + GROUP BY ShareId diff --git a/1-59059-720-6/Chapter11/19-GROUPBY-2.sql b/1-59059-720-6/Chapter11/19-GROUPBY-2.sql new file mode 100644 index 0000000..b4c3d6b --- /dev/null +++ b/1-59059-720-6/Chapter11/19-GROUPBY-2.sql @@ -0,0 +1,5 @@ +SELECT sp.ShareId, s.ShareDesc,MIN(Price) MinPrice, Max(Price) MaxPrice + FROM ShareDetails.SharePrices sp + LEFT JOIN ShareDetails.Shares s ON s.ShareId = sp.ShareId + WHERE sp.ShareId < 9999 + GROUP BY ALL sp.ShareId, s.ShareDesc diff --git a/1-59059-720-6/Chapter11/2-JOINonTwoColumns.sql b/1-59059-720-6/Chapter11/2-JOINonTwoColumns.sql new file mode 100644 index 0000000..393a414 --- /dev/null +++ b/1-59059-720-6/Chapter11/2-JOINonTwoColumns.sql @@ -0,0 +1,4 @@ +SELECT s.ShareDesc,sp.Price,sp.PriceDate + FROM ShareDetails.Shares s + JOIN ShareDetails.SharePrices sp ON sp.ShareId = s.ShareId + AND sp.Price = s.CurrentPrice diff --git a/1-59059-720-6/Chapter11/20-HAVING.sql b/1-59059-720-6/Chapter11/20-HAVING.sql new file mode 100644 index 0000000..9f9c0a5 --- /dev/null +++ b/1-59059-720-6/Chapter11/20-HAVING.sql @@ -0,0 +1,6 @@ +SELECT sp.ShareId, s.ShareDesc,MIN(Price) MinPrice, Max(Price) MaxPrice + FROM ShareDetails.SharePrices sp + LEFT JOIN ShareDetails.Shares s ON s.ShareId = sp.ShareId + WHERE sp.ShareId < 9999 + GROUP BY ALL sp.ShareId, s.ShareDesc +HAVING MIN(Price) > 10 diff --git a/1-59059-720-6/Chapter11/21-PREDISTINCT.sql b/1-59059-720-6/Chapter11/21-PREDISTINCT.sql new file mode 100644 index 0000000..e0dd629 --- /dev/null +++ b/1-59059-720-6/Chapter11/21-PREDISTINCT.sql @@ -0,0 +1,3 @@ +SELECT s.ShareDesc,sp.Price,sp.PriceDate + FROM ShareDetails.Shares s + JOIN ShareDetails.SharePrices sp ON sp.ShareId = s.ShareId diff --git a/1-59059-720-6/Chapter11/22-DISTINCT-1.sql b/1-59059-720-6/Chapter11/22-DISTINCT-1.sql new file mode 100644 index 0000000..f2a230b --- /dev/null +++ b/1-59059-720-6/Chapter11/22-DISTINCT-1.sql @@ -0,0 +1,3 @@ +SELECT DISTINCT s.ShareDesc,sp.Price,sp.PriceDate + FROM ShareDetails.Shares s + JOIN ShareDetails.SharePrices sp ON sp.ShareId = s.ShareId diff --git a/1-59059-720-6/Chapter11/23-DISTINCT-2.sql b/1-59059-720-6/Chapter11/23-DISTINCT-2.sql new file mode 100644 index 0000000..c51eb0a --- /dev/null +++ b/1-59059-720-6/Chapter11/23-DISTINCT-2.sql @@ -0,0 +1,3 @@ +SELECT DISTINCT s.ShareDesc + FROM ShareDetails.Shares s + JOIN ShareDetails.SharePrices sp ON sp.ShareId = s.ShareId diff --git a/1-59059-720-6/Chapter11/24-DATEADD.sql b/1-59059-720-6/Chapter11/24-DATEADD.sql new file mode 100644 index 0000000..73b2a9b --- /dev/null +++ b/1-59059-720-6/Chapter11/24-DATEADD.sql @@ -0,0 +1,3 @@ +DECLARE @OldTime datetime +SET @OldTime = '24 March 2006 3:00 PM' +SELECT DATEADD(hh,4,@OldTime) diff --git a/1-59059-720-6/Chapter11/25-DATEADD-SUBTRACTION.sql b/1-59059-720-6/Chapter11/25-DATEADD-SUBTRACTION.sql new file mode 100644 index 0000000..1669a0a --- /dev/null +++ b/1-59059-720-6/Chapter11/25-DATEADD-SUBTRACTION.sql @@ -0,0 +1,3 @@ +DECLARE @OldTime datetime +SET @OldTime = '24 March 2006 3:00 PM' +SELECT DATEADD(hh,-6,@OldTime) diff --git a/1-59059-720-6/Chapter11/26-DATEDIFF.sql b/1-59059-720-6/Chapter11/26-DATEDIFF.sql new file mode 100644 index 0000000..df3fe0f --- /dev/null +++ b/1-59059-720-6/Chapter11/26-DATEDIFF.sql @@ -0,0 +1,4 @@ +DECLARE @FirstTime datetime, @SecondTime datetime +SET @FirstTime = '24 March 2006 3:00 PM' +SET @SecondTime = '24 March 2006 3:33PM' +SELECT DATEDIFF(ms,@FirstTime,@SecondTime) diff --git a/1-59059-720-6/Chapter11/27-DATENAME.sql b/1-59059-720-6/Chapter11/27-DATENAME.sql new file mode 100644 index 0000000..5c4c0b6 --- /dev/null +++ b/1-59059-720-6/Chapter11/27-DATENAME.sql @@ -0,0 +1,3 @@ +DECLARE @StatementDate datetime +SET @StatementDate = '24 March 2006 3:00 PM' +SELECT DATENAME(dw,@StatementDate) diff --git a/1-59059-720-6/Chapter11/28-DATEPART.sql b/1-59059-720-6/Chapter11/28-DATEPART.sql new file mode 100644 index 0000000..7e90d7e --- /dev/null +++ b/1-59059-720-6/Chapter11/28-DATEPART.sql @@ -0,0 +1,3 @@ +DECLARE @WhatsTheDay datetime +SET @WhatsTheDay = '24 March 2006 3:00 PM' +SELECT DATEPART(dd, @WhatsTheDay) diff --git a/1-59059-720-6/Chapter11/29-DATEFormatted.sql b/1-59059-720-6/Chapter11/29-DATEFormatted.sql new file mode 100644 index 0000000..f414c73 --- /dev/null +++ b/1-59059-720-6/Chapter11/29-DATEFormatted.sql @@ -0,0 +1,7 @@ +DECLARE @WhatsTheDay datetime +SET @WhatsTheDay = '24 March 2006 3:00 PM' +SELECT +DATENAME(dw,DATEPART(dd, @WhatsTheDay)) + ', ' + +CAST(DATEPART(dd,@WhatsTheDay) AS varchar(2)) + ' ' + +DATENAME(mm,@WhatsTheDay) + ' ' + +CAST(DATEPART(yyyy,@WhatsTheDay) AS char(4)) diff --git a/1-59059-720-6/Chapter11/3.LEFT-OUTERJOIN.sql b/1-59059-720-6/Chapter11/3.LEFT-OUTERJOIN.sql new file mode 100644 index 0000000..e89b928 --- /dev/null +++ b/1-59059-720-6/Chapter11/3.LEFT-OUTERJOIN.sql @@ -0,0 +1,3 @@ +SELECT s.ShareDesc,sp.Price,sp.PriceDate + FROM ShareDetails.Shares s + LEFT OUTER JOIN ShareDetails.SharePrices sp ON sp.ShareId = s.ShareId diff --git a/1-59059-720-6/Chapter11/30-ASCII.sql b/1-59059-720-6/Chapter11/30-ASCII.sql new file mode 100644 index 0000000..9929c68 --- /dev/null +++ b/1-59059-720-6/Chapter11/30-ASCII.sql @@ -0,0 +1,3 @@ +DECLARE @StringTest char(10) +SET @StringTest = ASCII('Robin ') +SELECT @StringTest diff --git a/1-59059-720-6/Chapter11/31-CHAR.sql b/1-59059-720-6/Chapter11/31-CHAR.sql new file mode 100644 index 0000000..24a59ed --- /dev/null +++ b/1-59059-720-6/Chapter11/31-CHAR.sql @@ -0,0 +1,3 @@ +DECLARE @StringTest char(10) +SET @StringTest = ASCII('Robin ') +SELECT CHAR(@StringTest) diff --git a/1-59059-720-6/Chapter11/32-CHAR-2.sql b/1-59059-720-6/Chapter11/32-CHAR-2.sql new file mode 100644 index 0000000..e160de9 --- /dev/null +++ b/1-59059-720-6/Chapter11/32-CHAR-2.sql @@ -0,0 +1,3 @@ +DECLARE @StringTest int +SET @StringTest = ASCII('Robin ') +SELECT CHAR(@StringTest) diff --git a/1-59059-720-6/Chapter11/33-LEFT.sql b/1-59059-720-6/Chapter11/33-LEFT.sql new file mode 100644 index 0000000..608c8bc --- /dev/null +++ b/1-59059-720-6/Chapter11/33-LEFT.sql @@ -0,0 +1,3 @@ +DECLARE @StringTest char(10) +SET @StringTest = 'Robin ' +SELECT LEFT(@StringTest,3) diff --git a/1-59059-720-6/Chapter11/34-Lower.sql b/1-59059-720-6/Chapter11/34-Lower.sql new file mode 100644 index 0000000..39fcd2a --- /dev/null +++ b/1-59059-720-6/Chapter11/34-Lower.sql @@ -0,0 +1,3 @@ +DECLARE @StringTest char(10) +SET @StringTest = 'Robin ' +SELECT LOWER(LEFT(@StringTest,3)) diff --git a/1-59059-720-6/Chapter11/35-LTRIM.sql b/1-59059-720-6/Chapter11/35-LTRIM.sql new file mode 100644 index 0000000..3c6ef67 --- /dev/null +++ b/1-59059-720-6/Chapter11/35-LTRIM.sql @@ -0,0 +1,3 @@ +DECLARE @StringTest char(10) +SET @StringTest = ' Robin' +SELECT 'Start-'+LTRIM(@StringTest),'Start-'+@StringTest diff --git a/1-59059-720-6/Chapter11/36-RIGHT.sql b/1-59059-720-6/Chapter11/36-RIGHT.sql new file mode 100644 index 0000000..c2d5431 --- /dev/null +++ b/1-59059-720-6/Chapter11/36-RIGHT.sql @@ -0,0 +1,3 @@ +DECLARE @StringTest char(10) +SET @StringTest = ' Robin' +SELECT RIGHT(@StringTest,3) diff --git a/1-59059-720-6/Chapter11/37-RTRIM.sql b/1-59059-720-6/Chapter11/37-RTRIM.sql new file mode 100644 index 0000000..8934610 --- /dev/null +++ b/1-59059-720-6/Chapter11/37-RTRIM.sql @@ -0,0 +1,3 @@ +DECLARE @StringTest char(10) +SET @StringTest = 'Robin' +SELECT @StringTest+'-End',RTRIM(@StringTest)+'-End' diff --git a/1-59059-720-6/Chapter11/38-STR.sql b/1-59059-720-6/Chapter11/38-STR.sql new file mode 100644 index 0000000..268c18d --- /dev/null +++ b/1-59059-720-6/Chapter11/38-STR.sql @@ -0,0 +1 @@ +SELECT 'A'+STR(82) \ No newline at end of file diff --git a/1-59059-720-6/Chapter11/39-LTRIM-AND-STR.sql b/1-59059-720-6/Chapter11/39-LTRIM-AND-STR.sql new file mode 100644 index 0000000..90e67b9 --- /dev/null +++ b/1-59059-720-6/Chapter11/39-LTRIM-AND-STR.sql @@ -0,0 +1 @@ +SELECT 'A'+LTRIM(STR(82)) \ No newline at end of file diff --git a/1-59059-720-6/Chapter11/4.LEFT-OUTERJOIN-WithNoPrice.sql b/1-59059-720-6/Chapter11/4.LEFT-OUTERJOIN-WithNoPrice.sql new file mode 100644 index 0000000..174293e --- /dev/null +++ b/1-59059-720-6/Chapter11/4.LEFT-OUTERJOIN-WithNoPrice.sql @@ -0,0 +1,4 @@ +SELECT s.ShareDesc,sp.Price,sp.PriceDate + FROM ShareDetails.Shares s + LEFT OUTER JOIN ShareDetails.SharePrices sp ON sp.ShareId = s.ShareId +WHERE sp.Price IS NULL diff --git a/1-59059-720-6/Chapter11/40-SUBSTRING.sql b/1-59059-720-6/Chapter11/40-SUBSTRING.sql new file mode 100644 index 0000000..18bb897 --- /dev/null +++ b/1-59059-720-6/Chapter11/40-SUBSTRING.sql @@ -0,0 +1,3 @@ +DECLARE @StringTest char(10) +SET @StringTest = 'Robin ' +SELECT SUBSTRING(@StringTest,3,LEN(@StringTest)) diff --git a/1-59059-720-6/Chapter11/41-UPPER.sql b/1-59059-720-6/Chapter11/41-UPPER.sql new file mode 100644 index 0000000..ef67479 --- /dev/null +++ b/1-59059-720-6/Chapter11/41-UPPER.sql @@ -0,0 +1,3 @@ +DECLARE @StringTest char(10) +SET @StringTest = 'Robin ' +SELECT UPPER(@StringTest) diff --git a/1-59059-720-6/Chapter11/42-CASE.sql b/1-59059-720-6/Chapter11/42-CASE.sql new file mode 100644 index 0000000..bd53c19 --- /dev/null +++ b/1-59059-720-6/Chapter11/42-CASE.sql @@ -0,0 +1,6 @@ +SET QUOTED_IDENTIFIER OFF +SELECT CustomerId,CASE WHEN CreditType = 0 THEN "Debits" ELSE "Credits" END AS TranType,SUM(Amount) + FROM TransactionDetails.Transactions t + JOIN TransactionDetails.TransactionTypes tt ON tt.TransActionTypeId = t.TransactionType + WHERE t.DateEntered BETWEEN '1 Aug 2005' AND '31 Aug 2005' + GROUP BY CustomerId,CreditType diff --git a/1-59059-720-6/Chapter11/43-CAST.sql b/1-59059-720-6/Chapter11/43-CAST.sql new file mode 100644 index 0000000..a8cc23c --- /dev/null +++ b/1-59059-720-6/Chapter11/43-CAST.sql @@ -0,0 +1,3 @@ +DECLARE @Cast int +SET @Cast = 1234 +SELECT CAST(@Cast as char(10)) + '-End' diff --git a/1-59059-720-6/Chapter11/44-CONVERT.sql b/1-59059-720-6/Chapter11/44-CONVERT.sql new file mode 100644 index 0000000..1810ccf --- /dev/null +++ b/1-59059-720-6/Chapter11/44-CONVERT.sql @@ -0,0 +1,3 @@ +DECLARE @Convert int +SET @Convert = 5678 +SELECT CONVERT(char(10),@Convert) + '-End' diff --git a/1-59059-720-6/Chapter11/45-ISDATE.sql b/1-59059-720-6/Chapter11/45-ISDATE.sql new file mode 100644 index 0000000..469dff8 --- /dev/null +++ b/1-59059-720-6/Chapter11/45-ISDATE.sql @@ -0,0 +1,3 @@ +DECLARE @IsDate char(15) +SET @IsDate = '31 Sep 2005' +SELECT ISDATE(@IsDate) diff --git a/1-59059-720-6/Chapter11/46-ISDATe-VALID.sql b/1-59059-720-6/Chapter11/46-ISDATe-VALID.sql new file mode 100644 index 0000000..fcac6cb --- /dev/null +++ b/1-59059-720-6/Chapter11/46-ISDATe-VALID.sql @@ -0,0 +1,3 @@ +DECLARE @IsDate char(15) +SET @IsDate = '30 Sep 2005' +SELECT ISDATE(@IsDate) diff --git a/1-59059-720-6/Chapter11/47-ISNULL.sql b/1-59059-720-6/Chapter11/47-ISNULL.sql new file mode 100644 index 0000000..e8fafd5 --- /dev/null +++ b/1-59059-720-6/Chapter11/47-ISNULL.sql @@ -0,0 +1,3 @@ +DECLARE @IsNull char(10) +SET @IsNull = NULL +SELECT ISNULL(@IsNull,GETDATE()) diff --git a/1-59059-720-6/Chapter11/48-ISNUMERIC-FALSE.sql b/1-59059-720-6/Chapter11/48-ISNUMERIC-FALSE.sql new file mode 100644 index 0000000..8a9f94c --- /dev/null +++ b/1-59059-720-6/Chapter11/48-ISNUMERIC-FALSE.sql @@ -0,0 +1,3 @@ +DECLARE @IsNum char(10) +SET @IsNum = 'Robin ' +SELECT ISNUMERIC(@IsNum) diff --git a/1-59059-720-6/Chapter11/49-ISNUMERIC-TRUE.sql b/1-59059-720-6/Chapter11/49-ISNUMERIC-TRUE.sql new file mode 100644 index 0000000..6480004 --- /dev/null +++ b/1-59059-720-6/Chapter11/49-ISNUMERIC-TRUE.sql @@ -0,0 +1,3 @@ +DECLARE @IsNum char(10) +SET @IsNum = '1234 ' +SELECT ISNUMERIC(@IsNum) diff --git a/1-59059-720-6/Chapter11/5.RIGHT-OUTERJOIN.sql b/1-59059-720-6/Chapter11/5.RIGHT-OUTERJOIN.sql new file mode 100644 index 0000000..cd10bf8 --- /dev/null +++ b/1-59059-720-6/Chapter11/5.RIGHT-OUTERJOIN.sql @@ -0,0 +1,3 @@ +SELECT s.ShareDesc,sp.Price,sp.PriceDate + FROM ShareDetails.SharePrices sp + RIGHT OUTER JOIN ShareDetails.Shares s ON sp.ShareId = s.ShareId diff --git a/1-59059-720-6/Chapter11/50-spaddmessage.sql b/1-59059-720-6/Chapter11/50-spaddmessage.sql new file mode 100644 index 0000000..43bf86b --- /dev/null +++ b/1-59059-720-6/Chapter11/50-spaddmessage.sql @@ -0,0 +1,2 @@ +sp_addmessage @msgnum=50001,@severity=1, +@msgtext='Customer is overdrawn' diff --git a/1-59059-720-6/Chapter11/51-CustomerOverdrawn.sql b/1-59059-720-6/Chapter11/51-CustomerOverdrawn.sql new file mode 100644 index 0000000..e31120a --- /dev/null +++ b/1-59059-720-6/Chapter11/51-CustomerOverdrawn.sql @@ -0,0 +1 @@ +RAISERROR (50001,1,1) \ No newline at end of file diff --git a/1-59059-720-6/Chapter11/52-sp_addmessage_chage.sql b/1-59059-720-6/Chapter11/52-sp_addmessage_chage.sql new file mode 100644 index 0000000..5f5953c --- /dev/null +++ b/1-59059-720-6/Chapter11/52-sp_addmessage_chage.sql @@ -0,0 +1,2 @@ +sp_addmessage @msgnum =50001,@severity=1, +@msgtext='Customer is overdrawn. CustomerId= %010u',@replace='replace' diff --git a/1-59059-720-6/Chapter11/53-RAISEERROR-improved.sql b/1-59059-720-6/Chapter11/53-RAISEERROR-improved.sql new file mode 100644 index 0000000..8b1c36a --- /dev/null +++ b/1-59059-720-6/Chapter11/53-RAISEERROR-improved.sql @@ -0,0 +1 @@ +RAISERROR (50001,1,1,243) \ No newline at end of file diff --git a/1-59059-720-6/Chapter11/54-DOUBLE-@@ERROR.sql b/1-59059-720-6/Chapter11/54-DOUBLE-@@ERROR.sql new file mode 100644 index 0000000..d285d8c --- /dev/null +++ b/1-59059-720-6/Chapter11/54-DOUBLE-@@ERROR.sql @@ -0,0 +1,3 @@ +SELECT 100/0 +SELECT @@ERROR +SELECT @@ERROR diff --git a/1-59059-720-6/Chapter11/55-RAISERROR-With-@@ERROR.sql b/1-59059-720-6/Chapter11/55-RAISERROR-With-@@ERROR.sql new file mode 100644 index 0000000..f1261d0 --- /dev/null +++ b/1-59059-720-6/Chapter11/55-RAISERROR-With-@@ERROR.sql @@ -0,0 +1,2 @@ +RAISERROR (50001,1,1,243) +SELECT @@ERROR diff --git a/1-59059-720-6/Chapter11/56-RAISERROR-Severity11.sql b/1-59059-720-6/Chapter11/56-RAISERROR-Severity11.sql new file mode 100644 index 0000000..753ae0e --- /dev/null +++ b/1-59059-720-6/Chapter11/56-RAISERROR-Severity11.sql @@ -0,0 +1,2 @@ +RAISERROR (50001,11,1,243) +SELECT @@ERROR diff --git a/1-59059-720-6/Chapter11/57-TryCatch-1.sql b/1-59059-720-6/Chapter11/57-TryCatch-1.sql new file mode 100644 index 0000000..afdcd17 --- /dev/null +++ b/1-59059-720-6/Chapter11/57-TryCatch-1.sql @@ -0,0 +1,10 @@ +DECLARE @Probs int +BEGIN TRY + SELECT 'This will work' + SELECT @Probs='Not Right' + SELECT 10+5,'This will also work, however the error means it wont run' +END TRY +BEGIN CATCH + SELECT 'An error has occurred at line ' + LTRIM(STR(ERROR_LINE())) + + ' with error ' + LTRIM(STR(ERROR_NUMBER())) + ' ' + ERROR_MESSAGE() +END CATCH diff --git a/1-59059-720-6/Chapter11/58-TryCatch-2.sql b/1-59059-720-6/Chapter11/58-TryCatch-2.sql new file mode 100644 index 0000000..3f4298b --- /dev/null +++ b/1-59059-720-6/Chapter11/58-TryCatch-2.sql @@ -0,0 +1,16 @@ +DECLARE @Probs int +BEGIN TRY + SELECT 'This will work' + BEGIN TRY + SELECT @Probs='Not Right' + SELECT 10+5,'This will also work, however the error means it wont run' + END TRY + BEGIN CATCH + SELECT 'The second catch block' + END CATCH + SELECT 'And then this will now work' +END TRY +BEGIN CATCH + SELECT 'An error has occurred at line ' + LTRIM(STR(ERROR_LINE())) + + ' with error ' + LTRIM(STR(ERROR_NUMBER())) + ' ' + ERROR_MESSAGE() +END CATCH diff --git a/1-59059-720-6/Chapter11/59-TryCatch-3.sql b/1-59059-720-6/Chapter11/59-TryCatch-3.sql new file mode 100644 index 0000000..cc7166f --- /dev/null +++ b/1-59059-720-6/Chapter11/59-TryCatch-3.sql @@ -0,0 +1,15 @@ +DECLARE @Probs int +BEGIN TRY + SELECT 'This will work' + BEGIN TRY + SELECT * FROM #Temp + END TRY + BEGIN CATCH + SELECT 'The second catch block' + END CATCH + SELECT 'And then this will now work' +END TRY +BEGIN CATCH + SELECT 'An error has occurred at line ' + LTRIM(STR(ERROR_LINE())) + + ' with error ' + LTRIM(STR(ERROR_NUMBER())) + ' ' + ERROR_MESSAGE() +END CATCH diff --git a/1-59059-720-6/Chapter11/6-InsertForTest.sql b/1-59059-720-6/Chapter11/6-InsertForTest.sql new file mode 100644 index 0000000..933c40a --- /dev/null +++ b/1-59059-720-6/Chapter11/6-InsertForTest.sql @@ -0,0 +1,3 @@ +INSERT INTO ShareDetails.SharePrices +(ShareId, Price, PriceDate) +VALUES (99999,12.34,'1 Aug 2005 10:10AM') diff --git a/1-59059-720-6/Chapter11/60-TryCatch-LocalVariable.sql b/1-59059-720-6/Chapter11/60-TryCatch-LocalVariable.sql new file mode 100644 index 0000000..8969b46 --- /dev/null +++ b/1-59059-720-6/Chapter11/60-TryCatch-LocalVariable.sql @@ -0,0 +1,19 @@ +DECLARE @Probs int +SELECT 'This will work' +BEGIN TRY + SELECT @Probs='Not Right' + SELECT 10+5,'This will also work, however the error means it wont run' +END TRY +BEGIN CATCH + DECLARE @ErrMsg NVARCHAR(4000) + DECLARE @ErrSeverity INT + DECLARE @ErrState INT + SELECT 'Blimey! An error' + + SELECT + @ErrMsg = ERROR_MESSAGE(), + @ErrSeverity = ERROR_SEVERITY(), + @ErrState = ERROR_STATE(); + + RAISERROR (@ErrMsg,@ErrSeverity,@ErrState) +END CATCH diff --git a/1-59059-720-6/Chapter11/7-Full-OUTERJOIN.sql b/1-59059-720-6/Chapter11/7-Full-OUTERJOIN.sql new file mode 100644 index 0000000..5da19ed --- /dev/null +++ b/1-59059-720-6/Chapter11/7-Full-OUTERJOIN.sql @@ -0,0 +1,3 @@ +SELECT s.ShareDesc,sp.Price,sp.PriceDate + FROM ShareDetails.SharePrices sp + FULL OUTER JOIN ShareDetails.Shares s ON sp.ShareId = s.ShareId diff --git a/1-59059-720-6/Chapter11/8-CROSS-JOIN.sql b/1-59059-720-6/Chapter11/8-CROSS-JOIN.sql new file mode 100644 index 0000000..fe8bf2c --- /dev/null +++ b/1-59059-720-6/Chapter11/8-CROSS-JOIN.sql @@ -0,0 +1,3 @@ +SELECT s.ShareDesc,sp.Price,sp.PriceDate + FROM ShareDetails.SharePrices sp + CROSS JOIN ShareDetails.Shares s diff --git a/1-59059-720-6/Chapter11/9-Variables.sql b/1-59059-720-6/Chapter11/9-Variables.sql new file mode 100644 index 0000000..9700c81 --- /dev/null +++ b/1-59059-720-6/Chapter11/9-Variables.sql @@ -0,0 +1,6 @@ +DECLARE @MyDate datetime, @CurrPriceInCents money +SET @MyDate = GETDATE() +SELECT @CurrPriceInCents = CurrentPrice * 100 + FROM ShareDetails.Shares + WHERE ShareId = 2 +SELECT @MyDate,@CurrPriceInCents diff --git a/1-59059-720-6/Chapter11/readme.txt b/1-59059-720-6/Chapter11/readme.txt new file mode 100644 index 0000000..d844631 --- /dev/null +++ b/1-59059-720-6/Chapter11/readme.txt @@ -0,0 +1,4 @@ +This chapter deals with advancing your T-SQL knowledge + +Robin Dewson +www.fat-belly.com \ No newline at end of file diff --git a/1-59059-720-6/Chapter12/1-MaxSharePriceExample.sql b/1-59059-720-6/Chapter12/1-MaxSharePriceExample.sql new file mode 100644 index 0000000..cba65ef --- /dev/null +++ b/1-59059-720-6/Chapter12/1-MaxSharePriceExample.sql @@ -0,0 +1,13 @@ +ALTER TABLE ShareDetails.Shares +ADD MaximumSharePrice money + +DECLARE @MaxPrice money +SELECT @MaxPrice = MAX(Price) + FROM ShareDetails.SharePrices + WHERE ShareId = 1 +SELECT @MaxPrice + +UPDATE ShareDetails.Shares +SET MaximumSharePrice = @MaxPrice +WHERE ShareId = 1 + diff --git a/1-59059-720-6/Chapter12/10-CTE-NoCTE.sql b/1-59059-720-6/Chapter12/10-CTE-NoCTE.sql new file mode 100644 index 0000000..1700ba8 --- /dev/null +++ b/1-59059-720-6/Chapter12/10-CTE-NoCTE.sql @@ -0,0 +1,16 @@ +USE AdventureWorks +GO +SELECT p.ProductSubcategoryID, s.Name,SUM(ListPrice) AS ListPrice + INTO #Temp1 + FROM Production.Product p + JOIN Production.ProductSubcategory s ON s.ProductSubcategoryID = + p.ProductSubcategoryID + WHERE p.ProductSubcategoryID IS NOT NULL + GROUP BY p.ProductSubcategoryID, s.Name + +SELECT ProductSubcategoryID,Name,MAX(ListPrice) + FROM #Temp1 + GROUP BY ProductSubcategoryID, Name +HAVING MAX(ListPrice) = (SELECT MAX(ListPrice) FROM #Temp1) + +DROP TABLE #Temp1 diff --git a/1-59059-720-6/Chapter12/11-CTE-WithCTE.sql b/1-59059-720-6/Chapter12/11-CTE-WithCTE.sql new file mode 100644 index 0000000..f27430d --- /dev/null +++ b/1-59059-720-6/Chapter12/11-CTE-WithCTE.sql @@ -0,0 +1,13 @@ +WITH ProdList (ProductSubcategoryID,Name,ListPrice) AS +( +SELECT p.ProductSubcategoryID, s.Name,SUM(ListPrice) AS ListPrice + FROM Production.Product p + JOIN Production.ProductSubcategory s ON s.ProductSubcategoryID = + p.ProductSubcategoryID + WHERE p.ProductSubcategoryID IS NOT NULL + GROUP BY p.ProductSubcategoryID, s.Name +) +SELECT ProductSubcategoryID,Name,MAX(ListPrice) + FROM ProdList + GROUP BY ProductSubcategoryID, Name + HAVING MAX(ListPrice) = (SELECT MAX(ListPrice) FROM ProdList) diff --git a/1-59059-720-6/Chapter12/12-Recursive-CTE.sql b/1-59059-720-6/Chapter12/12-Recursive-CTE.sql new file mode 100644 index 0000000..24e229e --- /dev/null +++ b/1-59059-720-6/Chapter12/12-Recursive-CTE.sql @@ -0,0 +1,43 @@ +USE AdventureWorks; +GO +WITH EmployeeReportingStructure +(ManagerID, EmployeeID, EmployeeLevel, Level, +ManagerContactId,ManagerTitle,ManagerFirst,ManagerLast, +EmployeeTitle,EmployeeFirst,EmployeeLast) +AS +( +-- Anchor member definition + SELECT e.ManagerID, e.EmployeeID, e.Title as EmployeeLevel, + 0 AS Level, + e.ContactId as ManagerContactId, + CAST(' ' as nvarchar(8)) as ManagerTitle, + CAST(' ' as nvarchar(50)) as ManagerFirst, + CAST(' ' as nvarchar(50)) as ManagerLast, + c.Title as EmployeeTitle,c.FirstName as EmployeeFirst, + c.LastName as EmployeeLast + FROM HumanResources.Employee AS e + INNER JOIN Person.Contact c ON c.ContactId = e.ContactId + WHERE ManagerID IS NULL + UNION ALL +-- Recursive member definition + SELECT e.ManagerID, e.EmployeeID, e.Title as EmployeeLevel, Level + 1, + e.ContactId as ManagerContactId, + m.Title as ManagerTitle,m.FirstName as ManagerFirst, + m.LastName as ManagerLast, + c.Title as EmployeeTitle,c.FirstName as EmployeeFirst, + c.LastName as EmployeeLast + FROM HumanResources.Employee AS e + INNER JOIN Person.Contact c ON c.ContactId = e.ContactId + INNER JOIN EmployeeReportingStructure AS d + ON d.EmployeeID = e.ManagerID + INNER JOIN Person.Contact m ON m.ContactId = d.ManagerContactId +) +-- Statement that executes the CTE +SELECT ManagerID, EmployeeID, +ISNULL(ManagerTitle+' ','')+ManagerFirst+' '+ManagerLast as Manager, +EmployeeLevel, +ISNULL(EmployeeTitle+' ','')+EmployeeFirst+' '+EmployeeLast as Employee, +Level +FROM EmployeeReportingStructure +ORDER BY Level,EmployeeLast,EmployeeFirst +OPTION (MAXRECURSION 4) diff --git a/1-59059-720-6/Chapter12/13-Pre-PIVOT.sql b/1-59059-720-6/Chapter12/13-Pre-PIVOT.sql new file mode 100644 index 0000000..71ddbe3 --- /dev/null +++ b/1-59059-720-6/Chapter12/13-Pre-PIVOT.sql @@ -0,0 +1,7 @@ +USE AdventureWorks +GO +SELECT productID,UnitPriceDiscount,SUM(linetotal) + FROM Sales.SalesOrderDetail + WHERE productID IN (776,711,747) + GROUP BY productID,UnitPriceDiscount + ORDER BY productID,UnitPriceDiscount diff --git a/1-59059-720-6/Chapter12/14-PIVOT.sql b/1-59059-720-6/Chapter12/14-PIVOT.sql new file mode 100644 index 0000000..dff8093 --- /dev/null +++ b/1-59059-720-6/Chapter12/14-PIVOT.sql @@ -0,0 +1,11 @@ +SELECT pt.Discount,ISNULL([711],0.00) As Product711, + ISNULL([747],0.00) As Product747,ISNULL([776],0.00) As Product776 +FROM +(SELECT sod.LineTotal, sod.ProductID, sod.UnitPriceDiscount as Discount + FROM Sales.SalesOrderDetail sod) so +PIVOT +( +SUM(so.LineTotal) +FOR so.ProductID IN ([776], [711], [747]) +) AS pt +ORDER BY pt.Discount diff --git a/1-59059-720-6/Chapter12/15-UNPIVOT-Into-TEMP.sql b/1-59059-720-6/Chapter12/15-UNPIVOT-Into-TEMP.sql new file mode 100644 index 0000000..c0d5c95 --- /dev/null +++ b/1-59059-720-6/Chapter12/15-UNPIVOT-Into-TEMP.sql @@ -0,0 +1,14 @@ +USE AdventureWorks +go +SELECT pt.Discount,ISNULL([711],0.00) As Product711, + ISNULL([747],0.00) As Product747,ISNULL([776],0.00) As Product776 +INTO #Temp1 +FROM +(SELECT sod.LineTotal, sod.ProductID, sod.UnitPriceDiscount as Discount + FROM Sales.SalesOrderDetail sod) so +PIVOT +( +SUM(so.LineTotal) +FOR so.ProductID IN ([776], [711], [747]) +) AS pt +ORDER BY pt.Discount diff --git a/1-59059-720-6/Chapter12/16-ROW_NUMBER.sql b/1-59059-720-6/Chapter12/16-ROW_NUMBER.sql new file mode 100644 index 0000000..a553ac6 --- /dev/null +++ b/1-59059-720-6/Chapter12/16-ROW_NUMBER.sql @@ -0,0 +1,7 @@ +USE AdventureWorks +GO +SELECT ROW_NUMBER() OVER(ORDER BY LastName) AS RowNum, + FirstName + ' ' + LastName + FROM HumanResources.vEmployee + WHERE JobTitle = 'Production Technician - WC60' + ORDER BY LastName diff --git a/1-59059-720-6/Chapter12/17-RowNum-2.sql b/1-59059-720-6/Chapter12/17-RowNum-2.sql new file mode 100644 index 0000000..a783e4b --- /dev/null +++ b/1-59059-720-6/Chapter12/17-RowNum-2.sql @@ -0,0 +1,8 @@ +USE AdventureWorks +GO +SELECT ROW_NUMBER() + OVER(PARTITION BY SUBSTRING(LastName,1,1) + ORDER BY LastName) AS RowNum, FirstName + ' ' + LastName + FROM HumanResources.vEmployee + WHERE JobTitle = 'Production Technician - WC60' + ORDER BY LastName diff --git a/1-59059-720-6/Chapter12/18-Rank.sql b/1-59059-720-6/Chapter12/18-Rank.sql new file mode 100644 index 0000000..0de3887 --- /dev/null +++ b/1-59059-720-6/Chapter12/18-Rank.sql @@ -0,0 +1,7 @@ +USE AdventureWorks +GO +SELECT ROW_NUMBER() OVER(ORDER BY Department) AS RowNum, + RANK() OVER(ORDER BY Department) AS Ranking, + FirstName + ' ' + LastName AS Employee, Department + FROM HumanResources.vEmployeeDepartment + ORDER BY RowNum diff --git a/1-59059-720-6/Chapter12/19-DenseRank.sql b/1-59059-720-6/Chapter12/19-DenseRank.sql new file mode 100644 index 0000000..ad3b0a8 --- /dev/null +++ b/1-59059-720-6/Chapter12/19-DenseRank.sql @@ -0,0 +1,7 @@ +USE AdventureWorks +GO +SELECT ROW_NUMBER() OVER(ORDER BY Department) AS RowNum, + DENSE_RANK() OVER(ORDER BY Department) AS Ranking, + CONVERT(varchar(25),FirstName + ' ' + LastName), Department + FROM HumanResources.vEmployeeDepartment + ORDER BY RowNum diff --git a/1-59059-720-6/Chapter12/2-Correlated-Subquery.sql b/1-59059-720-6/Chapter12/2-Correlated-Subquery.sql new file mode 100644 index 0000000..a3668ab --- /dev/null +++ b/1-59059-720-6/Chapter12/2-Correlated-Subquery.sql @@ -0,0 +1,11 @@ +SELECT ShareId,MaximumSharePrice +FROM ShareDetails.Shares +UPDATE ShareDetails.Shares +SET MaximumSharePrice = (SELECT MAX(Price) + FROM ShareDetails.SharePrices sp + WHERE sp.ShareId = s.ShareId) +FROM ShareDetails.Shares s +SELECT ShareId,MaximumSharePrice +FROM ShareDetails.Shares + + diff --git a/1-59059-720-6/Chapter12/20-NTile.sql b/1-59059-720-6/Chapter12/20-NTile.sql new file mode 100644 index 0000000..e902762 --- /dev/null +++ b/1-59059-720-6/Chapter12/20-NTile.sql @@ -0,0 +1,5 @@ +USE AdventureWorks +GO +SELECT NTILE(10) OVER(ORDER BY Department) AS NTile, + FirstName + ' ' + LastName, Department + FROM HumanResources.vEmployeeDepartment diff --git a/1-59059-720-6/Chapter12/21-sp_tableoption.sql b/1-59059-720-6/Chapter12/21-sp_tableoption.sql new file mode 100644 index 0000000..4677c69 --- /dev/null +++ b/1-59059-720-6/Chapter12/21-sp_tableoption.sql @@ -0,0 +1,2 @@ +sp_tableoption 'TransactionDetails.Transactions', +'large value types out of row',1 diff --git a/1-59059-720-6/Chapter12/22-LOB-1.sql b/1-59059-720-6/Chapter12/22-LOB-1.sql new file mode 100644 index 0000000..d0fb49d --- /dev/null +++ b/1-59059-720-6/Chapter12/22-LOB-1.sql @@ -0,0 +1,3 @@ +UPDATE TransactionDetails.Transactions +SET Notes = 'From online auction sale of Redskins memorabilia, helmet' +WHERE TransactionId = 1 diff --git a/1-59059-720-6/Chapter12/23-LOB-Select.sql b/1-59059-720-6/Chapter12/23-LOB-Select.sql new file mode 100644 index 0000000..1695599 --- /dev/null +++ b/1-59059-720-6/Chapter12/23-LOB-Select.sql @@ -0,0 +1 @@ +SELECT * FROM TransactionDetails.Transactions \ No newline at end of file diff --git a/1-59059-720-6/Chapter12/24-LOB-Update-Dolphins.sql b/1-59059-720-6/Chapter12/24-LOB-Update-Dolphins.sql new file mode 100644 index 0000000..30b5f3d --- /dev/null +++ b/1-59059-720-6/Chapter12/24-LOB-Update-Dolphins.sql @@ -0,0 +1,3 @@ +UPDATE TransactionDetails.Transactions +SET Notes .WRITE('From online auction sale of Dolphins memorabilia, helmet',0,8000) +WHERE TransactionId = 1 diff --git a/1-59059-720-6/Chapter12/25-Update-Raiders.sql b/1-59059-720-6/Chapter12/25-Update-Raiders.sql new file mode 100644 index 0000000..735aed9 --- /dev/null +++ b/1-59059-720-6/Chapter12/25-Update-Raiders.sql @@ -0,0 +1,3 @@ +UPDATE TransactionDetails.Transactions +SET Notes .WRITE('Raiders',28,8) +WHERE TransactionId = 1 diff --git a/1-59059-720-6/Chapter12/26-Select-Raiders.sql b/1-59059-720-6/Chapter12/26-Select-Raiders.sql new file mode 100644 index 0000000..e2d0094 --- /dev/null +++ b/1-59059-720-6/Chapter12/26-Select-Raiders.sql @@ -0,0 +1 @@ +SELECT * FROM TransactionDetails.Transactions WHERE TransactionId = 1 \ No newline at end of file diff --git a/1-59059-720-6/Chapter12/27-Image-LOB.sql b/1-59059-720-6/Chapter12/27-Image-LOB.sql new file mode 100644 index 0000000..221d80e --- /dev/null +++ b/1-59059-720-6/Chapter12/27-Image-LOB.sql @@ -0,0 +1,5 @@ +INSERT INTO TransactionDetails.Transactions +(CustomerId, TransactionType, DateEntered,Amount,Notes, RelatedProductId) +SELECT 2, 1, GETDATE(),1000,PhotoToLoad.*, 1 +FROM OPENROWSET +(BULK 'c:\temp\photo.tif', SINGLE_BLOB) PhotoToLoad \ No newline at end of file diff --git a/1-59059-720-6/Chapter12/28-UpdateImage.sql b/1-59059-720-6/Chapter12/28-UpdateImage.sql new file mode 100644 index 0000000..bacb4b4 --- /dev/null +++ b/1-59059-720-6/Chapter12/28-UpdateImage.sql @@ -0,0 +1,6 @@ +UPDATE TransactionDetails.Transactions +SET Notes = ( +SELECT PhotoToLoad.* +FROM OPENROWSET +(BULK 'c:\Temp\photo2.jpg', SINGLE_BLOB) PhotoToLoad) +WHERE transactionId = 9 \ No newline at end of file diff --git a/1-59059-720-6/Chapter12/3-Backup-ReferenceONLY.sql b/1-59059-720-6/Chapter12/3-Backup-ReferenceONLY.sql new file mode 100644 index 0000000..98170ed --- /dev/null +++ b/1-59059-720-6/Chapter12/3-Backup-ReferenceONLY.sql @@ -0,0 +1,22 @@ +No need to run as its for reference only + + +DECLARE @BackupSet AS INT +SELECT @BackupSet = position + FROM msdb..backupset + WHERE database_name='ApressFinancial' + AND backup_set_id= + (SELECT MAX(backup_set_id) + FROM msdb..backupset s + WHERE database_name='ApressFinancial') +IF @BackupSet IS NULL +BEGIN + RAISERROR('Verify failed. Backup information for database + ''ApressFinancial'' not found.', 16, 1) +END +RESTORE VERIFYONLY +FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\ +ApressFinancial\ApressFinancial_backup_200508061136.bak' +WITH FILE = @BackupSet, +NOUNLOAD, +NOREWIND diff --git a/1-59059-720-6/Chapter12/4-IN.sql b/1-59059-720-6/Chapter12/4-IN.sql new file mode 100644 index 0000000..3cbfcf1 --- /dev/null +++ b/1-59059-720-6/Chapter12/4-IN.sql @@ -0,0 +1,3 @@ +SELECT * + FROM ShareDetails.Shares + WHERE ShareId IN (1,3,5) \ No newline at end of file diff --git a/1-59059-720-6/Chapter12/5-IN-Subquery.sql b/1-59059-720-6/Chapter12/5-IN-Subquery.sql new file mode 100644 index 0000000..88b7003 --- /dev/null +++ b/1-59059-720-6/Chapter12/5-IN-Subquery.sql @@ -0,0 +1,8 @@ +SELECT * + FROM ShareDetails.Shares + WHERE ShareId IN (SELECT ShareId + FROM ShareDetails.Shares + WHERE CurrentPrice > (SELECT MIN(CurrentPrice) + FROM ShareDetails.Shares) + AND CurrentPrice < (SELECT MAX(CurrentPrice) + FROM ShareDetails.Shares)) diff --git a/1-59059-720-6/Chapter12/6-IN-OR.sql b/1-59059-720-6/Chapter12/6-IN-OR.sql new file mode 100644 index 0000000..132bd45 --- /dev/null +++ b/1-59059-720-6/Chapter12/6-IN-OR.sql @@ -0,0 +1,5 @@ +SELECT * + FROM ShareDetails.Shares + WHERE ShareId = 1 + OR ShareId = 3 + OR ShareId = 5 diff --git a/1-59059-720-6/Chapter12/7-NOT-EXISTS.sql b/1-59059-720-6/Chapter12/7-NOT-EXISTS.sql new file mode 100644 index 0000000..e8ab870 --- /dev/null +++ b/1-59059-720-6/Chapter12/7-NOT-EXISTS.sql @@ -0,0 +1,5 @@ +SELECT * + FROM ShareDetails.Shares s + WHERE NOT EXISTS (SELECT 1 + FROM TransactionDetails.Transactions t + WHERE t.RelatedShareId = s.ShareId) diff --git a/1-59059-720-6/Chapter12/8-CROSS-APPLY.sql b/1-59059-720-6/Chapter12/8-CROSS-APPLY.sql new file mode 100644 index 0000000..8f72115 --- /dev/null +++ b/1-59059-720-6/Chapter12/8-CROSS-APPLY.sql @@ -0,0 +1,27 @@ +CREATE FUNCTION TransactionDetails.ReturnTransactions +(@CustId bigint) RETURNS @Trans TABLE +(TransactionId bigint, +CustomerId bigint, +TransactionDescription nvarchar(30), +DateEntered datetime, +Amount money) +AS +BEGIN + INSERT INTO @Trans + SELECT TransactionId, CustomerId, TransactionDescription, + DateEntered, Amount + FROM TransactionDetails.Transactions t + JOIN TransactionDetails.TransactionTypes tt ON + tt.TransactionTypeId = t.TransactionType + WHERE CustomerId = @CustId + RETURN +END +GO +SELECT c.CustomerFirstName, CustomerLastName, +Trans.TransactionId,TransactionDescription, +DateEntered,Amount +FROM CustomerDetails.Customers AS c + CROSS APPLY + TransactionDetails.ReturnTransactions(c.CustomerId) + AS Trans + diff --git a/1-59059-720-6/Chapter12/9-OUTER-APPLY.sql b/1-59059-720-6/Chapter12/9-OUTER-APPLY.sql new file mode 100644 index 0000000..4479ac0 --- /dev/null +++ b/1-59059-720-6/Chapter12/9-OUTER-APPLY.sql @@ -0,0 +1,7 @@ +SELECT c.CustomerFirstName, CustomerLastName, +Trans.TransactionId,TransactionDescription, +DateEntered,Amount +FROM CustomerDetails.Customers AS c + OUTER APPLY + TransactionDetails.ReturnTransactions(c.CustomerId) + AS Trans diff --git a/1-59059-720-6/Chapter12/readme.txt b/1-59059-720-6/Chapter12/readme.txt new file mode 100644 index 0000000..052e341 --- /dev/null +++ b/1-59059-720-6/Chapter12/readme.txt @@ -0,0 +1,4 @@ + + +Robin Dewson +www.fat-belly.com \ No newline at end of file diff --git a/1-59059-720-6/Chapter13/1-Create Trigger.sql b/1-59059-720-6/Chapter13/1-Create Trigger.sql new file mode 100644 index 0000000..b6dc405 --- /dev/null +++ b/1-59059-720-6/Chapter13/1-Create Trigger.sql @@ -0,0 +1,17 @@ +CREATE TRIGGER trgInsTransactions +ON TransactionDetails.Transactions +AFTER INSERT +AS +UPDATE CustomerDetails.Customers +SET ClearedBalance = ClearedBalance + +(SELECT CASE WHEN CreditType = 0 +THEN i.Amount * -1 +ELSE i.Amount +END +FROM INSERTED i +JOIN TransactionDetails.TransactionTypes tt +ON tt.TransactionTypeId = i.TransactionType +WHERE AffectCashBalance = 1) +FROM CustomerDetails.Customers c +JOIN INSERTED i ON i.CustomerId = c.CustomerId +GO \ No newline at end of file diff --git a/1-59059-720-6/Chapter13/10-Test-AlterUpdate.sql b/1-59059-720-6/Chapter13/10-Test-AlterUpdate.sql new file mode 100644 index 0000000..7868674 --- /dev/null +++ b/1-59059-720-6/Chapter13/10-Test-AlterUpdate.sql @@ -0,0 +1,15 @@ +SELECT * +FROM TransactionDetails.Transactions +WHERE TransactionId=5 +SELECT ClearedBalance +FROM CustomerDetails.Customers +WHERE CustomerId = 1 +UPDATE TransactionDetails.Transactions +SET DateEntered = DATEADD(dd,-1,DateEntered) +WHERE TransactionId = 5 +SELECT * +FROM TransactionDetails.Transactions +WHERE TransactionId=5 +SELECT ClearedBalance +FROM CustomerDetails.Customers +WHERE CustomerId = 1 \ No newline at end of file diff --git a/1-59059-720-6/Chapter13/12-COLUMNS-UPDATED.sql b/1-59059-720-6/Chapter13/12-COLUMNS-UPDATED.sql new file mode 100644 index 0000000..4fadf49 --- /dev/null +++ b/1-59059-720-6/Chapter13/12-COLUMNS-UPDATED.sql @@ -0,0 +1,35 @@ +ALTER TRIGGER TransactionDetails.trgInsTransactions +ON TransactionDetails.Transactions +AFTER UPDATE,INSERT +AS +IF (SUBSTRING(COLUMNS_UPDATED(),1,1) = power(2,(3-1)) +OR SUBSTRING(COLUMNS_UPDATED(),1,1) = power(2,(5-1))) +BEGIN +UPDATE CustomerDetails.Customers +SET ClearedBalance = ClearedBalance - +ISNULL((SELECT CASE WHEN CreditType = 0 +THEN d.Amount * -1 +ELSE d.Amount +END +FROM DELETED d +JOIN TransactionDetails.TransactionTypes tt +ON tt.TransactionTypeId = d.TransactionType +WHERE AffectCashBalance = 1),0) +FROM CustomerDetails.Customers c +JOIN DELETED d ON d.CustomerId = c.CustomerId +UPDATE CustomerDetails.Customers +SET ClearedBalance = ClearedBalance + +ISNULL((SELECT CASE WHEN CreditType = 0 +THEN i.Amount * -1 +ELSE i.Amount +END +FROM INSERTED i +JOIN TransactionDetails.TransactionTypes tt +ON tt.TransactionTypeId = i.TransactionType +WHERE AffectCashBalance = 1),0) +FROM CustomerDetails.Customers c +JOIN INSERTED i ON i.CustomerId = c.CustomerId +RAISERROR ('We have completed an update ',10,1) +END +ELSE +RAISERROR ('Updates have been skipped',10,1) \ No newline at end of file diff --git a/1-59059-720-6/Chapter13/13-DDL Trigger.sql b/1-59059-720-6/Chapter13/13-DDL Trigger.sql new file mode 100644 index 0000000..14529c5 --- /dev/null +++ b/1-59059-720-6/Chapter13/13-DDL Trigger.sql @@ -0,0 +1,14 @@ +CREATE TRIGGER trgSprocs +ON DATABASE +FOR CREATE_PROCEDURE, ALTER_PROCEDURE, DROP_PROCEDURE +AS +IF DATEPART(hh,GETDATE()) > 9 AND DATEPART(hh,GETDATE()) < 17 +BEGIN +DECLARE @Message nvarchar(max) +SELECT @Message = +'Completing work during core hours. Trying to release - ' ++ EVENTDATA().value +('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','nvarchar(max)') +RAISERROR (@Message, 16, 1) +ROLLBACK +END \ No newline at end of file diff --git a/1-59059-720-6/Chapter13/14-Test-DDL-Trigger.sql b/1-59059-720-6/Chapter13/14-Test-DDL-Trigger.sql new file mode 100644 index 0000000..01b43ff --- /dev/null +++ b/1-59059-720-6/Chapter13/14-Test-DDL-Trigger.sql @@ -0,0 +1,3 @@ +CREATE PROCEDURE Test1 +AS +SELECT 'Hello all' \ No newline at end of file diff --git a/1-59059-720-6/Chapter13/15-Drop-DDL-Trigger.sql b/1-59059-720-6/Chapter13/15-Drop-DDL-Trigger.sql new file mode 100644 index 0000000..aa2f02e --- /dev/null +++ b/1-59059-720-6/Chapter13/15-Drop-DDL-Trigger.sql @@ -0,0 +1 @@ +DROP TRIGGER trgSprocs ON DATABASE \ No newline at end of file diff --git a/1-59059-720-6/Chapter13/16-CreateSecondDDLTrigger.sql b/1-59059-720-6/Chapter13/16-CreateSecondDDLTrigger.sql new file mode 100644 index 0000000..e2b115e --- /dev/null +++ b/1-59059-720-6/Chapter13/16-CreateSecondDDLTrigger.sql @@ -0,0 +1,5 @@ +CREATE TRIGGER trgDBDump +ON DATABASE +FOR DDL_DATABASE_LEVEL_EVENTS +AS +SELECT EVENTDATA() \ No newline at end of file diff --git a/1-59059-720-6/Chapter13/17-Test-DDL-2.sql b/1-59059-720-6/Chapter13/17-Test-DDL-2.sql new file mode 100644 index 0000000..01b43ff --- /dev/null +++ b/1-59059-720-6/Chapter13/17-Test-DDL-2.sql @@ -0,0 +1,3 @@ +CREATE PROCEDURE Test1 +AS +SELECT 'Hello all' \ No newline at end of file diff --git a/1-59059-720-6/Chapter13/2-Test Trigger.sql b/1-59059-720-6/Chapter13/2-Test Trigger.sql new file mode 100644 index 0000000..5fc0d9c --- /dev/null +++ b/1-59059-720-6/Chapter13/2-Test Trigger.sql @@ -0,0 +1,9 @@ +SELECT ClearedBalance +FROM CustomerDetails.Customers +WHERE customerId=1 +INSERT INTO TransactionDetails.Transactions (CustomerId,TransactionType, +Amount,RelatedProductId, DateEntered) +VALUES (1,2,200,1,GETDATE()) +SELECT ClearedBalance +FROM CustomerDetails.Customers +WHERE customerId=1 \ No newline at end of file diff --git a/1-59059-720-6/Chapter13/3-Noncash Test.sql b/1-59059-720-6/Chapter13/3-Noncash Test.sql new file mode 100644 index 0000000..76bdff9 --- /dev/null +++ b/1-59059-720-6/Chapter13/3-Noncash Test.sql @@ -0,0 +1,9 @@ +SELECT ClearedBalance +FROM CustomerDetails.Customers +WHERE customerId=1 +INSERT INTO TransactionDetails.Transactions (CustomerId,TransactionType, +Amount,RelatedProductId, DateEntered) +VALUES (1,3,200,1,GETDATE()) +SELECT ClearedBalance +FROM CustomerDetails.Customers +WHERE customerId=1 \ No newline at end of file diff --git a/1-59059-720-6/Chapter13/4-InspectingTransactionDetails.sql b/1-59059-720-6/Chapter13/4-InspectingTransactionDetails.sql new file mode 100644 index 0000000..4fa663b --- /dev/null +++ b/1-59059-720-6/Chapter13/4-InspectingTransactionDetails.sql @@ -0,0 +1,3 @@ +SELECT * +FROM TransactionDetails.Transactions +WHERE CustomerId=1 \ No newline at end of file diff --git a/1-59059-720-6/Chapter13/5-AlterTrigger.sql b/1-59059-720-6/Chapter13/5-AlterTrigger.sql new file mode 100644 index 0000000..5c1e8c2 --- /dev/null +++ b/1-59059-720-6/Chapter13/5-AlterTrigger.sql @@ -0,0 +1,16 @@ +ALTER TRIGGER TransactionDetails.trgInsTransactions +ON TransactionDetails.Transactions +AFTER INSERT +AS +UPDATE CustomerDetails.Customers +SET ClearedBalance = ClearedBalance + +ISNULL((SELECT CASE WHEN CreditType = 0 +THEN i.Amount * -1 +ELSE i.Amount +END +FROM INSERTED i +JOIN TransactionDetails.TransactionTypes tt +ON tt.TransactionTypeId = i.TransactionType +WHERE AffectCashBalance = 1),0) +FROM CustomerDetails.Customers c +JOIN INSERTED i ON i.CustomerId = c.CustomerId \ No newline at end of file diff --git a/1-59059-720-6/Chapter13/6-NonCash-Retest.sql b/1-59059-720-6/Chapter13/6-NonCash-Retest.sql new file mode 100644 index 0000000..76bdff9 --- /dev/null +++ b/1-59059-720-6/Chapter13/6-NonCash-Retest.sql @@ -0,0 +1,9 @@ +SELECT ClearedBalance +FROM CustomerDetails.Customers +WHERE customerId=1 +INSERT INTO TransactionDetails.Transactions (CustomerId,TransactionType, +Amount,RelatedProductId, DateEntered) +VALUES (1,3,200,1,GETDATE()) +SELECT ClearedBalance +FROM CustomerDetails.Customers +WHERE customerId=1 \ No newline at end of file diff --git a/1-59059-720-6/Chapter13/7-UPDATE-Trigger.sql b/1-59059-720-6/Chapter13/7-UPDATE-Trigger.sql new file mode 100644 index 0000000..2f744c7 --- /dev/null +++ b/1-59059-720-6/Chapter13/7-UPDATE-Trigger.sql @@ -0,0 +1,28 @@ +ALTER TRIGGER TransactionDetails.trgInsTransactions +ON TransactionDetails.Transactions +AFTER INSERT,UPDATE +AS +UPDATE CustomerDetails.Customers +SET ClearedBalance = ClearedBalance - +ISNULL((SELECT CASE WHEN CreditType = 0 +THEN d.Amount * -1 +ELSE d.Amount +END +FROM DELETED d +JOIN TransactionDetails.TransactionTypes tt +ON tt.TransactionTypeId = d.TransactionType +WHERE AffectCashBalance = 1),0) +FROM CustomerDetails.Customers c +JOIN DELETED d ON d.CustomerId = c.CustomerId +UPDATE CustomerDetails.Customers +SET ClearedBalance = ClearedBalance + +ISNULL((SELECT CASE WHEN CreditType = 0 +THEN i.Amount * -1 +ELSE i.Amount +END +FROM INSERTED i +JOIN TransactionDetails.TransactionTypes tt +ON tt.TransactionTypeId = i.TransactionType +WHERE AffectCashBalance = 1),0) +FROM CustomerDetails.Customers c +JOIN INSERTED i ON i.CustomerId = c.CustomerId \ No newline at end of file diff --git a/1-59059-720-6/Chapter13/8-Test-UPDATE.sql b/1-59059-720-6/Chapter13/8-Test-UPDATE.sql new file mode 100644 index 0000000..85e0933 --- /dev/null +++ b/1-59059-720-6/Chapter13/8-Test-UPDATE.sql @@ -0,0 +1,15 @@ +SELECT * +FROM TransactionDetails.Transactions +WHERE CustomerId = 1 +SELECT ClearedBalance +FROM CustomerDetails.Customers +WHERE CustomerId = 1 +UPDATE TransactionDetails.Transactions +SET Amount = 100 +WHERE TransactionId = 5 +SELECT * +FROM TransactionDetails.Transactions +WHERE CustomerId = 1 +SELECT ClearedBalance +FROM CustomerDetails.Customers +WHERE CustomerId = 1 \ No newline at end of file diff --git a/1-59059-720-6/Chapter13/9-AlterUPDATE.sql b/1-59059-720-6/Chapter13/9-AlterUPDATE.sql new file mode 100644 index 0000000..911c9cc --- /dev/null +++ b/1-59059-720-6/Chapter13/9-AlterUPDATE.sql @@ -0,0 +1,34 @@ +ALTER TRIGGER TransactionDetails.trgInsTransactions +ON TransactionDetails.Transactions +AFTER INSERT,UPDATE +AS +IF UPDATE(Amount) OR Update(TransactionType) +BEGIN +UPDATE CustomerDetails.Customers +SET ClearedBalance = ClearedBalance - +ISNULL((SELECT CASE WHEN CreditType = 0 +THEN d.Amount * -1 +ELSE d.Amount +END +FROM DELETED d +JOIN TransactionDetails.TransactionTypes tt +ON tt.TransactionTypeId = d.TransactionType +WHERE AffectCashBalance = 1),0) +FROM CustomerDetails.Customers c +JOIN DELETED d ON d.CustomerId = c.CustomerId +UPDATE CustomerDetails.Customers +SET ClearedBalance = ClearedBalance + +ISNULL((SELECT CASE WHEN CreditType = 0 +THEN i.Amount * -1 +ELSE i.Amount +END +FROM INSERTED i +JOIN TransactionDetails.TransactionTypes tt +ON tt.TransactionTypeId = i.TransactionType +WHERE AffectCashBalance = 1),0) +FROM CustomerDetails.Customers c +JOIN INSERTED i ON i.CustomerId = c.CustomerId +RAISERROR ('We have completed an update',10,1) +END +ELSE +RAISERROR ('Updates have been skipped',10,1) \ No newline at end of file diff --git a/1-59059-720-6/Chapter14/1-ReportStructure.sql b/1-59059-720-6/Chapter14/1-ReportStructure.sql new file mode 100644 index 0000000..3e802b8 --- /dev/null +++ b/1-59059-720-6/Chapter14/1-ReportStructure.sql @@ -0,0 +1,8 @@ +USE ApressFinancial +GO +CREATE TABLE CustomerDetails.Title +(CustomerTitleId int primary key,TitleDescription varchar(16)) +GO +INSERT INTO CustomerDetails.Title VALUES (1,'Mr') +INSERT INTO CustomerDetails.Title VALUES (2,'Ms') +INSERT INTO CustomerDetails.Title VALUES (3,'Dr') \ No newline at end of file diff --git a/3354.pdf b/3354.pdf new file mode 100644 index 0000000..dd0afdb Binary files /dev/null and b/3354.pdf differ diff --git a/3434.pdf b/3434.pdf new file mode 100644 index 0000000..4894faa Binary files /dev/null and b/3434.pdf differ diff --git a/9781590597200.jpg b/9781590597200.jpg new file mode 100644 index 0000000..318c9de Binary files /dev/null and b/9781590597200.jpg differ diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..e77a775 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,27 @@ +Freeware License, some rights reserved + +Copyright (c) 2007 Robin Dewson + +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. + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..dcbac5a --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +#Apress Source Code + +This repository accompanies [*Beginning SQL Server 2005 Express for Developers*](http://www.apress.com/9781590597200) by Robin Dewson (Apress, 2007). + +![Cover image](9781590597200.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. diff --git a/contributing.md b/contributing.md new file mode 100644 index 0000000..f6005ad --- /dev/null +++ b/contributing.md @@ -0,0 +1,14 @@ +# Contributing to Apress Source Code + +Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. + +## How to Contribute + +1. Make sure you have a GitHub account. +2. Fork the repository for the relevant book. +3. Create a new branch on which to make your change, e.g. +`git checkout -b my_code_contribution` +4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. +5. Submit a pull request. + +Thank you for your contribution! \ No newline at end of file