Presentation Paul

550 views

Published on

Sample code from work done in SQL Masters course completed at SetFocus

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
550
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Presentation Paul

  1. 1. <ul><li>SQL </li></ul><ul><li>Portfolio </li></ul><ul><li>Paul Glaeser </li></ul><ul><li>[email_address] </li></ul><ul><li>610-327-3801 </li></ul>
  2. 2. Table of Contents Introduction – 3 BlockFlix Movie Rental -4 BlockFlix SSIS – 6 BlockFlix SSRS - 10 Piggy Bank - 14 Shred XML – 16 View - 17 Triggers – 18 Stored Procedure -19
  3. 3. Introduction This Presentation will illustrate key skills learned in the SQL Masters course such as SSIS, SSRS, SQLServer, T-SQL, Triggers and Views
  4. 4. BlockFlix Movie Rental <ul><li>Introduction: </li></ul><ul><li>BlockFlix is a new and upcoming Movie Rental company. They have opened a few stores nationwide and are in process of creating a centralized database for tracking inventory, sales, customers and memberships. </li></ul><ul><li>Audience: </li></ul><ul><li>3C executives to technical level employees. </li></ul><ul><li>Project Goals: </li></ul><ul><li>Develop the Central Database, Store database and Kiosk Database. </li></ul><ul><li>Develop the xml file format for the vendor to use to submit movies as well as the SSIS package to read and load the data in the file. </li></ul><ul><li>Develop the 4 core reports requested in SSRS </li></ul><ul><ul><li>Most Rented Movie in past month, List of Customers and Membership type </li></ul></ul><ul><ul><li>Most active Customers in past month, Lost/Damaged movies with related Transactions </li></ul></ul>
  5. 5. Database Diagram
  6. 6. Sample XML Input
  7. 7. IMPORT XML FILE VIA SSIS AFTER CONTROL FLOW EXECUTION ADD MOVIE Demonstration Consisting of 3 linked data flows
  8. 8. IMPORT XML FILE VIA SSIS AFTER MOVIE/TALENT EXECUTION ADD MOVIE Demonstration
  9. 9. IMPORT XML FILE VIA SSIS AFTER MOVIE/TALENT EXECUTION ADD MOVIE Demonstration
  10. 10. Most active customers in the past month
  11. 11. Most rented movies in the past month
  12. 12. Lost / damaged movies and their related transactions
  13. 13. Customers and Membership Types
  14. 14. P I G G Y - B A N K <ul><li>Introduction: </li></ul><ul><li>Basic banking application with customers, accounts, transactions, interest , overdraft and statements </li></ul><ul><li>Audience: </li></ul><ul><li>Technical Management , Developers etc. </li></ul><ul><li>Project Goals: </li></ul><ul><li>For Customers Create and update the Customer information </li></ul><ul><li>Accounts – support Checking and Savings </li></ul><ul><li>Overdraft – account specific and general </li></ul><ul><li>Transactions – Deposits, Withdrawals/Purchases, Transfers </li></ul><ul><li>Interest Rates can be applied </li></ul><ul><li>Build view for ATM procedures and search engine queries </li></ul><ul><li>Protective Triggers </li></ul>
  15. 15. Database Diagram
  16. 16. Shred Xml into Database Table <ul><li>/* Shred XML Data into appropriate tables */ </li></ul><ul><li>-- Declare xml variable to handle xml input </li></ul><ul><li>DECLARE @doc xml </li></ul><ul><li>SET @doc = '<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> </li></ul><ul><li><Bank> </li></ul><ul><li><Customer ID=&quot;1&quot; FirstName=&quot;John&quot; LastName=&quot;Doe&quot; Street=&quot;123, Fake Street&quot; City=&quot;Anytown&quot; State=&quot;NY&quot; Zip=&quot;11280&quot; HomePhone=&quot;555-123-4567&quot; WorkPhone=&quot;555-456-7890&quot; MobilePhone=&quot;555-111-2345&quot; EMail=&quot;jDoe@someweb.com&quot;> </li></ul><ul><li><Accounts> </li></ul><ul><li><Saving ID=&quot;100000&quot; Balance=&quot;548.25&quot; Active=&quot;1&quot;> </li></ul><ul><li><Transactions> <Transaction ID=&quot;1&quot; Type=&quot;Initial Deposit&quot; Date=&quot;4/01/2009&quot; Amount=&quot;500.00&quot; NewBalance=&quot;500.00&quot; /> </li></ul><ul><li><Transaction ID=&quot;5&quot; Type=&quot;Deposit&quot; Date=&quot;4/01/2009&quot; Amount=&quot;50.00&quot; NewBalance=&quot;550.00&quot; /> </li></ul><ul><li></Transactions> </li></ul><ul><li></Saving> </li></ul><ul><li></Accounts> </li></ul><ul><li></Customer> </li></ul><ul><li>-- Declare variable to handle the xml document in the sp_xml_preparedocument </li></ul><ul><li>DECLARE @docHandle int </li></ul><ul><li>-- Put XML into memory </li></ul><ul><li>EXEC sp_xml_preparedocument @docHandle OUTPUT, @doc </li></ul><ul><li>--toggle identity on </li></ul><ul><li>Set identity_insert dbo.Transactions on </li></ul><ul><li>insert into dbo.Transactions (TransactionID, AccountID, TransactionTypeID, CustomerID, TransactionDate, TransactionAmount, NewBalance) </li></ul><ul><li>SELECT TransactionID, AccountID, </li></ul><ul><li> CASE </li></ul><ul><li>WHEN [Type] = 'Initial Deposit' THEN 1 </li></ul><ul><li>WHEN [Type] = 'Deposit' THEN 2 </li></ul><ul><li>WHEN [Type] = 'Withdrawal' THEN 3 </li></ul><ul><li>WHEN [Type] = 'Transfer-Withdrawal' THEN 4 </li></ul><ul><li>WHEN [Type] = 'Transfer-Deposit' THEN 5 </li></ul><ul><li>WHEN [Type] = 'Checkcard Purchase' THEN 6 </li></ul><ul><li>ELSE 7 -- [Type] = 'Check' </li></ul><ul><li>END, </li></ul><ul><li>CustomerID, TransactionDate, TransactionAmount,NewBalance </li></ul><ul><li>FROM OPENXML (@docHandle, '/Bank/Customer/Accounts/*/Transactions/Transaction', 1) </li></ul><ul><li>WITH ( TransactionID int '@ID' </li></ul><ul><li> , AccountID int '../../@ID' </li></ul><ul><li> , [Type] varchar(20) '@Type' </li></ul><ul><li> , CustomerID int '../../../../@ID' </li></ul><ul><li> , TransactionDate datetime '@Date' </li></ul><ul><li> , TransactionAmount money '@Amount' </li></ul><ul><li> , NewBalance money '@NewBalance' ) </li></ul><ul><li>-- toggle identity back off </li></ul><ul><li>Set identity_insert dbo.Transactions off </li></ul><ul><li>-- remove xml information from memory </li></ul><ul><li>execute sp_xml_removedocument @docHandle </li></ul>
  17. 17. Create View for Statements CREATE VIEW [dbo].[AccountStatement] WITH SCHEMABINDING AS SELECT dbo.Account.CurrentBalance, dbo.Account.AccountID, dbo.TransactionType.TransactionTypeName, dbo.AccountStatus.AccountStatus, dbo.AccountType.AccountTypeName, dbo.AccountType.InterestRate, dbo.Customer.CustomerID, dbo.Customer.CustomerFirstName, dbo.Customer.CustomerLastName, dbo.Customer.CustomerMiddleInitial, dbo.Customer.Street, dbo.Customer.City, dbo.Customer.State, dbo.Customer.ZipCode, dbo.Customer.HomePhone, dbo.Transactions.TransactionDate, dbo.Transactions.TransactionAmount,dbo.Transactions.NewBalance FROM dbo.Account INNER JOIN dbo.AccountStatus ON dbo.Account.AccountStatusID = dbo.AccountStatus.AccountStatusID INNER JOIN dbo.AccountType ON dbo.Account.AccountTypeID = dbo.AccountType.AccountTypeID INNER JOIN dbo.CustomerAccount ON dbo.Account.AccountID = dbo.CustomerAccount.AccountID INNER JOIN dbo.Customer ON dbo.CustomerAccount.CustomerID = dbo.Customer.CustomerID INNER JOIN dbo.Transactions ON dbo.Account.AccountID = dbo.Transactions.AccountID AND dbo.Customer.CustomerID = dbo.Transactions.CustomerID INNER JOIN dbo.TransactionType ON dbo.Transactions.TransactionTypeID = dbo.TransactionType.TransactionTypeID
  18. 18. Triggers to protect Data CREATE TRIGGER PreventChange ON DATABASE FOR DROP_TABLE, ALTER_TABLE AS PRINT 'You must disable Trigger &quot;PreventChange&quot; to drop or alter tables!' ROLLBACK CREATE TRIGGER DelTrans ON [dbo].[Transactions] INSTEAD OF DELETE AS BEGIN Raiserror('Deletes of Transactions not allowed', 13,1); ROLLBACK TRAN; END Create Trigger DelAccount on [dbo].[Account] INSTEAD OF DELETE AS BEGIN Raiserror('Deletes of Accounts not allowed', 13,1); ROLLBACK TRAN; END CREATE TRIGGER UpdTran ON [dbo].[Transactions] INSTEAD OF UPDATE AS BEGIN Raiserror('Updates of Transactions not allowed', 14,1); ROLLBACK TRAN; END
  19. 19. Stored Procedures with T-SQL CREATE PROCEDURE [dbo].[Deposit] -- Add the parameters for the stored procedure here @CustomerID int = NULL, @AccountID int = NULL, @TransactionAmount money = NULL AS BEGIN TRY BEGIN TRAN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Check for null values if @AccountID is null BEGIN raiserror('Account ID must be provided', 12, 1); END if @CustomerID is null BEGIN raiserror('Customer ID must be provided', 12, 2); END If @TransactionAmount Is Null BEGIN raiserror('Transaction Amount must be provided', 12, 3); END
  20. 20. --check valid Custid and accountid if @AccountID != (SELECT AccountID FROM Account WHERE AccountID = @AccountID) BEGIN raiserror('Account ID is invalid', 12, 4); END if @CustomerID != (SELECT CustomerID FROM Customer WHERE CustomerID = @CustomerID) BEGIN raiserror('Customer ID is invalid', 12, 5); END --Fetch current balance to calculate new balance DECLARE @CURRBAL money = (SELECT CurrentBalance FROM Account WHERE AccountID = @AccountID), @NewBalance money = NULL, @TransactionTypeID tinyint = 2, @AcctStatID tinyint = 1 SET @NewBalance = @CURRBAL + @TransactionAmount;
  21. 21. -- Insert statements for procedure here -- check account active if @AcctStatID != (SELECT AccountStatusID FROM Account WHERE AccountID = @AccountID) BEGIN Raiserror('Can not Deposit for Inactive Account', 12, 6); END BEGIN INSERT INTO [PIGGYBank].[dbo].[Transactions] ([AccountID] ,[TransactionTypeID] ,[CustomerID] ,[TransactionDate] ,[TransactionAmount] ,[NewBalance]) VALUES (@AccountID ,@TransactionTypeID ,@CustomerID ,CURRENT_TIMESTAMP ,@TransactionAmount ,@NewBalance); UPDATE [PIGGYBank].[dbo].[Account] SET [CurrentBalance] = @NewBalance WHERE AccountID = @AccountID; END COMMIT TRAN END TRY
  22. 22. BEGIN CATCH if @@TRANCOUNT > 0 ROLLBACK TRAN DECLARE @ErrorMessage NVARCHAR(4000); DECLARE @ErrorSeverity INT; DECLARE @ErrorState INT; --populate vars with error info SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE(); -- rethrow pass to front end or another proc RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState); END CATCH

×