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
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 "PreventChange" 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