Presentation Paul

  • 425 views
Uploaded on

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

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

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
425
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
3
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1.
    • SQL
    • Portfolio
    • Paul Glaeser
    • [email_address]
    • 610-327-3801
  • 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. Introduction This Presentation will illustrate key skills learned in the SQL Masters course such as SSIS, SSRS, SQLServer, T-SQL, Triggers and Views
  • 4. BlockFlix Movie Rental
    • Introduction:
    • 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.
    • Audience:
    • 3C executives to technical level employees.
    • Project Goals:
    • Develop the Central Database, Store database and Kiosk Database.
    • 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.
    • Develop the 4 core reports requested in SSRS
      • Most Rented Movie in past month, List of Customers and Membership type
      • Most active Customers in past month, Lost/Damaged movies with related Transactions
  • 5. Database Diagram
  • 6. Sample XML Input
  • 7. IMPORT XML FILE VIA SSIS AFTER CONTROL FLOW EXECUTION ADD MOVIE Demonstration Consisting of 3 linked data flows
  • 8. IMPORT XML FILE VIA SSIS AFTER MOVIE/TALENT EXECUTION ADD MOVIE Demonstration
  • 9. IMPORT XML FILE VIA SSIS AFTER MOVIE/TALENT EXECUTION ADD MOVIE Demonstration
  • 10. Most active customers in the past month
  • 11. Most rented movies in the past month
  • 12. Lost / damaged movies and their related transactions
  • 13. Customers and Membership Types
  • 14. P I G G Y - B A N K
    • Introduction:
    • Basic banking application with customers, accounts, transactions, interest , overdraft and statements
    • Audience:
    • Technical Management , Developers etc.
    • Project Goals:
    • For Customers Create and update the Customer information
    • Accounts – support Checking and Savings
    • Overdraft – account specific and general
    • Transactions – Deposits, Withdrawals/Purchases, Transfers
    • Interest Rates can be applied
    • Build view for ATM procedures and search engine queries
    • Protective Triggers
  • 15. Database Diagram
  • 16. Shred Xml into Database Table
    • /* Shred XML Data into appropriate tables */
    • -- Declare xml variable to handle xml input
    • DECLARE @doc xml
    • SET @doc = '<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
    • <Bank>
    • <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;>
    • <Accounts>
    • <Saving ID=&quot;100000&quot; Balance=&quot;548.25&quot; Active=&quot;1&quot;>
    • <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; />
    • <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; />
    • </Transactions>
    • </Saving>
    • </Accounts>
    • </Customer>
    • -- Declare variable to handle the xml document in the sp_xml_preparedocument
    • DECLARE @docHandle int
    • -- Put XML into memory
    • EXEC sp_xml_preparedocument @docHandle OUTPUT, @doc
    • --toggle identity on
    • Set identity_insert dbo.Transactions on
    • insert into dbo.Transactions (TransactionID, AccountID, TransactionTypeID, CustomerID, TransactionDate, TransactionAmount, NewBalance)
    • SELECT TransactionID, AccountID,
    • CASE
    • WHEN [Type] = 'Initial Deposit' THEN 1
    • WHEN [Type] = 'Deposit' THEN 2
    • WHEN [Type] = 'Withdrawal' THEN 3
    • WHEN [Type] = 'Transfer-Withdrawal' THEN 4
    • WHEN [Type] = 'Transfer-Deposit' THEN 5
    • WHEN [Type] = 'Checkcard Purchase' THEN 6
    • ELSE 7 -- [Type] = 'Check'
    • END,
    • CustomerID, TransactionDate, TransactionAmount,NewBalance
    • FROM OPENXML (@docHandle, '/Bank/Customer/Accounts/*/Transactions/Transaction', 1)
    • WITH ( TransactionID int '@ID'
    • , AccountID int '../../@ID'
    • , [Type] varchar(20) '@Type'
    • , CustomerID int '../../../../@ID'
    • , TransactionDate datetime '@Date'
    • , TransactionAmount money '@Amount'
    • , NewBalance money '@NewBalance' )
    • -- toggle identity back off
    • Set identity_insert dbo.Transactions off
    • -- remove xml information from memory
    • execute sp_xml_removedocument @docHandle
  • 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. 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. 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. --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. -- 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. 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
  • 23.  
  • 24.