Susan Whitfield<br />MSSQL Server 2008 Portfolio<br />Email: lilredlokita@gmail.com<br />
Introduction<br />Good day… <br />My name is Susan Whitfield and I set out on a journey to augment my IT skills. This led ...
Table of Contents<br />Introduction		.	.	.	.	.	.	.	.	.	.	.	.	2<br />Piggy Bank 1 Project:.	.	.	.	.	.	.	.	.	.	.	4<br />Desi...
Project Specifications<br />ER Diagram<br />Diagram 0<br />Piggy Bank Project<br />
Project Specifications<br />Create a database for Piggy Unlimited. We were given a list of columns and were asked to creat...
DB Creation<br />Diagram 0<br />Queries<br />T-SQL Project<br />
Database Creation<br />USE [JungleBooks]<br />GO<br />/****** Object:  Table [dbo].[Orders]    Script Date: 10/07/2009 00:...
GO<br />SET ANSI_PADDING OFF<br />GO<br />/****** Object:  Table [dbo].[Customers]    Script Date: 10/07/2009 00:01:16 ***...
	[ISBN] ASC<br />)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLO...
Problem 1<br />The Cheap Books form displays available books below a certain price.  The user enters 15 in the txtUnitPric...
Problem 3<br />The Range of Customers form allows the user to view orders placed within a given range of customer IDs.  Th...
Problem 5<br />The Available Books page is a report listing the publishers and the books they offer.  Output a list of pub...
Problem 7<br />The Author and Books page is a report which lists the author name, book title and ISBN.  The list is to be ...
Problem 9<br />The Search For Customers by Credit Card page also allows the user to search for Customers by the last four ...
Problem 11<br />Write and execute a query on the Title, Item and Copy tables that returns the ISBN, copy_no, on_loan, titl...
Problem 13<br />Using joins and a UNION clause, write a query to retrieve a single list of members both adult and juvenile...
Problem 14<br />Write the above statement again using a CASE statement.  You cannot JOIN to the Adult or Juvenile tables. ...
Problem 15<br />Write and execute a query that returns the member_no, full name, out_date, due_date, and title columns fro...
Problem 16<br />Write and execute a query that returns the member number, member name and number of past due loans of each...
Problem 18<br />Write and execute a query on the Reservation table that returns the ISBN, title and Total.  The Total colu...
Diagram 0<br />Views<br />Triggers<br />Stored procedures (code snippets of a few sp’s)<br />XML shred snippet<br />Piggy ...
Views<br />/****** Object:  View [dbo].[v_CurrentMonthStatement]    Script Date: 10/17/2009 00:43:55 ******/<br />SET ANSI...
/****** Object:  View [dbo].[v_GetHistoricalStatement]    Script Date: 10/17/2009 00:43:55 ******/<br />SET ANSI_NULLS ON<...
/****** Object:  View [dbo].[v_ListCustomerAccounts]    Script Date: 10/17/2009 00:43:55 ******/<br />SET ANSI_NULLS ON<br...
Triggers<br />USE [PiggyBankWk4]<br />GO<br />/****** Object:  DdlTrigger [NoUnwantedChanges]    Script Date: 10/17/2009 0...
SP’s<br />Add Account Stored Proc Code Snippet<br />
Deposit Transaction Stored Proc Code Snippet<br />
Deposit Transfer Stored Proc Code Snippet<br />
Insert Customer Stored Proc Code Snippet<br />
Close Account Stored Proc Code Snippet<br />
Reopen Account Stored Proc Code Snippet<br />
Withdrawal Transfer Stored Proc Code Snippet<br />
XML shred<br />
Backup/Restore Disaster Recovery Mini-Simulation<br />Snapshot Creation and Usage<br />Working With Schemas<br />Resource ...
Backup & Restore: Disaster Recovery Mini-Simulation<br />--primary file group backup<br />BACKUP DATABASE [AdventureWorks2...
Snapshot Creation and Usage<br />/* Author:	Susan Whitfield<br />Date:		Friday, November 6, 2009<br />Purpose:	To show dat...
Working With Schemas<br />/* Author:	Susan Whitfield<br />Date:		Friday, November 6, 2009<br />Purpose:	Schema work: Worki...
Resource Governor Configuration & Usage<br />-- Configure Resource Governor.<br />BEGIN TRAN<br />USE Master<br />GO<br />...
Ownership Chaining<br />USE master GO<br />CREATE LOGIN Joe WITH PASSWORD = &apos;P@$$w0rd&apos;<br />CREATE LOGIN Tom WIT...
Adventure Works Repair<br />Diagram 0<br />High Effort Jobs Report<br />Invoiced Job Summary<br />Customer Billing Summary...
High Effort Jobs Report<br />--requirements<br />/*The final report (&quot;High Effort Jobs&quot;) is intended to<br /> sh...
Invoiced Job Summary<br />--requirements<br />/*<br />The first report (&quot;Invoiced Job Summary&quot;) represents <br /...
Customer Billing Summary Requirements<br />--requirements<br />/*<br />The second report (&quot;Customer Billing Summary&q...
Customer Billing Summary<br />SELECT CustomerName, <br />(SELECT Count(JobID) FROM Job WHERE StatusID&lt;&gt; 6) as &apos;...
Source Files<br />Table Creation<br />Triggers<br />Stored Procedure<br />User Defined Function<br />SSIS Packages<br />Mi...
3 Master Files<br />ProductMaster.CSV<br />ShipMethodMaster.CSV<br />VendorMaster.CSV<br />Four Purchase Order Transaction...
Table Creation<br />Product Table<br />USE [MiniAdventureWorksDB]<br />GO<br />/****** Object:  Table [dbo].[Product]    S...
Vendor Table<br />USE [MiniAdventureWorksDB]<br />GO<br />/****** Object:  Table [dbo].[Vendor]    Script Date: 11/27/2009...
ShipMethod Table<br />USE [MiniAdventureWorksDB]<br />GO<br />/****** Object:  Table [dbo].[ShipMethod]    Script Date: 11...
PurchaseOrderHeader Table<br />USE [MiniAdventureWorksDB]<br />GO<br />/****** Object:  Table [dbo].[PurchaseOrderHeader] ...
PurchaseOrderDetail Table<br />USE [MiniAdventureWorksDB]<br />GO<br />/****** Object:  Table [dbo].[PurchaseOrderDetail] ...
Triggers<br />Trigger Creation<br />USE MiniAdventureWorksDB<br />GO<br />CREATE TRIGGER UpdateProduct<br />ON dbo.Product...
Trigger Creation<br />USE MiniAdventureWorksDB<br />GO<br />CREATE TRIGGER UpdatePurchaseOrderHeader<br />ON dbo.PurchaseO...
Trigger Creation<br />USE MiniAdventureWorksDB<br />GO<br />CREATE TRIGGER UpdateVendor<br />ON dbo.Vendor<br />AFTER UPDA...
Stored Procedure<br />Stored Procedure Creation<br />USE MiniAdventureWorksDB<br />GO<br />CREATE procedure InsertOrderHea...
User Defined Function<br />User Defined Function<br />USE [MiniAdventureWorksDB]<br />GO<br />/****** Object:  UserDefined...
SSIS Packages<br />Create Database<br />
Import  Products<br />
Import  Products<br />
Import  Products<br />
Import  Vendors<br />
Import  Vendors<br />
Import  ShipMethod<br />
Import  ShipMethod<br />
Import  Orders<br />
Import  Orders<br />
Product Price Increase<br />
Product Price Increase<br />
Stored Procedures<br />XML Source Files<br />SSIS Packages<br />BlockFlix<br />
Stored Procedures<br />Insert Movie<br />USE [Blockflix]<br />GO<br />/****** Object:  StoredProcedure [dbo].[usp_InsertMo...
Insert Movie Into Inventory<br />USE [Blockflix]<br />GO<br />/****** Object:  StoredProcedure [dbo].[usp_InsertMovieIntoI...
Insert Into Cast<br />USE [Blockflix] GO<br />/****** Object:  StoredProcedure [dbo].[usp_InsertIntoCast]    Script Date: ...
Insert Products<br />USE [Blockflix]<br />GO<br />/****** Object:  StoredProcedure [dbo].[usp_InsertProducts]    Script Da...
Customer Queue Part 1<br />
Customer Queue Part 2<br />
Customer Queue Results<br />
XML Source File For Movies<br />
XML Source File For Products<br />
SSIS Packages<br />Import Movies<br />
Import Movies<br />
Import Movies<br />
Import Movies<br />
Import Products<br />
Import Products<br />
Import Store Info<br />
Import Kiosk Info<br />
Import Kiosk Info<br />
Database Design Diagram<br />Instructor Results Report Examples (SSRS)<br />Evaluation Application<br />
SQL Server 2008 Portfolio
SQL Server 2008 Portfolio
SQL Server 2008 Portfolio
Upcoming SlideShare
Loading in …5
×

SQL Server 2008 Portfolio

2,484 views

Published on

Portfolio of some of the work completed in the SQL Server 2008 course at SetFocus.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,484
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

SQL Server 2008 Portfolio

  1. 1. Susan Whitfield<br />MSSQL Server 2008 Portfolio<br />Email: lilredlokita@gmail.com<br />
  2. 2. Introduction<br />Good day… <br />My name is Susan Whitfield and I set out on a journey to augment my IT skills. This led me to SetFocus, LLC. and their SQL Masters Program. The goal of this program is to provide trainees with the skills necessary to become SQL Server 2008 experts, and to utilize the knowledge learned, in a real world setting within a short amount of time. SetFocus is a certified Microsoft Gold partner and uses Microsoft certified courses to accomplish this goal. <br />The course was a very intense 13 weeks long, which showed the database administration side as well as the development side of SQL Server 2008. Included in this presentation are examples from projects completed during the 13 weeks. We utilized; SSMS (SQL Server Management Studio), SSIS (SQL Server Integration Studio), SSRS (SQL Server Report Studio), Visio, and BIDS (Business Intelligence Development Studio), in order to develop T-SQL code, create: packages, reports, database diagrams, as well as administer: security, mirroring, clustering, replication, snapshots, isolation levels, jobs, and other administration tasks.<br />Enjoy the presentation and if you have any questions please feel free to contact me at the email included.<br /> Susan Whitfield<br />
  3. 3. Table of Contents<br />Introduction . . . . . . . . . . . . 2<br />Piggy Bank 1 Project:. . . . . . . . . . . 4<br />Designing database<br />TSQL Assignment: . . . . . . . . . . . 8<br />Designing database and utilizing T-SQL skills<br />Piggy Bank 2 Project:. . . . . . . . . . . 24<br />Designing database, creating: views, triggers, and robust stored procedures, shredding XML<br />DBA Practical’s: . . . . . . . . . . . . 38<br />Adventure Works Repair Project: . . . . . . . . . 44<br />T-SQL practice<br />Mini Adventure Works Project: . . . . . . . . . 50<br />Table creation, triggers, stored procedures, user defined function, and SSIS packages<br />BlockFlix Project (Final Project: showing portions I completed): . . .74<br />Stored procedures , XML Source Files, SSIS packages<br />Evaluation Application (Mini-Final Project: showing portions I completed): . 93<br />Instructor Results Report examples<br />
  4. 4. Project Specifications<br />ER Diagram<br />Diagram 0<br />Piggy Bank Project<br />
  5. 5. Project Specifications<br />Create a database for Piggy Unlimited. We were given a list of columns and were asked to create tables, establish relationships via primary key constraints and foreign key constraints and choose most appropriate data types. After database/table creation we were asked to backup the database and send that backup to the instructor. We were required to denormalize to 3rd NF, and to justify use of denormalization. <br />
  6. 6.
  7. 7.
  8. 8. DB Creation<br />Diagram 0<br />Queries<br />T-SQL Project<br />
  9. 9. Database Creation<br />USE [JungleBooks]<br />GO<br />/****** Object: Table [dbo].[Orders] Script Date: 10/07/2009 00:01:16 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />CREATE TABLE [dbo].[Orders](<br /> [OrderID] [int] NOT NULL,<br /> [CustomerID] [int] NOT NULL,<br /> [OrderDate] [smalldatetime] NOT NULL,<br /> CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED <br />(<br /> [OrderID] ASC<br />)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]<br />) ON [PRIMARY]<br />GO<br />/****** Object: Table [dbo].[OrderItems] Script Date: 10/07/2009 00:01:16 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />SET ANSI_PADDING ON<br />GO<br />CREATE TABLE [dbo].[OrderItems](<br /> [OrderItemID] [int] NOT NULL,<br /> [OrderID] [int] NOT NULL,<br /> [ISBN] [char](20) NOT NULL,<br /> [QuantityOrdered] [int] NOT NULL,<br /> [QuantityDispatched] [int] NOT NULL,<br /> [UnitPrice] [money] NOT NULL,<br /> CONSTRAINT [PK_OrderItems] PRIMARY KEY CLUSTERED <br />(<br /> [OrderItemID] ASC<br />)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]<br />) ON [PRIMARY]<br />
  10. 10. GO<br />SET ANSI_PADDING OFF<br />GO<br />/****** Object: Table [dbo].[Customers] Script Date: 10/07/2009 00:01:16 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />SET ANSI_PADDING ON<br />GO<br />CREATE TABLE [dbo].[Customers](<br /> [CustomerID] [int] NOT NULL,<br /> [Name] [varchar](80) NOT NULL,<br /> [Address] [varchar](255) NOT NULL,<br /> [CardType] [char](20) NOT NULL,<br /> [CardNumber] [char](20) NOT NULL,<br /> [ExpiryDate] [smalldatetime] NOT NULL,<br /> CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED <br />(<br /> [CustomerID] ASC<br />)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]<br />) ON [PRIMARY]<br />GO<br />SET ANSI_PADDING OFF<br />GO<br />/****** Object: Table [dbo].[Books] Script Date: 10/07/2009 00:01:16 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />SET ANSI_PADDING ON<br />GO<br />CREATE TABLE [dbo].[Books](<br /> [ISBN] [char](20) NOT NULL,<br /> [Publisher] [varchar](50) NOT NULL,<br /> [Title] [varchar](128) NOT NULL,<br /> [UnitPrice] [money] NOT NULL,<br /> [Abstract] [varchar](255) NOT NULL,<br /> [Pages] [smallint] NOT NULL,<br /> [Published] [smalldatetime] NOT NULL,<br /> [Stock] [int] NOT NULL,<br /> CONSTRAINT [PK_Books] PRIMARY KEY CLUSTERED <br />(<br />
  11. 11. [ISBN] ASC<br />)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]<br />) ON [PRIMARY]<br />GO<br />SET ANSI_PADDING OFF<br />GO<br />/****** Object: Table [dbo].[BookAuthors] Script Date: 10/07/2009 00:01:16 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />SET ANSI_PADDING ON<br />GO<br />CREATE TABLE [dbo].[BookAuthors](<br /> [BookAuthorID] [int] NOT NULL,<br /> [ISBN] [char](20) NOT NULL,<br /> [AuthorID] [int] NOT NULL,<br /> CONSTRAINT [PK_BookAuthors] PRIMARY KEY CLUSTERED <br />(<br /> [BookAuthorID] ASC<br />)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]<br />) ON [PRIMARY]<br />GO<br />SET ANSI_PADDING OFF<br />GO<br />/****** Object: Table [dbo].[Authors] Script Date: 10/07/2009 00:01:16 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />SET ANSI_PADDING ON<br />GO<br />CREATE TABLE [dbo].[Authors](<br /> [AuthorID] [int] NOT NULL,<br /> [Name] [varchar](120) NOT NULL,<br /> CONSTRAINT [PK_Authors] PRIMARY KEY CLUSTERED <br />(<br /> [AuthorID] ASC<br />)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]<br />) ON [PRIMARY]<br />GO<br />SET ANSI_PADDING OFF<br />GO<br />
  12. 12.
  13. 13. Problem 1<br />The Cheap Books form displays available books below a certain price. The user enters 15 in the txtUnitPrice form field. Return ISBN, title and publisher in order by title.<br />Statement1<br />Write a query that displays ISBN, title, publisher having books that are priced less than $15, ordered by the title.<br />T-SQL1<br />SELECT ISBN, Title, Publisher<br />FROM Books<br />WHEREUnitPrice&lt; 15<br />ORDERBY Title<br />Problem 2<br />The Search For Authors form displays a list of author names which contains a string which is input by the user. The user enters ‘pet’ in the txtName form field. Display author names in alphabetical order. Use the alias ‘Search Results’ for the author name data.<br />Statement2<br />Write a query that displays author names having the name ‘pet’, displayed in alphabetical order. Use heading: ‘Search Results’.<br />T-SQL2<br />SELECT Name AS&apos;Search Results&apos;<br />FROM Authors<br />WHERE Name like&apos;%pet%&apos;<br />ORDERBY Name<br />
  14. 14. Problem 3<br />The Range of Customers form allows the user to view orders placed within a given range of customer IDs. The user enters 6 in the txtIDStart form field and 15 in txtIDEnd form field. Display all columns of data in order by Customer ID. Alias the columns in the result set as: ‘Order #’, ‘Cust ID’, ‘Order Date’.<br />Statement3<br />Write a query to display order number, customer id, and order date ordered by customer id having customer ids in the range between 6 and 15. Use headings: ‘Order#’, ‘Cust ID’, and ‘Order Date’.<br />T-SQL3<br />SELECTOrderID&apos;Order#&apos;,CustomerID&apos;Cust ID&apos;,OrderDate&apos;Order Date&apos;<br />FROM Orders<br />WHERECustomerIDbetween 6 and 15<br />ORDERBYCustomerID<br />Problem 4<br />The Large Orders page is a report displaying the largest number of items sold per order. Display the Order ID, Customer ID and Name along with the total number of items they ordered. Display the record with the largest quantity first. Alias as: “Order ID’, ‘Cust ID’, ‘Customer’ and ‘# of items’.<br />Statement4<br />Write a query that displays order id, customer id, name and quantity of ordered items, showing the largest quantity first. Use Headings: ‘Order ID’, ‘Cust ID’, ‘Customer’ and ‘# of items’.<br />T-SQL4<br />SELECTO.OrderID&apos;Order ID&apos;,O.CustomerID&apos;Cust ID&apos;,<br /> Name &apos;Customer&apos;,SUM(QuantityOrdered)&apos;# of items&apos;<br />FROM Customers C JOIN Orders O ON<br />C.CustomerID=O.CustomerIDJOINOrderItems OI ON<br />O.OrderID=OI.OrderID<br />GROUPBYO.OrderID,O.CustomerID, Name<br />ORDERBYSUM(QuantityOrdered)DESC<br />
  15. 15. Problem 5<br />The Available Books page is a report listing the publishers and the books they offer. Output a list of publishers, the books they publish and each book’s ISBN. Ensure the output lists the Publishers in alphabetical order and the titles in alphabetical order.<br />Statement5<br />Write a query to display publishers, books they publish and the ISBN for each book, displaying in alphabetical order<br />T-SQL5<br />SELECT Publisher, Title, ISBN<br />FROM Books<br />ORDERBY Publisher, Title<br />Problem 6<br />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’.<br />Statement6<br />Write a query to display customer ID, customer name, expiry date for records that are expiring within the next thirty days in descending order. Use headings: ‘ID’, ‘Name’, ‘Expires’<br />T-SQL6<br />SELECTCustomerID&apos;ID&apos;, Name &apos;Name&apos;,ExpiryDate&apos;Expires&apos;<br />FROM Customers<br />WHEREExpiryDate &lt; Getdate()+30<br />ORDERBYExpiryDateDESC<br />
  16. 16. Problem 7<br />The Author and Books page is a report which lists the author name, book title and ISBN. The list is to be output in order by author name and title.<br />Statement7<br />Write a query to display the author name, book title and ISBN, displayed in order by author name and title.<br />T-SQL7<br />SELECT Name, Title, BA.ISBN<br />FROM Authors A JOINBookAuthors BA ON<br />A.AuthorID=BA.AuthorIDJOIN Books B ON<br /> B.ISBN = BA.ISBN<br />ORDERBY Name, Title<br />Problem 8<br />The Hot Items page is a report of popular items. The user will input 5 in the txtMinimumTotal form field. Display ISBN, Title and the total number of items ordered. The total is to represent the sum of all quantity ordered amounts per ISBN. Display all records with a total greater than or equal to 5. The hottest items should be listed first. Alias as: ‘ISBN’, ’Title’ , ’Total’.<br />Statement8<br />Write a query that displays ISBN, Title, total number of items ordered where the total is greater or equal to 5, with the output ordered by total descending. Use headings ‘ISBN’, ‘Title’ and ‘Total’. <br />T-SQL8<br />SELECT OI.ISBN &apos;ISBN&apos;, Title &apos;Title&apos;,SUM(QuantityOrdered)&apos;Total&apos;<br />FROMOrderItems OI JOIN Books B ON<br /> OI.ISBN = B.ISBN<br />GROUPBY OI.ISBN, Title<br />HAVINGSUM(QuantityOrdered)&gt;= 5<br />ORDERBYSUM(QuantityOrdered)DESC<br />
  17. 17. Problem 9<br />The Search For Customers by Credit Card page also allows the user to search for Customers by the last four digits of their credit card number. The user inputs ‘’7889” in the txtLastFourDigits form field. Return all information from the Customer table. Alias as: ‘ID’, ‘Name’, ‘CurrentAddress’, ’CardType’, ‘CardNumber’, ‘Expires’. In case you have different customers using the same credit card, order by Customer ID in ascending order.<br />Statement9<br />Write a query to display customer id, name, address, cardtype, cardnumber, and expirydate using the last four digits in the credit card number. Use headings: ‘ID’, ‘Name’, ‘CurrentAddress’, ‘CardType’, ‘CardNumber’, and ‘Expires’.<br />T-SQL9<br />SELECTCustomerID&apos;ID&apos;, Name &apos;Name&apos;,Address&apos;CurrentAddress&apos;,<br />CardType&apos;CardType&apos;,CardNumber&apos;CardNumber&apos;,ExpiryDate&apos;Expires&apos;<br />FROM Customers<br />WHERECardNumberlike&apos;%7889&apos;<br />ORDERBYCustomerID<br />Problem 10<br />Write and execute a query on the Member and Adult tables in the Library database that returns the firstname, middleinitial, lastname, street, city, state and zip. Concatenate the firstname, middleinitial and lastname columns into one string and alias as Name. Make sure that the spacing is appropriate when there is no middle initial due to a NULL or empty string value. Display records in order by lastname and firstname.<br />Statement10<br />Write a query to display firstname, middleinitial, lastname, street, city, state and zip. Should be displayed in order by the lastname and firstname.<br />T-SQL10<br />SELECT (firstname+&apos; &apos;+isnull(middleinitial+&apos; &apos;,&apos;&apos;)+lastname) AS&apos;Name&apos;, street, city,state, zip<br /> FROM adult a JOIN member m ON<br />a.member_no=m.member_no<br /> ORDERBYlastname,firstname<br />
  18. 18. Problem 11<br />Write and execute a query on the Title, Item and Copy tables that returns the ISBN, copy_no, on_loan, title, translation, and cover for rows in the copy table with an ISBN of 500 or 1000. Only available books should be displayed and hardback copies should be listed first.<br />Statement11<br />Write a query that displays the ISBN, copy_no, on_loan, title, translation, and cover where the ISBN is 500 or 1000 and the books are available and order it by cover.<br />T-SQL11<br />SELECTc.isbn,copy_no,on_loan, title, translation, cover <br />FROM title t JOIN item iON<br />t.title_no=i.title_noJOIN copy c ON<br />c.isbn=i.isbn<br />WHERE(c.ISBN= 500 ORc.ISBN= 1000) AND on_loan=&apos;N&apos;<br />ORDERBY cover<br />Problem 12<br />Write and execute a query to retrieve the member’s full name and member_no from the Member table and the ISBN and log_date values from the Reservation table for member numbers 250, 341, and 1675. Order the results by member_no and log_date. You should show information for these members, even if they have no books on reserve.<br />Statement12<br />Write a query to display firstname, middleinitial, lastname, member_no, ISBN, log_date for members with 250, 341 and 1675 member numbers. Order results by member_no and log_date. Members who do not have books on reserve should be included in the output.<br />T-SQL12<br />SELECTfirstname,middleinitial,lastname,m.member_no,isbn,log_date<br />FROM member m LEFTOUTERJOIN reservation r ON<br />m.member_no=r.member_no<br />WHEREm.member_noin(250, 341, 1675)<br />ORDERBYm.member_no,log_date<br />
  19. 19. Problem 13<br />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.<br />Statement13<br />Write a query that displays ISBN, title, member_no, name of each member (adult/juvenile) who has the book with ISBN number 288 reserved; indicate whether member is adult/juvenile and output records by name.<br />T-SQL13 (Query #1 in problem #14)<br />SELECTc.isbn, title,a.member_no,lastname+&apos;, &apos;+firstname<br />as&apos;name&apos;,&apos;Adult&apos;as&apos;Adult / Juvenile&apos;<br />FROM adult a JOIN reservation r ON<br />a.member_no=r.member_noJOIN copy c ON<br />r.isbn=c.isbnJOIN title t ON<br />c.title_no=t.title_noJOIN member m ON<br />m.member_no=a.member_no<br />WHEREc.ISBN= 288<br />UNION<br />SELECTc.isbn, title,j.member_no,lastname+&apos;, &apos;+firstname<br />as&apos;name&apos;,&apos;Juvenile&apos;as&apos;Adult / Juvenile&apos;<br />FROM juvenile j JOIN reservation r ON<br />j.member_no=r.member_noJOIN copy c ON<br />r.isbn=c.isbnJOIN title t ON<br />c.title_no=t.title_noJOIN member m ON<br />m.member_no=j.member_no<br />WHEREc.ISBN= 288<br />ORDERBY&apos;name&apos;<br />
  20. 20. Problem 14<br />Write the above statement again using a CASE statement. You cannot JOIN to the Adult or Juvenile tables. Compare the two versions and determine which one is more efficient. Cut and paste the proof of your research. (No need to copy the output of records)<br />Statement14<br />Write the query from problem 13 using a CASE statement. Compare each version and determine which one is more efficient.<br />T-SQL14 (Query #2)<br />SELECTi.isbn, title,m.member_no,lastname+&apos;, &apos;+<br />firstname&apos;name&apos;,&apos;Adult/Juvenile&apos;=<br />CASE<br />WHENEXISTS(SELECTmember_no<br />FROM juvenile j<br />WHEREj.member_no=m.member_no)<br />THEN&apos;Juvenile&apos;<br />ELSE&apos;Adult&apos;<br />END<br />FROM member m JOIN reservation r ON<br />m.member_no=r.member_noJOIN item iON<br />i.isbn=r.isbnJOIN title t ON<br />t.title_no=i.title_no<br />WHEREi.isbn= 288<br />ORDERBY&apos;name&apos;<br />Output14<br />Viewing the Execution plan in SSMS, we find this:<br /> Query 1: Query cost (relative to the batch: 65%<br /> Query 2: Query cost (relative to the batch: 35%<br />The above indicates that Query 2, using the CASE statement, was more efficient.<br />
  21. 21. Problem 15<br />Write and execute a query that returns the member_no, full name, out_date, due_date, and title columns from the Loan, Member, and Title tables. Convert the datetime values from the out_date and due_date columns to char(12), format 101. Restrict the results to books which were due prior to the current date. Load the records into a temporary table called #overdue. Leave the query window open after you create the temporary table.<br />Statement15<br />Write a query to display member_no, full name, out_date, due_date, and title. Datetime values should be converted to specified format and only books that were due before today’s date should be returned. Load all returned records into a temp table.<br />T-SQL15<br />CREATETABLE #overdue<br />(member_nosmallintNOTNULL,<br />fullnamevarchar(35)NOTNULL,<br />out_datechar(12)NOTNULL,<br />due_datechar(12)NOTNULL,<br /> title varchar(63)NOTNULL)<br /> <br />INSERTINTO #overdue (member_no,fullname,out_date,due_date, title)<br />SELECTm.member_no,firstname+&apos; &apos;+ISNULL(SUBSTRING(middleinitial,1,1),&apos;&apos;)+<br />&apos; &apos;+lastname,CONVERT(char(12),out_date, 101)ASout_date,<br />CONVERT(char(12),due_date, 101)ASdue_date, title<br />FROM loan l JOIN title t ON<br />l.title_no=t.title_noJOIN member m ON<br />m.member_no=l.member_no<br />WHEREdue_date&lt;GETDATE()<br />
  22. 22. Problem 16<br />Write and execute a query that returns the member number, member name and number of past due loans of each member from the #overdue table in a column called ‘# Overdue’. Use the same query window that you used to create the temporary table. Display by the number outstanding and the member name.<br />Statement16<br />Write a query to display the member number, member name, number of past due loans in a specific column. Oder by number outstanding and the member name. Use heading: ‘# Overdue’.<br />T-SQL16<br />SELECTmember_no,fullname,COUNT(due_date)AS&apos;# Overdue&apos;<br />FROM #overdue<br />GROUPBYmember_no,fullname<br />ORDERBY&apos;# Overdue&apos;DESC,fullname<br />Problem 17<br />Write and execute a query that returns member_no, firstname, lastname and sum of fine_paid for members who have paid the highest fines to date. Members should only appear once in the list. Display the highest fine first. If more than one member has paid the same amount display the records in order by member_no.<br />Statement17<br />Write a query to display member_no, firstname, lastname, and sum of fine-paid for members who have paid the highest fines to date. Order by fine_paid, if more than one member has paid the same amount display in order by member_no.<br />T-SQL17<br />SELECTlh.member_no,firstname,lastname,SUM(fine_paid)<br />AS&apos;Paid Fines&apos;<br />FROMloanhistlhJOIN member m ON<br />lh.member_no=m.member_no<br />WHEREfine_paidISNOTNULL<br />GROUPBYlh.member_no,firstname,lastname<br />ORDERBY&apos;Paid Fines&apos;DESC,lh.member_no<br />
  23. 23. Problem 18<br />Write and execute a query on the Reservation table that returns the ISBN, title and Total. The Total column is a calculated column which represents the number of members wishing to reserve a particular book.<br />Statement18<br />Write a query that displays ISBN, title, and total (calculated column).<br />T-SQL18<br />SELECTr.isbn, title,Count(member_no)AS&apos;Total&apos;<br />FROM reservation r JOIN item iON<br />r.isbn=i.isbnJOIN title t ON<br />t.title_no=i.title_no<br />GROUPBYr.isbn, title<br />
  24. 24. Diagram 0<br />Views<br />Triggers<br />Stored procedures (code snippets of a few sp’s)<br />XML shred snippet<br />Piggy Bank 2 Project<br />
  25. 25.
  26. 26. Views<br />/****** Object: View [dbo].[v_CurrentMonthStatement] Script Date: 10/17/2009 00:43:55 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />CREATE VIEW [dbo].[v_CurrentMonthStatement] WITH SCHEMABINDING<br />AS<br /> SELECT CustomerFirstName, CustomerMiddleInitial, CustomerLastName,<br /> Street, City, State, ZipCode, TransactionID, <br />AccountID, TransactionTypeID, T.CustomerID, <br />TransactionDate, TransactionAmount, NewBalance<br /> FROM dbo.Transactions T JOIN dbo.Customer C ON<br />T.CustomerID = C.CustomerID<br />GO<br />/****** Object: View [dbo].[v_GetBalance] Script Date: 10/17/2009 00:43:55 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />CREATE VIEW [dbo].[v_GetBalance] WITH SCHEMABINDING<br />AS<br /> SELECT TransactionID, AccountID, T.TransactionTypeID, CustomerID, <br />TransactionDate, TransactionAmount, TransactionTypeName,<br />NewBalance<br /> FROM dbo.Transactions T JOIN dbo.TransactionType TT ON<br />T.TransactionTypeID = TT.TransactionTypeID<br />GO<br />
  27. 27. /****** Object: View [dbo].[v_GetHistoricalStatement] Script Date: 10/17/2009 00:43:55 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />CREATE VIEW [dbo].[v_GetHistoricalStatement] WITH SCHEMABINDING<br />AS<br /> SELECT CustomerFirstName, CustomerMiddleInitial, CustomerLastName,<br /> Street, City, State, ZipCode, TransactionID, AccountID,<br />TransactionTypeID, T.CustomerID, TransactionDate,<br />TransactionAmount, NewBalance<br /> FROM dbo.Transactions T JOIN dbo.Customer C ON<br />T.CustomerID = C.CustomerID<br />GO<br />/****** Object: View [dbo].[v_GetTransactions] Script Date: 10/17/2009 00:43:55 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />CREATE VIEW [dbo].[v_GetTransactions] WITH SCHEMABINDING<br />AS<br /> SELECT TransactionID, AccountID, TransactionTypeID,<br />CustomerID, TransactionDate, TransactionAmount,<br />NewBalance<br /> FROM dbo.Transactions<br />GO<br />
  28. 28. /****** Object: View [dbo].[v_ListCustomerAccounts] Script Date: 10/17/2009 00:43:55 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />CREATE VIEW [dbo].[v_ListCustomerAccounts] WITH SCHEMABINDING<br />AS<br /> SELECT CustomerFirstName, CustomerMiddleInitial, CustomerLastName,<br /> Street, City, State, ZipCode, CA.AccountID, CA.CustomerID<br /> FROM dbo.CustomerAccount CA JOIN dbo.Customer C ON<br />C.CustomerID = CA.CustomerID<br />GO<br />
  29. 29. Triggers<br />USE [PiggyBankWk4]<br />GO<br />/****** Object: DdlTrigger [NoUnwantedChanges] Script Date: 10/17/2009 00:50:48 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />CREATE TRIGGER [NoUnwantedChanges] <br />ON DATABASE <br />FOR DROP_TABLE, ALTER_TABLE <br />AS <br /> PRINT &apos;You must disable Trigger &quot;NoUnwantedChanges&quot; to drop or alter tables!&apos; <br />ROLLBACK;<br />GO<br />/****** Object: Trigger [dbo].[NoAccountDeletions] Script Date: 10/17/2009 00:51:09 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />CREATE TRIGGER [dbo].[NoAccountDeletions]<br />ON [PiggyBankWk4].[dbo].[Account]<br />FOR DELETE <br />AS <br /> PRINT &apos;You cannot delete accounts from the Account table, you may however disable the account.&apos; <br /> ROLLBACK<br />GO<br />
  30. 30. SP’s<br />Add Account Stored Proc Code Snippet<br />
  31. 31. Deposit Transaction Stored Proc Code Snippet<br />
  32. 32. Deposit Transfer Stored Proc Code Snippet<br />
  33. 33. Insert Customer Stored Proc Code Snippet<br />
  34. 34. Close Account Stored Proc Code Snippet<br />
  35. 35. Reopen Account Stored Proc Code Snippet<br />
  36. 36. Withdrawal Transfer Stored Proc Code Snippet<br />
  37. 37. XML shred<br />
  38. 38. Backup/Restore Disaster Recovery Mini-Simulation<br />Snapshot Creation and Usage<br />Working With Schemas<br />Resource Governor Configuration & Usage<br />Ownership Chaining<br />DBA Practical’s<br />
  39. 39. Backup & Restore: Disaster Recovery Mini-Simulation<br />--primary file group backup<br />BACKUP DATABASE [AdventureWorks2008] FILEGROUP = N&apos;PRIMARY&apos; TO [Adventureworks2008Backups] WITH NOFORMAT, INIT, <br /> NAME = N&apos;AdventureWorks2008-Full Primary Filegroup Backup&apos;, SKIP, NOREWIND, NOUNLOAD, STATS = 10<br />GO<br />--secondary file group backup<br />BACKUP DATABASE [AdventureWorks2008] FILEGROUP = N&apos;EmployeeWorkHistoryFG&apos; TO [Adventureworks2008Backups] WITH NOFORMAT, NOINIT, NAME = N&apos;AdventureWorks2008-Full Secondary Filegroup Backup&apos;, SKIP, NOREWIND, NOUNLOAD, STATS = 10<br />GO<br />--Update the ClockOutTime for the only record in the EmployeeWorkHistoryTable. The current value is NULL<br />USE AdventureWorks2008<br />GO<br />UPDATE HumanResources.EmployeeWorkHistorySET ClockOutTime= GETDATE() WHERE BusinessEntityID= 1<br />--log file backup<br />BACKUP LOG [AdventureWorks2008] TO [Adventureworks2008Backups] WITH NOFORMAT, NOINIT, <br /> NAME = N&apos;AdventureWorks2008-Transaction Log Backup&apos;, SKIP, NOREWIND, NOUNLOAD, STATS = 10<br />GO<br />--Simulate accidental deletion of a table by executing the following statements:<br />DROP TABLE HumanResources.EmployeeWorkHistory<br />--secondary filegroup with norecovery, the transaction log backup with RECOVERY<br />RESTORE HEADERONLY FROM Adventureworks2008Backups<br />--AdventureWorks2008-Full Primary FilegroupBackup,AdventureWorks2008-Full Secondary FilegroupBackup, AdventureWorks2008-Transaction Log Backup<br />RESTORE FILELISTONLY FROM Adventureworks2008Backups<br />--AdventureWorks2008_Data , EmpWorkHist, AdventureWorks2008_Log, -FileStreamDocuments<br />Use master<br />GO<br />RESTORE DATABASE AdventureWorks2008 FROM Adventureworks2008Backups WITH FILE = 1, NORECOVERY<br />--Processed 8 pages for database &apos;AdventureWorks2008&apos;, file &apos;EmpWorkHist&apos; on file 2.<br />--RESTORE DATABASE ... FILE=&lt;name&gt; successfully processed 8 pages in 0.134 seconds (0.466 MB/sec).<br />RESTORE LOG AdventureWorks2008 FROM Adventureworks2008Backups WITH RECOVERY<br />--Processed 0 pages for database &apos;AdventureWorks2008&apos;, file &apos;EmpWorkHist&apos; on file 3.<br />--The roll forward start point is now at log sequence number (LSN) 47000000029200001. Additional roll forward past LSN 47000000031500001 is required to complete the restore sequence.<br />--RESTORE LOG successfully processed 0 pages in 0.060 seconds (0.000 MB/sec).<br />
  40. 40. Snapshot Creation and Usage<br />/* Author: Susan Whitfield<br />Date: Friday, November 6, 2009<br />Purpose: To show database snapshot creation and usage.<br />*/<br />--creation of db snapshot<br />USE master<br />GO<br />CREATE DATABASE AdventureWorks_snapshot1420 ON<br />(NAME = AdventureWorks_Data,<br />FILENAME= &apos;C:Program FilesMicrosoft SQL ServerMSSQL10.MSSQLSERVERMSSQLDATAAdventureWorks_1420.ss&apos;)<br />AS SNAPSHOT OF AdventureWorks<br />--insertion of record<br />USE AdventureWorks<br />GO<br />INSERT INTO Production.ProductCategory(Name, ModifiedDate) VALUES (&apos;Test Category&apos;, GETDATE())<br />--making sure record exists<br />select * FROM Production.ProductCategoryWHERE Name = &apos;Test Category&apos;<br />--ProductCategoryID Name rowguidModifiedDate<br />--7 Test Category E597E244-E10E-4222-9F4D-9B14291AC41C 2009-11-06 15:19:07.603<br />--revert back to snapshot<br />USE master<br />GO<br />RESTORE DATABASE AdventureWorks FROM DATABASE_SNAPSHOT = &apos;AdventureWorks_snapshot1420&apos;<br />--testing to see if record is still there<br />USE AdventureWorks<br />GO<br />select * FROM Production.ProductCategoryWHERE Name = &apos;Test Category&apos;<br />--nothing is there with that name<br />
  41. 41. Working With Schemas<br />/* Author: Susan Whitfield<br />Date: Friday, November 6, 2009<br />Purpose: Schema work: Working With Schemas.<br />*/<br />--creating login<br />USE master<br />GO<br />CREATE LOGIN Michael WITH PASSWORD = &apos;P@$$w0rd&apos;<br />USE Adventureworks2008<br />GO<br />CREATE USER Michael WITH DEFAULT_SCHEMA = Person<br />GO<br />CREATE TABLE dbo.Person(FirstNamevarchar(25), LastNamevarchar(25), EmailAddressvarchar(50))<br />GO<br />INSERT INTO dbo.Person(FirstName, LastName, EmailAddress) VALUES (&apos;Joe&apos;, &apos;Mugg&apos;, &apos;jm@mugg.com&apos;)<br />GO<br />GRANT SELECT ON dbo.PersonTO Michael<br />--run from a window logged in under Michael<br />USE Adventureworks2008<br />GO<br />SELECT * FROM Person<br />GO<br />--didnt work because of ambiguity. Need to specify schema.table name<br />
  42. 42. Resource Governor Configuration & Usage<br />-- Configure Resource Governor.<br />BEGIN TRAN<br />USE Master<br />GO<br />-- Create a resource pool that sets the MAX_CPU_PERCENT to 40%. <br />CREATE RESOURCE POOL pMAX_CPU_PERCENT_40 WITH(max_cpu_percent=40)<br />GO<br />--creation of the workload group using resource pool created above.<br />CREATE WORKLOAD GROUP gMAX_CPU_PERCENT_40 USING pMAX_CPU_PERCENT_40<br />GO<br />-- Create a classification function.<br />-- Note that any request that does not get classified goes into <br />-- the &apos;Default&apos; group.<br />CREATE FUNCTION rgclassifier_MAX_CPU() RETURNS SYSNAME <br />WITH SCHEMABINDING<br />AS<br />BEGIN<br /> DECLARE @workload_group_name AS SYSNAME<br /> IF (SUSER_NAME() = &apos;Joe&apos;)<br />SET @workload_group_name= &apos;gMAX_CPU_PERCENT_40&apos;<br />RETURN @workload_group_name<br />END;<br />GO<br />-- Register the classifier function with Resource Governor.<br />ALTER RESOURCE GOVERNOR WITH (CLASSIFIER_FUNCTION= dbo.rgclassifier_MAX_CPU);<br />COMMIT TRAN;<br />GO<br />-- Start Resource Governor<br />ALTER RESOURCE GOVERNOR RECONFIGURE;<br />GO<br />
  43. 43. Ownership Chaining<br />USE master GO<br />CREATE LOGIN Joe WITH PASSWORD = &apos;P@$$w0rd&apos;<br />CREATE LOGIN Tom WITH PASSWORD = &apos;P@$$w0rd&apos;<br />GO<br />USE Adventureworks2008 GO<br />CREATE USER Joe<br />CREATE USER Tom<br />GO<br />CREATE SCHEMA Marketing AUTHORIZATION Joe GO<br />CREATE TABLE Marketing.PrintCompanies(Name varchar(25), PricePerPagemoney) GO<br />INSERT INTO Marketing.PrintCompanies(Name, PricePerPage) VALUES (&apos;Adventureworks&apos;, .25)<br />GRANT CREATE PROCEDURE TO Joe GO<br />GRANT SELECT ON Person.PersonTO Joe GO<br />--below was used in a new query logged in under Joe&apos;s login<br />CREATE PROCEDURE Marketing.uspPrintCompanies<br />AS<br />SET NOCOUNT ON<br />SELECT Name FROM Marketing.PrintCompanies<br />GO<br />CREATE PROCEDURE Marketing.uspPerson<br />AS<br />SET NOCOUNT ON<br />SELECT FirstName, LastNameFROM Person.Person<br />GO<br />GRANT EXECUTE ON Marketing.uspPrintCompaniesTO Tom<br />GRANT EXECUTE ON Marketing.uspPersonTO Tom<br />GO<br />--below was used in a new query logged in under Tom&apos;s login<br />USE AdventureWorks2008 GO<br />Execute Marketing.uspPrintCompanies<br />--works<br />EXECUTE Marketing.uspPerson<br />Msg 229, Level 14, State 5, Procedure uspPerson, Line 4<br />The SELECT permission was denied on the object &apos;Person&apos;, database &apos;AdventureWorks2008&apos;, schema &apos;Person&apos;.<br />--Tom does not have access to the Person.Person table. Select permissions were granted to Joe. Trying to do the select on the Person.Persontable will not work because it is outside of his<br />--&quot;ownership chain&quot;, meaning that even though Joe granted execute permissions to Tom, that only applies to the execution of the procedure. The owner or a dba or someone with sufficient permissions would need to grant select to Tom as well.<br />
  44. 44. Adventure Works Repair<br />Diagram 0<br />High Effort Jobs Report<br />Invoiced Job Summary<br />Customer Billing Summary<br />
  45. 45.
  46. 46. High Effort Jobs Report<br />--requirements<br />/*The final report (&quot;High Effort Jobs&quot;) is intended to<br /> show jobs where there is a disproportionately high <br /> effort by production staff, measured by total number <br /> of items on the job. The data should contain the <br /> following fields:<br /> Job name<br /> Customer name<br /> Total number of items<br /> Total gross margin<br /> Total revenue<br />The data should be sorted by &quot;Total number of items&quot;, <br />highest to lowest, should only show jobs with 5 or more <br />items, and should only show customers in the <br />&apos;Transactional&apos; or &apos;BPO&apos; classifications <br />(&apos;Enterprise&apos; classified customers should not be shown).<br />*/<br />SELECT JobName, CustomerName, count(JobItemID) as &apos;Total number of items&apos;,<br />(SUM(Cost) * MarginPercentage) as &apos;Total gross margin&apos;,(SUM(Cost)/(1-MarginPercentage)) as &apos;Total revenue&apos; <br />FROM Job J JOIN Customer C <br />ON J.CustomerID = C.CustomerID JOIN JobItem JI<br />ON JI.JobID = J.JobID JOIN Classification CL <br />ON CL.ClassificationID = C.ClassificationID JOIN Item I<br />ON I.ItemID = JI.ItemID<br />WHERE ClassificationNamein(&apos;Transactional&apos;, &apos;BPO&apos;)<br />GROUP BY JobName, CustomerName, MarginPercentage<br />HAVING COUNT(JI.ItemID) &gt;= 2 --I used 2 to test it out to ensure that it worked<br />ORDER BY &apos;Total Number of Items&apos; DESC<br />
  47. 47. Invoiced Job Summary<br />--requirements<br />/*<br />The first report (&quot;Invoiced Job Summary&quot;) represents <br />jobs for which we are attempting to collect revenue. <br />The data should contain the following fields:<br /> Job name<br /> Customer name<br /> Total job cost<br /> Total gross margin<br /> Total job revenue<br /> Date invoiced<br /> Days since invoice date<br />The data should be sorted by &quot;Days since invoice date&quot;, <br />highest to lowest, and only show jobs in &quot;Collection&quot; <br />status.<br />*/<br />SELECT JobName, CustomerName, SUM(Cost) as &apos;Total job cost&apos;, <br />(SUM(Cost) * MarginPercentage) as &apos;Total gross margin&apos;, <br />(SUM(Cost)/(1-MarginPercentage)) as &apos;Total revenue&apos;, DATE,<br />(Day(DATE)-DAY(GetDate())) as &apos;Days since invoice date&apos;<br />FROM Job J JOIN Customer C<br />ON J.CustomerID = C.CustomerID JOIN JobItem JI<br />ON JI.JobID = J.JobID JOIN Item I<br />ON JI.ItemID = I.ItemID JOIN Status S<br /> ON S.StatusID = J.StatusID JOIN JobStageDate JSD <br />ON JSD.JobID = JI.JobID<br />WHERE StatusName= &apos;Collection&apos; AND StageDateTypeID = 4<br />GROUP BY JobName, CustomerName, MarginPercentage, Date<br />ORDER BY &apos;Days since invoice date&apos; DESC<br />
  48. 48. Customer Billing Summary Requirements<br />--requirements<br />/*<br />The second report (&quot;Customer Billing Summary&quot;) represents <br />an overview of the billing activity for each customer. <br />The data should contain the following fields:<br /> Customer name<br /> Total number of jobs for this customer which are not in &quot;Closed&quot; status<br /> Total revenue for all jobs for this customer which are not in &quot;Closed&quot; status<br /> Total number of jobs for this customer which are in &quot;Collection&quot; status<br /> Total revenue for all for this customer which are jobs in &quot;Collection&quot; status<br /> Total number of jobs for this customer which are in &quot;Closed&quot; status<br /> Total revenue for all jobs for this customer which are in &quot;Closed&quot; status<br /> Average revenue for all jobs for this customer which are in &quot;Closed&quot; status<br /> Total gross margin for all jobs for this customer which are in &quot;Closed&quot; status<br /> Average gross margin for all jobs for this customer which are in &quot;Closed&quot; status<br />The data should be sorted by &quot;Total revenue for all jobs <br />for this customer which are in &apos;Closed&apos; status&quot;, highest <br />to lowest.<br />*/<br />
  49. 49. Customer Billing Summary<br />SELECT CustomerName, <br />(SELECT Count(JobID) FROM Job WHERE StatusID&lt;&gt; 6) as &apos;Total # jobs not closed&apos;, <br />(SELECT (SUM(Cost)/(1-MarginPercentage)) FROM Item I JOIN JobItem JI ON I.ItemID = JI.ItemID<br /> JOIN Job J ON J.JobID = JI.JobID<br />WHERE StatusID&lt;&gt; 6 GROUP BY MarginPercentage) as &apos;Total revenue for jobs not closed&apos;,<br />(SELECT Count(JobID) FROM Job WHERE StatusID= 5) as &apos;Total # jobs in collection&apos;,<br />(SELECT (SUM(Cost)/(1-MarginPercentage)) FROM Item I JOIN JobItem JI ON I.ItemID = JI.ItemID<br /> JOIN Job J ON J.JobID = JI.JobID<br />WHERE StatusID= 5 GROUP BY MarginPercentage) as &apos;Total revenue for jobs in collection&apos;,<br />(SELECT count(JobID) FROM Job WHERE StatusID= 6) as &apos;Total # jobs closed&apos;,<br />(SELECT (SUM(Cost)/(1-MarginPercentage)) FROM Item I JOIN JobItem JI ON I.ItemID = JI.ItemID<br /> JOIN Job J ON J.JobID = JI.JobID<br />WHERE StatusID= 6 GROUP BY MarginPercentage) as &apos;Total revenue for closed jobs&apos;,<br />(SELECT Avg((Cost)/(1-MarginPercentage)) FROM Item I JOIN JobItem JI ON I.ItemID = JI.ItemID<br /> JOIN Job J ON J.JobID = JI.JobID<br />WHERE StatusID= 6 GROUP BY MarginPercentage) as &apos;Average revenue for closed jobs&apos;,<br />(SELECT (SUM(Cost) * MarginPercentage) FROM Item I JOIN JobItem JI ON I.ItemID = JI.ItemID<br /> JOIN Job J ON J.JobID = JI.JobID<br />WHERE StatusID= 6 GROUP BY MarginPercentage) as &apos;Total gross margin for closed jobs&apos;,<br />(SELECT Avg((Cost) * MarginPercentage) FROM Item I JOIN JobItem JI ON I.ItemID = JI.ItemID<br /> JOIN Job J ON J.JobID = JI.JobID<br />WHERE StatusID= 6 GROUP BY MarginPercentage) as &apos;Average gross margin for closed jobs&apos;<br />FROM Customer <br />WHERE CustomerName= &apos;Outback Rigs&apos; --choose a customer name from list<br />ORDER BY &apos;Total revenue for closed jobs&apos; DESC<br />
  50. 50. Source Files<br />Table Creation<br />Triggers<br />Stored Procedure<br />User Defined Function<br />SSIS Packages<br />Mini Adventure Works<br />
  51. 51. 3 Master Files<br />ProductMaster.CSV<br />ShipMethodMaster.CSV<br />VendorMaster.CSV<br />Four Purchase Order Transaction Files <br />PODATA_2001.CSV<br />PODATA_2002.CSV<br />PODATA_2003.CSV<br />PODATA_2004.CSV<br />1 Master File Update <br />UpdatedProducts.CSV<br />Source Files<br />
  52. 52. Table Creation<br />Product Table<br />USE [MiniAdventureWorksDB]<br />GO<br />/****** Object: Table [dbo].[Product] Script Date: 11/27/2009 16:57:37 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />CREATE TABLE [dbo].[Product](<br /> [ProductID] [int] IDENTITY(1,1) NOT NULL,<br /> [ProductNumber] [nvarchar](50) NOT NULL,<br /> [ProductName] [nvarchar](100) NOT NULL,<br /> [ListPrice] [decimal](14, 4) NOT NULL,<br /> [DateInserted] [datetime] NOT NULL,<br /> [DateModified] [datetime] NOT NULL,<br />CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED <br />(<br /> [ProductID] ASC<br />)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],<br />CONSTRAINT [ProductNumber_unique] UNIQUE NONCLUSTERED <br />(<br /> [ProductNumber] ASC<br />)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]<br />) ON [PRIMARY]<br />GO<br />ALTER TABLE [dbo].[Product] ADD CONSTRAINT [DateInserted_def] DEFAULT (getdate()) FOR [DateInserted]<br />GO<br />ALTER TABLE [dbo].[Product] ADD CONSTRAINT [DateModified_def] DEFAULT (getdate()) FOR [DateModified]<br />GO<br />
  53. 53. Vendor Table<br />USE [MiniAdventureWorksDB]<br />GO<br />/****** Object: Table [dbo].[Vendor] Script Date: 11/27/2009 16:58:16 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />CREATE TABLE [dbo].[Vendor](<br /> [VendorID] [int] IDENTITY(1,1) NOT NULL,<br /> [Name] [nvarchar](100) NOT NULL,<br /> [AccountNumber] [nvarchar](50) NOT NULL,<br /> [CreditRating] [tinyint] NOT NULL,<br /> [DateInserted] [datetime] NOT NULL,<br /> [DateModified] [datetime] NOT NULL,<br />CONSTRAINT [PK_Vendor] PRIMARY KEY CLUSTERED <br />(<br /> [VendorID] ASC<br />)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],<br />CONSTRAINT [AccountNumber_unique] UNIQUE NONCLUSTERED <br />(<br /> [AccountNumber] ASC<br />)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]<br />) ON [PRIMARY]<br />GO<br />ALTER TABLE [dbo].[Vendor] ADD CONSTRAINT [VendorDateInserted_def] DEFAULT (getdate()) FOR [DateInserted]<br />GO<br />ALTER TABLE [dbo].[Vendor] ADD CONSTRAINT [VendorDateModified_def] DEFAULT (getdate()) FOR [DateModified]<br />GO<br />
  54. 54. ShipMethod Table<br />USE [MiniAdventureWorksDB]<br />GO<br />/****** Object: Table [dbo].[ShipMethod] Script Date: 11/27/2009 16:59:25 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />CREATE TABLE [dbo].[ShipMethod](<br /> [ShipMethodID] [int] IDENTITY(1,1) NOT NULL,<br /> [Name] [nvarchar](50) NOT NULL,<br /> [DateInserted] [datetime] NOT NULL,<br /> [DateModified] [datetime] NOT NULL,<br />CONSTRAINT [PK_ShipMethod] PRIMARY KEY CLUSTERED <br />(<br /> [ShipMethodID] ASC<br />)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],<br />CONSTRAINT [Name_unique] UNIQUE NONCLUSTERED <br />(<br /> [Name] ASC<br />)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]<br />) ON [PRIMARY]<br />GO<br />ALTER TABLE [dbo].[ShipMethod] ADD CONSTRAINT [ShipMethodDateInserted_def] DEFAULT (getdate()) FOR [DateInserted]<br />GO<br />ALTER TABLE [dbo].[ShipMethod] ADD CONSTRAINT [ShipMethodDateModified_def] DEFAULT (getdate()) FOR [DateModified]<br />GO<br />
  55. 55. PurchaseOrderHeader Table<br />USE [MiniAdventureWorksDB]<br />GO<br />/****** Object: Table [dbo].[PurchaseOrderHeader] Script Date: 11/27/2009 17:00:46 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />CREATE TABLE [dbo].[PurchaseOrderHeader](<br /> [PurchaseOrderID] [int] IDENTITY(1,1) NOT NULL,<br /> [POHeaderNumber] [nvarchar](20) NOT NULL,<br /> [VendorID] [int] NOT NULL,<br /> [ShipMethodID] [int] NOT NULL,<br /> [OrderDate] [date] NOT NULL,<br /> [Freight] [decimal](14, 4) NOT NULL,<br /> [TotalDue] [decimal](14, 4) NOT NULL,<br /> [DateInserted] [datetime] NOT NULL,<br /> [DateModified] [datetime] NOT NULL,<br />CONSTRAINT [PK_PurchaseOrderHeader] PRIMARY KEY CLUSTERED <br />(<br /> [PurchaseOrderID] ASC<br />)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],<br />CONSTRAINT [PurchaseOrderHeader_unique] UNIQUE NONCLUSTERED <br />(<br /> [POHeaderNumber] ASC<br />)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]<br />) ON [PRIMARY]<br />GO<br />ALTER TABLE [dbo].[PurchaseOrderHeader] WITH CHECK ADD CONSTRAINT [FK_PurchaseOrderHeader_Vendor] FOREIGN KEY([VendorID])<br />REFERENCES [dbo].[Vendor]([VendorID])<br />GO<br />ALTER TABLE [dbo].[PurchaseOrderHeader] CHECK CONSTRAINT [FK_PurchaseOrderHeader_Vendor]<br />GO<br />ALTER TABLE [dbo].[PurchaseOrderHeader] ADD CONSTRAINT [POHDateInserted_def] DEFAULT (getdate()) FOR [DateInserted]<br />GO<br />ALTER TABLE [dbo].[PurchaseOrderHeader] ADD CONSTRAINT [POHDateModified_def] DEFAULT (getdate()) FOR [DateModified]<br />GO<br />
  56. 56. PurchaseOrderDetail Table<br />USE [MiniAdventureWorksDB]<br />GO<br />/****** Object: Table [dbo].[PurchaseOrderDetail] Script Date: 11/27/2009 17:01:13 ******/<br />SET ANSI_NULLS ON GO<br />SET QUOTED_IDENTIFIER ON GO<br />SET ANSI_PADDING ON GO<br />CREATE TABLE [dbo].[PurchaseOrderDetail](<br /> [PurchaseOrderID] [int] NOT NULL,<br /> [PurchaseOrderDetailID] [int] IDENTITY(1,1) NOT NULL,<br /> [ProductID] [int] NOT NULL,<br /> [OrderQty] [int] NOT NULL,<br /> [UnitPrice] [decimal](14, 4) NOT NULL,<br /> [TotalDue] AS ([UnitPrice]*[OrderQty]) PERSISTED,<br /> [DateInserted] [datetime] NOT NULL,<br /> [DateModified] [datetime] NOT NULL,<br />CONSTRAINT [PK_PurchaseOrderDetail_1] PRIMARY KEY CLUSTERED <br />(<br /> [PurchaseOrderID] ASC,<br /> [ProductID] ASC<br />)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]<br />) ON [PRIMARY]<br />GO<br />SET ANSI_PADDING OFF GO<br />ALTER TABLE [dbo].[PurchaseOrderDetail] WITH CHECK ADD CONSTRAINT [FK_PurchaseOrderDetail_Product] FOREIGN KEY([ProductID])<br />REFERENCES [dbo].[Product]([ProductID]) GO<br />ALTER TABLE [dbo].[PurchaseOrderDetail] CHECK CONSTRAINT [FK_PurchaseOrderDetail_Product] GO<br />ALTER TABLE [dbo].[PurchaseOrderDetail] WITH CHECK ADD CONSTRAINT [FK_PurchaseOrderDetail_PurchaseOrderHeader] FOREIGN KEY([PurchaseOrderID]) REFERENCES [dbo].[PurchaseOrderHeader]([PurchaseOrderID]) GO<br />ALTER TABLE [dbo].[PurchaseOrderDetail] CHECK CONSTRAINT [FK_PurchaseOrderDetail_PurchaseOrderHeader] GO<br />ALTER TABLE [dbo].[PurchaseOrderDetail] ADD CONSTRAINT [PODDateInserted_def] DEFAULT (getdate()) FOR [DateInserted] GO<br />ALTER TABLE [dbo].[PurchaseOrderDetail] ADD CONSTRAINT [PODDateModified_def] DEFAULT (getdate()) FOR [DateModified] GO<br />
  57. 57. Triggers<br />Trigger Creation<br />USE MiniAdventureWorksDB<br />GO<br />CREATE TRIGGER UpdateProduct<br />ON dbo.Product<br />AFTER UPDATE <br />AS<br /> BEGIN<br /> SET NOCOUNT ON;<br />UPDATE dbo.Product<br />SET DateModified= GetDate()<br />FROM dbo.Product P JOIN INSERTED I<br />ON P.ProductID = I.ProductID JOIN DELETED D<br />ON P.ProductID = D.ProductID<br />WHERE I.ProductName &lt;&gt; D.ProductName OR<br />I.ListPrice &lt;&gt; D.ListPrice<br />END<br />GO<br />USE MiniAdventureWorksDB<br />GO<br />CREATE TRIGGER UpdatePurchaseOrderDetail<br />ON dbo.PurchaseOrderDetail<br />AFTER UPDATE <br />AS<br /> BEGIN<br /> SET NOCOUNT ON;<br />UPDATE dbo.PurchaseOrderDetail<br />SET DateModified= GetDate()<br />FROM dbo.PurchaseOrderDetail POD JOIN INSERTED I<br />ON POD.PurchaseOrderDetailID = I.PurchaseOrderDetailID JOIN DELETED D<br />ON POD.PurchaseOrderDetailID = D.PurchaseOrderDetailID<br />WHERE I.OrderQty &lt;&gt; D.OrderQty OR<br />I.UnitPrice &lt;&gt; D.UnitPrice<br />END<br />GO<br />
  58. 58. Trigger Creation<br />USE MiniAdventureWorksDB<br />GO<br />CREATE TRIGGER UpdatePurchaseOrderHeader<br />ON dbo.PurchaseOrderHeader<br />AFTER UPDATE <br />AS<br /> BEGIN<br /> SET NOCOUNT ON;<br />UPDATE dbo.PurchaseOrderHeader<br />SET DateModified= GetDate()<br />FROM dbo.PurchaseOrderHeader POH JOIN INSERTED I<br />ON POH.PurchaseOrderID = I.PurchaseOrderID JOIN DELETED D<br />ON POH.PurchaseOrderID = D.PurchaseOrderID<br />WHERE I.OrderDate &lt;&gt; D.OrderDate OR<br />I.Freight &lt;&gt; D.Freight<br />END<br />GO<br />USE MiniAdventureWorksDB<br />GO<br />CREATE TRIGGER UpdateShipMethod<br />ON dbo.ShipMethod<br />AFTER UPDATE <br />AS<br /> BEGIN<br /> SET NOCOUNT ON;<br />UPDATE dbo.ShipMethod<br />SET DateModified= GetDate()<br />FROM dbo.ShipMethod SM JOIN INSERTED I<br />ON SM.ShipMethodID = I.ShipMethodID JOIN DELETED D<br />ON SM.ShipMethodID = D.ShipMethodID<br />WHERE I.Name &lt;&gt; D.Name<br />END<br />GO<br />
  59. 59. Trigger Creation<br />USE MiniAdventureWorksDB<br />GO<br />CREATE TRIGGER UpdateVendor<br />ON dbo.Vendor<br />AFTER UPDATE <br />AS<br /> BEGIN<br /> SET NOCOUNT ON;<br />UPDATE dbo.Vendor<br />SET DateModified= GetDate()<br />FROM dbo.Vendor V JOIN INSERTED I<br />ON V.VendorID = I.VendorID JOIN DELETED D<br />ON V.VendorID = D.VendorID<br />WHERE I.Name &lt;&gt; D.Name OR<br />I.CreditRating &lt;&gt; D.CreditRating<br />END<br />GO<br />
  60. 60. Stored Procedure<br />Stored Procedure Creation<br />USE MiniAdventureWorksDB<br />GO<br />CREATE procedure InsertOrderHeader<br /> @POHeaderNumbernvarchar(50), @VendorIDint, @ShipMethodIDint, <br /> @OrderdateDateTime, @Freight decimal(14,2), @TotalDuedecimal (14,2), <br /> @PurchaseOrderIDint OUTPUT<br />AS<br /> BEGIN<br /> INSERT INTO PurchaseOrderHeader<br />(POHeaderNumber, VendorID, ShipMethodID, OrderDate, Freight, TotalDue) <br />VALUES (@POHeaderNumber, @VendorID , @ShipMethodID , @Orderdate , @Freight, @TotalDue)<br />SET @PurchaseOrderID= SCOPE_IDENTITY()<br />END<br />GO<br />
  61. 61. User Defined Function<br />User Defined Function<br />USE [MiniAdventureWorksDB]<br />GO<br />/****** Object: UserDefinedFunction [dbo].[udf_Top_N_ProductOrdersForVendor] Script Date: 11/30/2009 19:59:19 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />CREATE FUNCTION [dbo].[udf_Top_N_ProductOrdersForVendor] <br />(@VendorIDint, @TopCountint)<br />RETURNS @TopProducts TABLE <br />(VendorIDint, ProductIDint, ProductNamevarchar(100), <br />ProductTotalDueDECIMAL(14,2), ProductRankint)<br />AS<br /> BEGIN<br />;WITH ProductCTE AS <br />(SELECT TOP (@TopCount) VendorID, ProductID, SUM(POD.TotalDue) AS ProductTotalDue<br /> FROM dbo.PurchaseOrderDetail POD join dbo.PurchaseOrderHeader POH <br />ON POD.PurchaseOrderID = POH.PurchaseOrderID<br />WHERE POH.VendorID = @VendorID<br />GROUP BY VendorID,ProductID<br />ORDER BY ProductTotalDueDesc)<br />INSERT INTO @TopProducts<br /> SELECT VendorID, ProductCTE.ProductID, Product.ProductNameas ProductName, <br />ProductTotalDue, <br />dense_RANK() OVER (ORDER BY ProductTotalDuedesc) as ProductRank<br /> FROM ProductCTEJOIN dbo.Product<br />ON ProductCTE.ProductID = Product.ProductID<br />RETURN <br />END<br />GO<br />
  62. 62. SSIS Packages<br />Create Database<br />
  63. 63. Import Products<br />
  64. 64. Import Products<br />
  65. 65. Import Products<br />
  66. 66. Import Vendors<br />
  67. 67. Import Vendors<br />
  68. 68. Import ShipMethod<br />
  69. 69. Import ShipMethod<br />
  70. 70. Import Orders<br />
  71. 71. Import Orders<br />
  72. 72. Product Price Increase<br />
  73. 73. Product Price Increase<br />
  74. 74. Stored Procedures<br />XML Source Files<br />SSIS Packages<br />BlockFlix<br />
  75. 75. Stored Procedures<br />Insert Movie<br />USE [Blockflix]<br />GO<br />/****** Object: StoredProcedure [dbo].[usp_InsertMovie] Script Date: 12/11/2009 11:46:49 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />CREATE procedure [dbo].[usp_InsertMovie]<br /> @MovieTitlenvarchar(100), @GenreIDint, @RatingIDint, <br /> @Year nvarchar(4), @MovieDescriptionnvarchar(max),<br /> @MovieIDint OUTPUT<br />AS<br /> BEGIN<br /> INSERT INTO dbo.Movies<br />(MovieTitle, GenreID, RatingID, Year, MovieDescription) <br />VALUES (@MovieTitle, @GenreID, @RatingID, @Year, @MovieDescription)<br />SET @MovieID= SCOPE_IDENTITY()<br />END<br />GO<br />
  76. 76. Insert Movie Into Inventory<br />USE [Blockflix]<br />GO<br />/****** Object: StoredProcedure [dbo].[usp_InsertMovieIntoInventory] Script Date: 12/11/2009 11:48:45 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />CREATE procedure [dbo].[usp_InsertMovieIntoInventory]<br /> @ProductNamenvarchar(50), @ItemQtyint,<br /> @ProductTypeIDint, @MediaTypeIDint,<br /> @StatusIDint, @MovieIDint<br />AS<br /> BEGIN<br /> DECLARE @loopiterationint, @RowQtyint<br /> SET @loopiteration= 0<br />SET @RowQty= 1<br />WHILE @loopiteration&lt; @ItemQty<br />BEGIN<br />INSERT INTO dbo.MasterInventory<br />(ProductName, QTY, ProductTypeID, MediaTypeID, StatusID, MovieID)<br />VALUES<br />(@ProductName, @RowQty, @ProductTypeID, @MediaTypeID, @StatusID,(SELECT MovieID FROM Movies <br /> WHERE MovieTitle= @ProductName))<br />SET @loopiteration= @loopiteration + 1<br />END<br /> END<br />GO<br />
  77. 77. Insert Into Cast<br />USE [Blockflix] GO<br />/****** Object: StoredProcedure [dbo].[usp_InsertIntoCast] Script Date: 12/11/2009 11:49:52 ******/<br />SET ANSI_NULLS ON GO<br />SET QUOTED_IDENTIFIER ON GO<br />CREATE procedure [dbo].[usp_InsertIntoCast]<br /> @MovieIDint, @Director1ID int, @Director2ID int, @Actor1ID int, <br /> @Actor2ID int, @Actor3ID int, @Producer1ID int, @Producer2ID int<br />AS<br /> BEGIN<br />--directors: JobTypeID for director is 2<br /> --director1<br />INSERT INTO dbo.Cast(MovieID, PersonID, JobTypeID) VALUES (@MovieID, @Director1ID, 2)<br />--if director2 is not null then it will insert into the Cast table <br />IF @Director2ID IS NOT NULL AND @Director2ID &lt;&gt; 0<br />BEGIN<br />INSERT INTO dbo.Cast(MovieID, PersonID, JobTypeID) VALUES (@MovieID, @Director2ID, 2)<br />END<br /> ELSE<br />--actors: JobTypeID for actors is 1<br /> --actor1<br />INSERT INTO dbo.Cast(MovieID, PersonID, JobTypeID) VALUES (@MovieID, @Actor1ID, 1)<br />--actor2<br />INSERT INTO dbo.Cast(MovieID, PersonID, JobTypeID) VALUES (@MovieID, @Actor2ID, 1)<br />--actor3<br />INSERT INTO dbo.Cast(MovieID, PersonID, JobTypeID) VALUES (@MovieID, @Actor3ID, 1)<br />--producers: JobTypeID for producers is 3<br /> --producer1<br />INSERT INTO dbo.Cast(MovieID, PersonID, JobTypeID) VALUES (@MovieID, @Producer1ID, 3)<br />--producer2: testing to see if producer2 is not null <br />IF @Producer2ID IS NOT NULL AND @Producer2ID &lt;&gt; 0<br />BEGIN<br />INSERT INTO dbo.Cast(MovieID, PersonID, JobTypeID) VALUES (@MovieID, @Producer2ID, 3)<br />END<br /> END<br />GO<br />
  78. 78. Insert Products<br />USE [Blockflix]<br />GO<br />/****** Object: StoredProcedure [dbo].[usp_InsertProducts] Script Date: 12/11/2009 11:50:09 ******/<br />SET ANSI_NULLS ON<br />GO<br />SET QUOTED_IDENTIFIER ON<br />GO<br />CREATE procedure [dbo].[usp_InsertProducts]<br /> @ProductNamenvarchar(50), @ItemQtyint,<br /> @ProductTypeIDint, @MediaTypeIDint,<br /> @StatusIDint<br />AS<br /> BEGIN<br />--local variable declaration<br />DECLARE @loopiterationint, @RowQtyint<br />SET @loopiteration= 0<br />SET @RowQty= 1<br />--testing to see if the product type is video game (2)<br />IF @ProductTypeID= 2<br />BEGIN <br />--loop will insert multiple rows for video games so that <br />--each copy of a video game will have a unique SKU #<br />WHILE @loopiteration&lt; @ItemQty<br />BEGIN<br />INSERT INTO dbo.MasterInventory(ProductName, QTY, ProductTypeID, MediaTypeID, StatusID)<br />VALUES (@ProductName, @RowQty, @ProductTypeID, @MediaTypeID, @StatusID)<br />SET @loopiteration= @loopiteration + 1<br />END<br />END<br />ELSE<br /> INSERT INTO dbo.MasterInventory(ProductName, QTY, ProductTypeID, MediaTypeID, StatusID)<br />VALUES (@ProductName, @ItemQty, @ProductTypeID, @MediaTypeID, @StatusID)<br />END<br />GO<br />
  79. 79. Customer Queue Part 1<br />
  80. 80. Customer Queue Part 2<br />
  81. 81. Customer Queue Results<br />
  82. 82. XML Source File For Movies<br />
  83. 83. XML Source File For Products<br />
  84. 84. SSIS Packages<br />Import Movies<br />
  85. 85. Import Movies<br />
  86. 86. Import Movies<br />
  87. 87. Import Movies<br />
  88. 88. Import Products<br />
  89. 89. Import Products<br />
  90. 90. Import Store Info<br />
  91. 91. Import Kiosk Info<br />
  92. 92. Import Kiosk Info<br />
  93. 93. Database Design Diagram<br />Instructor Results Report Examples (SSRS)<br />Evaluation Application<br />

×