Table of ContentsWhat is SetFocus 3RDBMS Concepts 4Writing Queries using SQL Server 2008 T SQL 7Implementing a SQL Server Database 13SSIS – SQL Server Integration Services 17SSRS – SQL Server Reporting Services 19
What is SetFocus?• The SetFocus SQL Master’s Program is an intensive, hands–on,project oriented program allowing knowledge and valuableexperience putting the SQL skill set to use in a simulated workenvironment.• Over 300 hours of in-depth hands on experience focused on SQL.• SetFocus projects are real world projects that are distributed justas I would receive in a position. I received project specifications andwas expected to identify best courses of action with deadlines setfor completion.
RDBMS Concepts Normalization and Database Objects
• Displaying data to a user in a familiar form alleviatesconfusion when describing how the system will perform• In order to get the best performance from a databasedata needs to be organized. This helps it to function at itsoptimal efficiency.• Keys/constraints are needed to keep the Data Integrityand assist in the separation of duties amongst databaseprogrammers and administrators. Normalization - What and Why We normalize data into a computerized and logical model by converting the data from its real world or physical model.
• Views – is a highly utilized tool in a SQL database and cancombine data from two or more tables. It also can utilize tables,functions, and even other views to produce results.• Functions – performs specific tasks by taking one or morearguments and return a result as a SQL object.•Stored Procedures – are complete T SQL programs that mayconsist of SQL statements and functions.• Triggers – is a special form of a stored procedure and isautomatically run when an event such as INSERT, UPDATE,DELETE occurs in a table. Similar to Schemabinding. Database Objects - WhyData retrieval needs to be as efficient as possible. Databases needs more tools to meet demands placed on them.
Writing Queries using MS SQL Server 2008 T-SQLSingle Table Queries, Views and Stored Procedures, Joins, Unions, and Advanced Queries
• The Cheap Books form displays available books below a certain price. The user enters 15 in the txtUnitPrice form field. ReturnISBN, title and publisher in order by title.Use JungleBooksDECLARE @txtUnitPrice intSET @txtUnitPrice = 15SELECT B.ISBN as ISBN, B.Title as Title, B.Publisher as PublisherFROM dbo.Books as BWHERE B.UnitPrice < @txtUnitPriceORDER BY b.Title• The Range of Customers form allows the user to view orders placed within a given range of customer IDs. The user enters 6 inthe txtIDStart form field and 15 in txtIDEnd form field. Display all columns of data in order by Customer ID. Alias the columnsin the result set as: ‘Order #’, ‘Cust ID’, ‘Order Date’.Use JungleBooksDeclare @txtlDStart as int , @txtlDEnd as intSet @txtlDStart = 6Set @txtlDEnd = 15Select OrderDate as [Order Date], CustomerID as [Cust ID], OrderID as [Order #]From dbo.OrdersWhere OrderID BETWEEN @txtlDStart AND @txtlDEndOrder by [Cust ID] Single Table Queries
•The Expired Cards page is a report on credit cards that have expired and credit cards that will expire soon. Output the customer ID, customer name and expiry date of the card for records with an expiry date prior to 30 days after the current date (today). Display the records by expiry date in descending order. Alias as: ‘ID’, ‘Name’, ‘Expires’. Use JungleBooks Declare @Expiration datetime Set @Expiration = DATEADD(day, 30, Getdate()) Select CustomerID as [ID], Name as [Name], Day(ExpiryDate)as [Expires] From dbo.Customers Where Day(ExpiryDate) < @Expiration Order by Expires DESCAdvanced Single Table Query with Results
• Write and execute a query on the Title, Item and Copy tables that returns the ISBN, copy_no, on_loan, title, translation, cover for rows inthe copy table with an ISBN of 500 or 1000. Only available books should be displayed and hardback copies should be listed first.Use libraryDeclare @onloan char(10) = NSelect Distinct I.isbn, C.copy_no, C.on_loan, T.title, I.translation,I.cover From dbo.title as T Join dbo.item as I on T.title_no = I.title_no Join dbo.copy as C on C.title_no = I.title_noWhere I.isbn IN(500, 1000) and C.on_loan = @onloanOrder by I.cover, C.on_loan• Retrieve the member’s full name and member_no from the Member table and the ISBN and log_date values fromthe Reservation table formember numbers 250, 341, and 1675. Order the results by member_no and log_date. You should show information for these members, evenif they have no books on reserve.Use librarySelect M.firstname + N + COALESCE(M.middleinitial, + N) + N + M.lastname as [Full Name], M.member_no, R.isbn, R.log_date From dbo.reservation as R Right Join dbo.member as M on R.member_no = M.member_noWhere M.member_no IN(250, 341, 1675)Order by M.member_no, R.log_date Advanced Queries Using Joins
Using joins and a UNION clause, write a query to retrieve a single list of members both adult and juvenile, who have reserved ISBN number 288. The list must include the ISBN, title, member_no and name (i.e.: Smith, John) of each member who has the reservation. Additionally, the list should indicate whether the member is an adult or a juvenile. Output the records by name.Use libraryDeclare @reserved# int = 288,@txtAdult As Char(10) = Adult‘, @txtJuvenile As Char(10) = JuvenileSelect R.isbn as ISBN, T.title as Title, R.member_no as [Member No.],@txtAdult as Member Type , M.lastname + N, + (M.middleinitial, + N) + N +M.firstname as FullNameFrom dbo.item as IJoin dbo.reservation as ROn I.isbn = R.isbnJoin dbo.member as MOn R.member_no = M.member_noJoin dbo.adult as AOn M.member_no = A.member_noJoin dbo.title as TOn I.title_no = T.title_noWhere R.isbn = @reserved#Union AllSelect R.isbn, T.title, M.member_no, @txtJuvenile, M.lastname + N, +M.middleinitial, + N) + N + M.firstname as FullNameFrom dbo.item as IJoin dbo.reservation as ROn I.isbn = R.isbnJoin dbo.member as MOn R.member_no = M.member_noJoin juvenile as JOn M.member_no = J.member_noJoin dbo.title as TOn I.title_no = T.title_noWhere R.isbn = @reserved#Order by FullNameJoin/Union Query with Results
Create a view in the TSQLFundamentals2008 database that returns the orderid, day of the week (Spelled Out), the name of the month (spelled Out), the day of the month, and the year based on the order date in the sales.orders tableUSE TSQLFundamentals2008Create VIEW [dbo].[MyDateView]ASSELECT orderid, DATENAME(dw, orderdate) AS [Day Name], DATENAME(MM, orderdate) AS [MonthName], DAY(orderdate) AS [Day Of Month], YEAR(orderdate) AS [Order Year]FROM Sales.Orders;Create a stored procedure in the TSQLFundamentals2008 database that returns the order ID, the orderdate, the ship country. The employee full name, and the company name. The ship country should be a parameter and the result set should be sorted by order date from most recent to oldest. pagef format o ed to fitUSE [TSQLFundamentals2008]GO SET ANSI_NULLS ON GO EditSET QUOTED_IDENTIFIER ON GOCREATE PROCEDURE [dbo].[usp_OrdersByCountry] @ShipCountry as VarChar(25)= USAASBEGINSET NOCOUNT ON;Select O.orderid, O.orderdate, O.shipcountry, E.firstname + N + E.lastname As Employee, C.companynameFrom Sales.Orders As OInner Join Sales.Customers As C On C.custid = O.custidInner Join HR.Employees As E On E.empid = O.empidWhere O.shipcountry = @ShipCountryOrder By O.orderdate DescEND View and Stored Procedure
Implementing a Microsoft SQL Server Database Piggy Bank Piggy Bank is a database produced by SetFocus students ranging from the CREATE DATABASE script to all stored procedures, Functions, Views, and Triggers required sothat banking transactions may be done in a clean efficientmanner. Deposits, withdrawals, bank statements, etc. arejust a few of the requirements considered in this project.
CREATE proc [dbo].[sp_addnewcustomer] @CustomerFirstName nvarchar(20) = null , @CustomerLastName nvarchar(30) = null , @CustomerMiddleInitial nvarchar(1) = null , @Street nvarchar(50) = null , @City nvarchar(20) = null , @State char(2) = null , @ZipCode char(10) = null , @Email nvarchar(30) = null , @HomePhone char(12) = null , @WorkPhone char(12) = null , @CellPhone char(12) = null , @CustomerID int OUTPUTasBEGIN TRYIf @CustomerLastName is null Begin RAISERROR(first name cant be null,11,1) EndBEGIN TRANINSERT INTO [PiggyBank].[dbo].[Customer] ([CustomerFirstName] ,[CustomerLastName] ,[CustomerMiddleInitial] ,[Street] ,[City],[State] ,[ZipCode] ,[Email] ,[HomePhone])VALUES ( @CustomerFirstName, @CustomerLastName, @CustomerMiddleInitial, @Street, @City, @State, @ZipCode, @Email, @HomePhone)SET @CustomerID = SCOPE_IDENTITY()Return @CustomerID Catch ollback/ issing Code (R t of N O TE : M fit forma ion) - Edited to Transact page Add Customer Stored Procedure
CREATE PROCEDURE [dbo].[sp_Deposit] @AccountID int = null ,@CustomerID int = null ,@TransactionAmount money = null ,@TransactionID int OUTPUTASBEGIN TRYDECLARE @NewBalance money, @CurrentBalance moneySET @NewBalance = (SELECT CurrentBalance FROM dbo.Account WHERE AccountID = @AccountID) + @TransactionAmount BEGIN TRAN IF NOT EXISTS (SELECT AccountID 01 FROM dbo.Account ID 1000 to Account of $2000 WHERE AccountID = @AccountID) Deposit ber 2) BEGIN RAISERROR (Account does not exist,3,1) (row num END IF NOT EXISTS (SELECT CustomerID FROM dbo.Customer WHERE CustomerID = @CustomerID) BEGIN RAISERROR(Customer does not exist,3,1) ENDBEGININSERT INTO [PiggyBank].[dbo].[Transactions] ([AccountID],[TransactionTypeID],[CustomerID],[TransactionDate],[TransactionAmount],[NewBalance]) VALUES (@AccountID,2,@CustomerID,GETDATE(),@TransactionAmount,@NewBalance) UPDATE dbo.Account SET CurrentBalance = @NewBalance WHERE AccountID = @AccountID SELECT @TransactionID = SCOPE_IDENTITY() Deposit into Bank Account
CREATE TRIGGER trgDropTablesON DATABASE FOR DROP_TABLEASBEGIN TRANRAISERROR(TABLES ARE SET SO THAT THEY MAY NOT BE DROPPED FROM SYSTEM, 5,1)ROLLBACKGOCREATE TRIGGER trgAlterTablesON DATABASE FOR ALTER_TABLEASBEGIN TRANRAISERROR(TABLES ARE SET SO THAT THEY MAY NOT BE ALTERED, 5,1)ROLLBACKGOCREATE TRIGGER trgDropViewsON DATABASE FOR DROP_VIEWASBEGIN TRANRAISERROR(VIEWS ARE SET SO THAT THEY MAY NOT BE DROPPED FROM SYSTEM, 5,1)ROLLBACKGOCREATE TRIGGER dbo.trgNODELETTRANSACTIONON dbo.TransactionsAFTER DELETEASBEGIN TRANRAISERROR(TRANSACTIONS CANNOT BE DELETED, 5,1)ROLLBACKGO Triggers in the Database
SSIS – SQL Server Integration ServicesExtract, Transfer & Load, Variables, Aggregation and Advanced Package Integration