Microsoft SQL Server 2008 R2

1,793 views

Published on

Published in: Technology
  • Be the first to comment

Microsoft SQL Server 2008 R2

  1. 1. MICROSOFT SQL SERVERBySunny Okoro
  2. 2. IntroductionThe purpose of this document is to illustrate various database operations that can be performed on Microsoft SQL Server including objectcreations like tables and views along with data mainpulations with join operations like inner joins and functions both built in and user definedfunctions.Development ToolsDatabase Platform: Microsoft SQL Server 2008R2Applications: Microsot Excel and Power Pivot, Microsoft SQL Server Business Intelligence Development Studio, Microsoft SQL Server Reporting Services
  3. 3. The date formats specified in the query statement outputmay be different from the ones represented in the table because Microsoft Excel PowerPiviot did an automatic convertation. Theoriginal query from MS SQL Server would have occupied many pages when not presented in a table format. In some currency related data the dollar symbol were added in Excel to make iteasier to read.USE DB665SELECT top(25)CUST_NO,DOB AS DATE_OF_BIRTH,F_NAME + ,+L_NAME AS CUSTOMER_NAMEFROM dbo.CUSTOMER_DIM009;CUST_NO DATE_OF_BIRTH CUSTOMER_NAME----------- ----------------------- -----------------------------------------------------------------------------------------------------13886 1970-09-26 00:00:00.000 Rachel,Taylor13887 1970-10-12 00:00:00.000 Nathan,Scott13888 1967-11-06 00:00:00.000 Ian,Brown13889 1966-08-10 00:00:00.000 Robert,Martin13890 1966-09-16 00:00:00.000 Jennifer,Carter13891 1966-12-07 00:00:00.000 Aaron,Simmons13892 1966-11-25 00:00:00.000 Spencer,Diaz13893 1966-08-14 00:00:00.000 Dalton,Carter13894 1969-02-25 00:00:00.000 Robyn,Ruiz13895 1969-12-25 00:00:00.000 Isabella,Brooks13896 1969-10-10 00:00:00.000 Hailey,Mitchell13897 1969-11-24 00:00:00.000 Carol,Washington13898 1969-10-11 00:00:00.000 Richard,Baker13899 1969-08-17 00:00:00.000 Cameron,Jones13900 1969-04-13 00:00:00.000 Isaac,Campbell13901 1969-10-21 00:00:00.000 Oscar,Foster13902 1969-03-18 00:00:00.000 Jeremiah,Simmons13903 1969-11-27 00:00:00.000 Elijah,Hernandez13904 1969-03-09 00:00:00.000 Natalie,Thompson13905 1969-03-07 00:00:00.000 Christian,Henderson13906 1965-06-25 00:00:00.000 Courtney,Evans13907 1965-02-22 00:00:00.000 Misty,Xie13908 1965-07-20 00:00:00.000 Alyssa,Diaz13909 1965-01-21 00:00:00.000 Riley,Price13910 1965-03-09 00:00:00.000 Isabelle,Butler(25 row(s) affected)SELECT TOP(12)CUST_NO,CONVERT(DATE, DOB)AS DATE_OF_BIRTH,F_NAME + ,+L_NAME AS CUSTOMER_NAMEFROM dbo.CUSTOMER_DIM009;CUST_NO DATE_OF_BIRTH CUSTOMER_NAME13886 9/26/1970 Rachel,Taylor13887 10/12/1970 Nathan,Scott13888 11/6/1967 Ian,Brown13889 8/10/1966 Robert,Martin13890 9/16/1966 Jennifer,Carter13891 12/7/1966 Aaron,Simmons13892 11/25/1966 Spencer,Diaz13893 8/14/1966 Dalton,Carter
  4. 4. 13894 2/25/1969 Robyn,Ruiz13895 12/25/1969 Isabella,Brooks13896 10/10/1969 Hailey,Mitchell13897 11/24/1969 Carol,Washington(12 row(s) affected)SELECT TOP(12)CUST_NO,CONVERT(DATE, DOB)AS DATE_OF_BIRTH,F_NAME + ,+L_NAME AS CUSTOMER_NAMEFROM dbo.CUSTOMER_DIM009ORDER BY CUSTOMER_NAME;CUST_NO DATE_OF_BIRTH CUSTOMER_NAME28866 2/5/1974 Aaron,Adams20285 10/21/1976 Aaron,Alexander20075 5/18/1953 Aaron,Allen17862 3/18/1960 Aaron,Baker12067 11/14/1944 Aaron,Bryant21414 6/7/1974 Aaron,Butler21151 12/22/1967 Aaron,Campbell27916 3/19/1974 Aaron,Carter28187 11/13/1959 Aaron,Chen16749 5/10/1974 Aaron,Coleman27663 9/24/1960 Aaron,Collins18695 11/4/1969 Aaron,Diaz(12 row(s) affected)SELECT TOP(12)CUST_NO,CONVERT(DATE, DOB)AS DATE_OF_BIRTH,DATEDIFF(YY, DOB, CURRENT_TIMESTAMP) AS AGE, GENDER,F_NAME + ,+L_NAME AS CUSTOMER_NAMEFROM dbo.CUSTOMER_DIM009;CUST_NO DATE_OF_BIRTH AGE GENDER CUSTOMER_NAME13886 9/26/1970 41 F Rachel,Taylor13887 10/12/1970 41 M Nathan,Scott13888 11/6/1967 44 M Ian,Brown13889 8/10/1966 45 M Robert,Martin13890 9/16/1966 45 F Jennifer,Carter13891 12/7/1966 45 M Aaron,Simmons13892 11/25/1966 45 M Spencer,Diaz13893 8/14/1966 45 M Dalton,Carter13894 2/25/1969 42 F Robyn,Ruiz13895 12/25/1969 42 F Isabella,Brooks13896 10/10/1969 42 F Hailey,Mitchell13897 11/24/1969 42 M Carol,Washington
  5. 5. (12 row(s) affected)SELECT CUST_NO,CONVERT(DATE, DOB)AS DATE_OF_BIRTH,DATEDIFF(YY, DOB, CURRENT_TIMESTAMP) AS AGE, GENDER,F_NAME + ,+L_NAME AS CUSTOMER_NAMEINTOCUSTOMER_NOT_50FROM dbo.CUSTOMER_DIM009WHERE DATEDIFF(YY, DOB, CURRENT_TIMESTAMP) < 50;(10215 row(s) affected)SELECT CUST_NO,CONVERT(DATE, DOB)AS DATE_OF_BIRTH,DATEDIFF(YY, DOB, CURRENT_TIMESTAMP) AS AGE, GENDER,F_NAME + ,+L_NAME AS CUSTOMER_NAMEINTOCUSTOMER_50_ABOVEFROM dbo.CUSTOMER_DIM009WHERE DATEDIFF(YY, DOB, CURRENT_TIMESTAMP) >=50;(8269 row(s) affected)SELECT TOP(29)CUST_NO, DATE_OF_BIRTH, AGE, GENDER,CUSTOMER_NAMEFROMCUSTOMER_NOT_50;CUST_NO DATE_OF_BIRTH AGE GENDER CUSTOMER_NAME13886 9/26/1970 41 F Rachel,Taylor13887 10/12/1970 41 M Nathan,Scott13888 11/6/1967 44 M Ian,Brown13889 8/10/1966 45 M Robert,Martin13890 9/16/1966 45 F Jennifer,Carter13891 12/7/1966 45 M Aaron,Simmons13892 11/25/1966 45 M Spencer,Diaz13893 8/14/1966 45 M Dalton,Carter13894 2/25/1969 42 F Robyn,Ruiz13895 12/25/1969 42 F Isabella,Brooks13896 10/10/1969 42 F Hailey,Mitchell13897 11/24/1969 42 M Carol,Washington13898 10/11/1969 42 M Richard,Baker13899 8/17/1969 42 M Cameron,Jones13900 4/13/1969 42 M Isaac,Campbell13901 10/21/1969 42 M Oscar,Foster13902 3/18/1969 42 M Jeremiah,Simmons
  6. 6. 13903 11/27/1969 42 M Elijah,Hernandez13904 3/9/1969 42 F Natalie,Thompson13905 3/7/1969 42 M Christian,Henderson13906 6/25/1965 46 F Courtney,Evans13907 2/22/1965 46 F Misty,Xie13908 7/20/1965 46 F Alyssa,Diaz13909 1/21/1965 46 F Riley,Price13910 3/9/1965 46 F Isabelle,Butler13911 5/26/1965 46 F Andrea,Kelly13912 3/20/1965 46 F Caroline,Simmons13913 9/18/1965 46 F Angela,Rogers13914 10/23/1964 47 F Abigail,BellSELECT TOP(29)CUST_NO, DATE_OF_BIRTH, AGE, GENDER,CUSTOMER_NAMEFROMCUSTOMER_50_ABOVE;CUST_NO DATE_OF_BIRTH AGE GENDER CUSTOMER_NAME11311 8/24/1953 58 F Gabrielle,Lopez11312 4/3/1953 58 F Sara,Richardson11313 2/27/1953 58 M Trevor,Jenkins11314 12/21/1953 58 F Mya,Flores11315 10/1/1953 58 F Hailey,Ward11316 11/17/1953 58 M Luke,Allen11317 9/17/1952 59 F Victoria,Russell11318 10/26/1952 59 F Jessica,Wilson11319 4/27/1937 74 F Jade,Bailey11320 6/26/1937 74 F Morgan,Hill11321 7/8/1938 73 M Terrance,Raman11322 3/15/1938 73 F Sydney,Garcia11323 6/20/1938 73 M Jose,Patterson11324 5/14/1939 72 M Zachary,Anderson11325 7/2/1939 72 M Elijah,Ross11326 6/26/1939 72 M Rafael,Xie
  7. 7. 11327 3/4/1940 71 M Jaime,Moreno11328 1/1/1940 71 M Julian,Griffin11329 7/25/1940 71 M Andy,Alvarez11330 5/13/1940 71 M Ryan,Thompson11334 3/22/1961 50 F Nicole,Brown11335 6/24/1961 50 F Carla,Raman11337 9/21/1961 50 M Jerome,Romero11338 8/10/1961 50 M Frank,Navarro11339 7/8/1961 50 M Dennis,She11340 4/6/1936 75 F Melody,Munoz11341 8/21/1936 75 M Randy,Zeng11343 9/13/1958 53 M Arthur,Carlson11344 7/24/1957 54 F Jessie,Jimenez(29 row(s) affected)select salter.SalesTerritoryKey as sale_territory_code, ltime.Fiscal_Year, ROUND(sum(final_amount),2) as total_amountfrom dbo.SALES_TERRITORY_DIM salter inner join dbo.sales_fact salon salter.SalesTerritoryKey = sal.TERRITORY_KEYinner join dbo.time_dim_new ltimeon ltime.time_id = sal.Time_IDwhere final_amount is not nullgroup by rollup(salter.SalesTerritoryKey,ltime.Fiscal_Year);sale_territory_code Fiscal_Year total_amount1 2002 $1,023,044.201 2003 $684,599.941 2004 $2,314,226.181 2005 $11,232.621 $4,033,102.932 2003 $2,264.252 2004 $4,954.122 $7,218.383 2003 $2,288.923 2004 $1,027.003 $3,315.924 2002 $1,686,610.404 2003 $891,724.974 2004 $3,730,105.204 2005 $10,116.624 $6,318,557.195 2003 $4,019.335 2004 $9,378.685 2005 $125.935 $13,523.936 2002 $633,276.586 2003 $337,036.826 2004 $1,203,212.146 2005 $11,993.36
  8. 8. 6 $2,185,518.907 2002 $457,741.087 2003 $699,906.687 2004 $1,760,133.467 2005 $3,858.617 $2,921,639.838 2002 $567,255.278 2003 $655,538.228 2004 $1,971,438.578 2005 $3,983.348 $3,198,215.399 2002 $2,838,415.079 2003 $2,320,041.959 2004 $4,843,745.409 2005 $10,203.849 $10,012,406.2610 2002 $608,310.6110 2003 $769,737.4610 2004 $2,365,129.5810 2005 $4,664.6710 $3,747,842.32$32,441,341.06(46 row(s) affected)select t.CalendarQuarter, salter.SalesTerritoryCountry as SALES_REGION, ROUND(sum(final_amount), 2)AS final_amountfrom dbo.SALES_TERRITORY_DIM salter inner join dbo.sales_fact salon salter.SalesTerritoryKey = sal.TERRITORY_KEYinner join dbo.TIME_DIM ton sal.Time_ID = t.TimeKeywhere t.CalendarQuarter is not nullgroup by CUBE(t.CalendarQuarter,salter.SalesTerritoryCountry);CalendarQuarter SALES_REGION final_amount1 Australia $ 2,658,668.772 Australia $ 2,887,612.753 Australia $ 1,965,983.094 Australia $ 2,500,141.66Australia $ 10,012,406.261 Canada $ 656,549.832 Canada $ 668,041.623 Canada $ 419,995.244 Canada $ 440,932.20Canada $ 2,185,518.901 France $ 718,900.012 France $ 943,563.823 France $ 580,171.704 France $ 679,004.30France $ 2,921,639.831 Germany $ 815,826.112 Germany $ 1,058,738.813 Germany $ 580,568.724 Germany $ 743,081.76Germany $ 3,198,215.391 UnitedKingdom$ 984,242.732 UnitedKingdom$ 1,036,699.233 UnitedKingdom$ 776,231.374 UnitedKingdom$ 950,668.99United $ 3,747,842.32
  9. 9. Kingdom1 UnitedStates$ 2,441,002.122 UnitedStates$ 3,432,570.223 UnitedStates$ 1,915,369.504 UnitedStates$ 2,586,776.52UnitedStates$ 10,375,718.35$ 32,441,341.061 $ 8,275,189.572 $ 10,027,226.443 $ 6,238,319.624 $ 7,900,605.43IF OBJECT_ID (dbo.customers, V) IS NOT NULLDROP VIEW dbo.customers ;GOCREATE VIEW dbo.customers WITH ENCRYPTIONASSELECT C.CUSTOMERKEY AS CUST_NO, C.FirstName + ,+C.LASTNAME AS CUSTOMER_Name, G.City, G.PostalCode, G.StateProvinceCode AS STATE_CODE, G.StateProvinceName AS STATE_NAME, G.CountryRegionCode AS COUNTRY_CODE, G.EnglishCountryRegionName AS COUNTRY_NAMEFROM DBO.DimCustomer CINNER JOINDBO.DimGeography GON C.GeographyKey = G.GeographyKeywith checkuse AdventureWorksDWuse AdventureWorksDWselect top(40)cust_no, upper (customer_name) as Customer_Name, city, postalcode, state_code, state_name, country_name, country_code from dbo.customers;cust_no Customer_Name city postalcode state_code state_name country_name country_code11000 JON,YANG Rockhampton 4700 QLD Queensland Australia AU11001 EUGENE,HUANG Seaford 3198 VIC Victoria Australia AU11002 RUBEN,TORRES Hobart 7001 TAS Tasmania Australia AU11003 CHRISTY,ZHU North Ryde 2113 NSW New SouthWalesAustralia AU11004 ELIZABETH,JOHNSON Wollongong 2500 NSW New SouthWalesAustralia AU11005 JULIO,RUIZ EastBrisbane4169 QLD Queensland Australia AU11006 JANET,ALVAREZ Matraville 2036 NSW New SouthWalesAustralia AU11007 MARCO,MEHTA Warrnambool 3280 VIC Victoria Australia AU11008 ROB,VERHOFF Bendigo 3550 VIC Victoria Australia AU11009 SHANNON,CARLSON Hervey Bay 4655 QLD Queensland Australia AU
  10. 10. 11010 JACQUELYN,SUAREZ EastBrisbane4169 QLD Queensland Australia AU11011 CURTIS,LU EastBrisbane4169 QLD Queensland Australia AU11012 LAUREN,WALKER Bremerton 98312 WA Washington UnitedStatesUS11013 IAN,JENKINS Lebanon 97355 OR Oregon UnitedStatesUS11014 SYDNEY,BENNETT Redmond 98052 WA Washington UnitedStatesUS11015 CHLOE,YOUNG Burbank 91502 CA California UnitedStatesUS11016 WYATT,HILL ImperialBeach91932 CA California UnitedStatesUS11017 SHANNON,WANG Sunbury 3429 VIC Victoria Australia AU11018 CLARENCE,RAI Bendigo 3550 VIC Victoria Australia AU11019 LUKE,LAL Langley V3A 4R2 BC BritishColumbiaCanada CA11020 JORDAN,KING Metchosin V9 BC BritishColumbiaCanada CA11021 DESTINY,WILSON Beaverton 97005 OR Oregon UnitedStatesUS11022 ETHAN,ZHANG Bellingham 98225 WA Washington UnitedStatesUS11023 SETH,EDWARDS Bellflower 90706 CA California UnitedStatesUS11024 RUSSELL,XIE Concord 94519 CA California UnitedStatesUS11025 ALEJANDRO,BECK Hawthorne 4171 QLD Queensland Australia AU11026 HAROLD,SAI Goulburn 2580 NSW New SouthWalesAustralia AU11027 JESSIE,ZHAO Warrnambool 3280 VIC Victoria Australia AU11028 JILL,JIMENEZ St.Leonards2065 NSW New SouthWalesAustralia AU11029 JIMMY,MORENO Bendigo 3550 VIC Victoria Australia AU11030 BETHANY,YUAN Cloverdale 6105 SA SouthAustraliaAustralia AU11031 THERESA,RAMOS Matraville 2036 NSW New SouthWalesAustralia AU11032 DENISE,STONE Melbourne 3000 VIC Victoria Australia AU11033 JAIME,NATH MilsonsPoint2061 NSW New SouthWalesAustralia AU11034 EBONY,GONZALEZ NorthSydney2055 NSW New SouthWalesAustralia AU11035 WENDY,DOMINGUEZ Cranbourne 3977 VIC Victoria Australia AU11036 JENNIFER,RUSSELL NationalCity91950 CA California UnitedStatesUS11037 CHLOE,GARCIA Cliffside V8Y 1L1 BC BritishColumbiaCanada CA11038 DIANA,HERNANDEZ LavenderBay2060 NSW New SouthWalesAustralia AU
  11. 11. 11039 MARC,MARTIN Wollongong 2500 NSW New SouthWalesAustralia AUuse AdventureWorksDWIF OBJECT_ID (dbo.customers1, V) IS NOT NULLDROP VIEW dbo.customers1 ;GOCREATE VIEW dbo.customers1 WITH ENCRYPTIONASSELECT C.CUSTOMERKEY AS CUST_NO, C.FirstName + ,+C.LASTNAME AS CUSTOMER_Name, G.City, G.PostalCode, G.StateProvinceCode AS STATE_CODE, G.StateProvinceName AS STATE_NAME, G.CountryRegionCode AS COUNTRY_CODE, G.EnglishCountryRegionName AS COUNTRY_NAME,t.SalesTerritoryCountry as Sales_Territory_Country, t.SalesTerritoryGroup as Sales_Territory_Group,t.SalesTerritoryRegion as Sales_Territory_RegionFROM DBO.DimCustomer CINNER JOINDBO.DimGeography GON C.GeographyKey = G.GeographyKeyinner join dbo.DimSalesTerritory ton g.SalesTerritoryKey = t.SalesTerritoryKeywith check option;use AdventureWorksDWselect top(34) * from dbo.customers1 ;CUST_NOCUSTOMER_Name City PostalCodeSTATE_CODESTATE_NAMECOUNTRY_CODECOUNTRY_NAMESales_Territory_CountrySales_Territory_GroupSales_Territory_Region11000 Jon,Yang Rockhampton4700 QLD QueenslandAU Australia Australia Pacific Australia11001 Eugene,Huang Seaford 3198 VIC Victoria AU Australia Australia Pacific Australia11002 Ruben,Torres Hobart 7001 TAS Tasmania AU Australia Australia Pacific Australia11003 Christy,Zhu North Ryde 2113 NSW New SouthWalesAU Australia Australia Pacific Australia11004 Elizabeth,JohnsonWollongong 2500 NSW New SouthWalesAU Australia Australia Pacific Australia11005 Julio,Ruiz EastBrisbane4169 QLD QueenslandAU Australia Australia Pacific Australia11006 Janet,Alvarez Matraville 2036 NSW New SouthWalesAU Australia Australia Pacific Australia11007 Marco,Mehta Warrnambool3280 VIC Victoria AU Australia Australia Pacific Australia11008 Rob,Verhoff Bendigo 3550 VIC Victoria AU Australia Australia Pacific Australia11009 Shannon,Carlson Hervey Bay 4655 QLD QueenslandAU Australia Australia Pacific Australia11010 Jacquelyn,Suarez EastBrisbane4169 QLD QueenslandAU Australia Australia Pacific Australia11011 Curtis,Lu EastBrisbane4169 QLD QueenslandAU Australia Australia Pacific Australia11012 Lauren,Walker Bremerton 98312 WA WashingtonUS UnitedStatesUnited States North America Northwest11013 Ian,Jenkins Lebanon 97355 OR Oregon US UnitedStatesUnited States North America Northwest11014 Sydney,Bennett Redmond 98052 WA WashingtonUS UnitedStatesUnited States North America Northwest11015 Chloe,Young Burbank 91502 CA CaliforniaUS UnitedStatesUnited States North America Southwest11016 Wyatt,Hill ImperialBeach91932 CA CaliforniaUS UnitedStatesUnited States North America Southwest11017 Shannon,Wang Sunbury 3429 VIC Victoria AU Australia Australia Pacific Australia11018 Clarence,Rai Bendigo 3550 VIC Victoria AU Australia Australia Pacific Australia11019 Luke,Lal Langley V3A 4R2 BC BritishColumbiaCA Canada Canada North America Canada11020 Jordan,King Metchosin V9 BC BritishColumbiaCA Canada Canada North America Canada11021 Destiny,Wilson Beaverton 97005 OR Oregon US UnitedStatesUnited States North America Northwest11022 Ethan,Zhang Bellingham 98225 WA WashingtonUS UnitedStatesUnited States North America Northwest
  12. 12. 11023 Seth,Edwards Bellflower 90706 CA CaliforniaUS UnitedStatesUnited States North America Southwest11024 Russell,Xie Concord 94519 CA CaliforniaUS UnitedStatesUnited States North America Southwest11025 Alejandro,Beck Hawthorne 4171 QLD QueenslandAU Australia Australia Pacific Australia11026 Harold,Sai Goulburn 2580 NSW New SouthWalesAU Australia Australia Pacific Australia11027 Jessie,Zhao Warrnambool3280 VIC Victoria AU Australia Australia Pacific Australia11028 Jill,Jimenez St.Leonards2065 NSW New SouthWalesAU Australia Australia Pacific Australia11029 Jimmy,Moreno Bendigo 3550 VIC Victoria AU Australia Australia Pacific Australia11030 Bethany,Yuan Cloverdale 6105 SA SouthAustraliaAU Australia Australia Pacific Australia11031 Theresa,Ramos Matraville 2036 NSW New SouthWalesAU Australia Australia Pacific Australia11032 Denise,Stone Melbourne 3000 VIC Victoria AU Australia Australia Pacific Australia11033 Jaime,Nath MilsonsPoint2061 NSW New SouthWalesAU Australia Australia Pacific Australiaselect c.CustomerKey as cust_no, c.FirstName + , + c.lastname as customer_name, f.SalesOrderNumber,f.UnitPrice, f.Freight, f.OrderQuantity, f.DiscountAmount, f.UnitPriceDiscountPct, f.UnitPrice * f.OrderQuantity + f.Freight - f.UnitPriceDiscountPct as Final_Amount, f.ProductKeyinto customer_recordsfrom FactInternetSales f right outer joindbo.DimCustomer conc.CustomerKey = f.CustomerKey(60398 row(s) affected)use AdventureWorksDWselect top(15)cust_no, customer_name, SalesOrderNumber,UnitPrice,Freight, OrderQuantity, DiscountAmount,UnitPriceDiscountPct, Final_Amount,ProductKeyfrom customer_recordscust_no customer_name SalesOrderNumber UnitPrice Freight OrderQuantity DiscountAmount UnitPriceDiscountPct Final_Amount ProductKey11003 Christy,Zhu SO43701 $3,399.99 $85.00 1 $0.00 $0.00 $3,484.99 34614501 Ruben,Prasad SO43700 $699.10 $17.48 1 $0.00 $0.00 $716.58 33621768 Cole,Watson SO43697 $3,578.27 $89.46 1 $0.00 $0.00 $3,667.73 31025863 Sydney,Wright SO43699 $3,399.99 $85.00 1 $0.00 $0.00 $3,484.99 34628389 Rachael,Martinez SO43698 $3,399.99 $85.00 1 $0.00 $0.00 $3,484.99 34611005 Julio,Ruiz SO43704 $3,374.99 $84.37 1 $0.00 $0.00 $3,459.36 35111011 Curtis,Lu SO43705 $3,399.99 $85.00 1 $0.00 $0.00 $3,484.99 34416624 Albert,Alvarez SO43703 $3,578.27 $89.46 1 $0.00 $0.00 $3,667.73 31027645 Colin,Anand SO43702 $3,578.27 $89.46 1 $0.00 $0.00 $3,667.73 31116351 Martha,Xu SO43709 $3,578.27 $89.46 1 $0.00 $0.00 $3,667.73 31316517 Katrina,Raji SO43710 $3,578.27 $89.46 1 $0.00 $0.00 $3,667.73 31420042 Brad,Deng SO43708 $699.10 $17.48 1 $0.00 $0.00 $716.58 33027616 Emma,Brown SO43707 $3,578.27 $89.46 1 $0.00 $0.00 $3,667.73 31227621 Edward,Brown SO43706 $3,578.27 $89.46 1 $0.00 $0.00 $3,667.73 31213513 Abigail,Henderson SO43712 $3,578.27 $89.46 1 $0.00 $0.00 $3,667.73 311USE AdventureWorksSELECTTOP(20)C.ContactID ,C.Title, C.FirstName + .+ C.MiddleName +. +C.LastName AS EMPLOYEE, SUBSTRING(C.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(C.Phone,-7,11)AS AREA_CODE, C.Phone,E.Title as Job_Title, E.Gender,CONVERT(DATE, E.BirthDate)AS BIRTH_DAY, DATEDIFF(YY, E.BirthDate, CURRENT_TIMESTAMP) AS AGE, CONVERT (DATE,E.HireDate) AS HIRE_DATE,DATEDIFF(YY, E.HIREDATE, CURRENT_TIMESTAMP) AS NUM_YERS_EMPLOYEED, E.ManagerID, (E.EMPLOYEEID) AS EMP_IDFROM
  13. 13. Person.Contact CINNER JOIN HumanResources.Employee EON C.ContactID = E .ContactIDContactID Title EMPLOYEE PHONE_NUMBER AREA_CODE Phone Job_Title Gender BIRTH_DAY AGE HIRE_DATE NUM_YERS_EMPLOYEEDManagerIDEMP_ID1209 Mr Guy.R.Gilbert 555-0195 320 320-555-0195ProductionTechnician -WC60M 5/15/1972 39 7/31/1996 15 16 11030 Mr Kevin.F.Brown 555-0189 150 150-555-0189MarketingAssistantM 6/3/1977 34 2/26/1997 14 6 21002 555-0187 212 212-555-0187EngineeringManagerM 12/13/1964 47 12/12/199714 12 31290 555-0100 612 612-555-0100Senior ToolDesignerM 1/23/1965 46 1/5/1998 13 3 41009 Mr Thierry.B.DHers 555-0183 168 168-555-0183ToolDesignerM 8/29/1949 62 1/11/1998 13 263 51028 Mr David.M.Bradley 555-0172 913 913-555-0172MarketingManagerM 4/19/1965 46 1/20/1998 13 109 61070 Ms JoLynn.M.Dobney 555-0145 903 903-555-0145ProductionSupervisor -WC60F 2/16/1946 65 1/26/1998 13 21 71071 Mrs Ruth.Ann.Ellerbrock 555-0130 145 145-555-0130ProductionTechnician -WC10F 7/6/1946 65 2/6/1998 13 185 81005 Mrs Gail.A.Erickson 555-0139 849 849-555-0139DesignEngineerF 10/29/1942 69 2/6/1998 13 3 91076 Mr Barry.K.Johnson 555-0180 206 206-555-0180ProductionTechnician -WC10M 4/27/1946 65 2/7/1998 13 185 101006 Mr Jossef.H.Goldberg 555-0189 122 122-555-0189DesignEngineerM 4/11/1949 62 2/24/1998 13 3 111001 Ms Terri.Lee.Duffy 555-0175 819 819-555-0175VicePresident ofEngineeringF 9/1/1961 50 3/3/1998 13 109 121072 Mr Sidney.M.Higa 555-0189 424 424-555-0189ProductionTechnician -WC10M 10/1/1946 65 3/5/1998 13 185 131067 Mr Taylor.R.Maxwell 555-0165 508 508-555-0165ProductionSupervisor -WC50M 5/3/1946 65 3/11/1998 13 21 141073 Mr Jeffrey.L.Ford 555-0185 984 984-555-0185ProductionTechnician -WC10M 8/12/1946 65 3/23/1998 13 185 151068 Ms Jo.A.Brown 555-0129 632 632-555-0129ProductionSupervisor -WC60F 11/9/1946 65 3/30/1998 13 21 16
  14. 14. 1074 Mrs Doris.M.Hartwig 555-0150 328 328-555-0150ProductionTechnician -WC10F 5/6/1946 65 4/11/1998 13 185 171069 Mr John.T.Campbell 555-0113 435 435-555-0113ProductionSupervisor -WC60M 9/8/1946 65 4/18/1998 13 21 181075 Mrs Diane.R.Glimp 555-0151 202 202-555-0151ProductionTechnician -WC10F 4/30/1946 65 4/29/1998 13 185 191129 Mr Steven.T.Selikoff 555-0114 925 925-555-0114ProductionTechnician -WC30M 6/15/1967 44 1/2/1999 12 173 20iF OBJECT_ID (EMP_1, V) IS NOT NULLDROP VIEW EMP_1 ;GOCREATE VIEW EMP_1 WITH SCHEMABINDINGASSELECT C.ContactID ,C.Title, C.FirstName + .+ C.MiddleName +. +C.LastName AS EMPLOYEE, SUBSTRING(C.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(C.Phone,-7,11)AS AREA_CODE, C.Phone,E.Title AS JOB_TITLE, E.Gender,CONVERT(DATE, E.BirthDate)AS BIRTH_DAY, DATEDIFF(YY, E.BirthDate, CURRENT_TIMESTAMP) AS AGE, CONVERT (DATE,E.HireDate) AS HIRE_DATE,DATEDIFF(YY, E.HIREDATE, CURRENT_TIMESTAMP) AS NUM_YERS_EMPLOYEED, E.ManagerID, E.MaritalStatus, (E.EMPLOYEEID) AS EMP_IDFROM PERSON.Contact CINNER JOINHumanResources.Employee EONC.ContactID = E.ContactIDWHERE C.FirstName + .+ C.MiddleName +. +C.LastName IS NOT NULLAND C.CONTACTID NOT IN (1025, 1022, 1023, 1024)WITH CHECK OPTIONUPDATE dbo.EMP_1SET TITLE = Mrwhere gender = M/****** Script for SelectTopNRows command from SSMS ******/SELECT TOP 12 [ContactID],[Title],[EMPLOYEE],[PHONE_NUMBER],[AREA_CODE],[Phone],[JOB_TITLE],[Gender],[BIRTH_DAY],[AGE],[HIRE_DATE],[NUM_YERS_EMPLOYEED],[ManagerID],[MaritalStatus],[EMP_ID]FROM [AdventureWorks].[dbo].[EMP_1]WHERE GENDER = M(12 row(s) affected)
  15. 15. UPDATE dbo.EMP_1SET TITLE = MrsOUTPUT DELETED.TITLE AS OLDTITLE, INSERTED.TITLE AS NEWTITLEwhere Gender = FAND MaritalStatus = MOLDTITLE NEWTITLE-------- --------NULL MrsNULL MrsMs. MrsMs. MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsMs. MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsContactID Title EMPLOYEE PHONE_NUMBER AREA_CODE Phone JOB_TITLE Gender BIRTH_DAY AGE HIRE_DATE NUM_YERS_EMPLOYEED ManagerID MaritalStatus EMP_ID1209 Mr Guy.R.Gilbert 555-0195 320 320-555-0195ProductionTechnician -WC60M 5/15/1972 39 7/31/1996 15 16 M 11030 Mr Kevin.F.Brown 555-0189 150 150-555-0189MarketingAssistantM 6/3/1977 34 2/26/1997 14 6 S 21009 Mr Thierry.B.DHers 555-0183 168 168-555-0183Tool Designer M 8/29/1949 62 1/11/1998 13 263 M 51028 Mr David.M.Bradley 555-0172 913 913-555-0172MarketingManagerM 4/19/1965 46 1/20/1998 13 109 S 61076 Mr Barry.K.Johnson 555-0180 206 206-555-0180ProductionTechnician -WC10M 4/27/1946 65 2/7/1998 13 185 S 101006 Mr Jossef.H.Goldberg 555-0189 122 122-555-0189DesignEngineerM 4/11/1949 62 2/24/1998 13 3 M 111072 Mr Sidney.M.Higa 555-0189 424 424-555-0189ProductionTechnician -WC10M 10/1/1946 65 3/5/1998 13 185 M 131067 Mr Taylor.R.Maxwell 555-0165 508 508-555-0165ProductionSupervisor -WC50M 5/3/1946 65 3/11/1998 13 21 M 141073 Mr Jeffrey.L.Ford 555-0185 984 984-555-0185ProductionTechnician -WC10M 8/12/1946 65 3/23/1998 13 185 S 151069 Mr John.T.Campbell 555-0113 435 435-555-0113ProductionSupervisor -WC60M 9/8/1946 65 4/18/1998 13 21 M 181129 Mr Steven.T.Selikoff 555-0114 925 925-555-0114ProductionTechnician -WC30M 6/15/1967 44 1/2/1999 12 173 M 201231 Mr Peter.J.Krebs 555-0196 913 913-555-0196ProductionControlManagerM 12/4/1972 39 1/2/1999 12 148 M 21
  16. 16. NULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL MrsNULL Mrs(79 row(s) affected)UPDATE dbo.EMP_1SET TITLE = MsOUTPUT DELETED.TITLE AS OLDTITLE, INSERTED.TITLE AS NEWTITLEwhere MaritalStatus = SAND GENDER = FOLDTITLE NEWTITLE-------- --------Ms MsMs MsMs MsMs MsMs MsMs MsMs MsMs MsMs MsMs MsMs MsMs MsMs MsMs MsMs MsMs Ms
  17. 17. Ms MsMs MsMs MsMs MsMs MsMs MsMs MsMs MsMs MsMs MsMs MsMs MsMs MsMs MsMs MsMs Ms(32 row(s) affected)SELECT TOP(30)CONTACTID, GENDER, MARITALSTATUS, TITLE, EMPLOYEEFROM dbo.EMP_1CONTACTID GENDER MARITALSTATUS TITLE EMPLOYEE1209 M M Mr Guy.R.Gilbert1030 M S Mr Kevin.F.Brown1009 M M Mr Thierry.B.DHers1028 M S Mr David.M.Bradley1070 F S Ms JoLynn.M.Dobney1071 F M Mrs Ruth.Ann.Ellerbrock1005 F M Mrs Gail.A.Erickson1076 M S Mr Barry.K.Johnson1006 M M Mr Jossef.H.Goldberg1001 F S Ms Terri.Lee.Duffy1072 M M Mr Sidney.M.Higa1067 M M Mr Taylor.R.Maxwell1073 M S Mr Jeffrey.L.Ford1068 F S Ms Jo.A.Brown1074 F M Mrs Doris.M.Hartwig1069 M M Mr John.T.Campbell1075 F M Mrs Diane.R.Glimp1129 M M Mr Steven.T.Selikoff1231 M M Mr Peter.J.Krebs1172 M S Mr Stuart.V.Munson1173 M S Mr Greg.F.Alderson1113 M S Mr David.N.Johnson1054 M S Mr Zheng.W.Mu1097 M M Mr Ivo.William.Salmre1156 M S Mr Paul.B.Komosinski1258 M S Mr Ashvini.R.Sharma1199 M M Mr Kendall.C.Keil1242 F M Mrs Paula.M.Barreto deMattos1140 M S Mr Alejandro.E.McGuel1122 M S Mr Garrett.R.Young(30 row(s) affected)USE MSSQLCREATE TABLE CUSTOMER(CUST_NO INT IDENTITY(02348, 112) CONSTRAINT CUSTCUST_NO_PK PRIMARY KEY, FIRST_NAME NVARCHAR(33) CONSTRAINT CUSTFNAME_NN NOT NULL, LAST_NAME NVARCHAR(25) CONSTRAINT CUSTLASTNAME_NN NOT NULL,GENDER CHAR(10) CONSTRAINT CUSTGENDER_NN NOT NULL,CUST_ADDRESS NVARCHAR(55) CONSTRAINT CUSTCUST_ADD_NN NOT NULL, CITY NVARCHAR(40) CONSTRAINT CUSTCITY_NN NOT NULL, CUST_STATE NVARCHAR(25) CONSTRAINT CUSTSTATE_CK CHECK (CUST_STATE iN(IL, DC, CA, TX, AZ, MD, GA, il,dc,ca,tx,az, md,ga)), ZIP_CODE INT)
  18. 18. USE MSSQLinsert into CUSTOMER(FIRST_NAME, LAST_NAME, CUST_ADDRESS , city, CUST_STATE, ZIP_CODE, GENDER)values (Jackie, Brown, 1818 S Washington BLVD SE APT 227, Washington, DC, 20021, Female);CREATE TRIGGER CUSTOMERSONdbo.customerinstead of insertas printThe data has been sucessfully inserted to the database.;insert into CUSTOMER(FIRST_NAME, LAST_NAME, CUST_ADDRESS , city, CUST_STATE, ZIP_CODE, GENDER)values (Sandy, Jack, 4040 N Washington BLVD SW , Washington, DC,20032, Male),(Christopher, McLean, 24 N.Illinios Ave NE, Washington, DC,20011, Male),(RoseMary, BlueGrass, 20 S Capitol RD SE, Washington, DC,20018, Female),(Cassandra, Morgan, 1010 W New York Ave NW , Washington, DC,20030, Female),(Diana, Mark, 85 S A ST SW, Washington, DC,20059, Female);The data has been sucessfully inserted to the database.(5 row(s) affected)insert into CUSTOMER(FIRST_NAME, LAST_NAME, CUST_ADDRESS , city, CUST_STATE, ZIP_CODE, GENDER)values (Jayson, Alex, 1830 S Cider BLVD NW, Washington, DC, 20032, Male),(JAEGERS, SHU, 40 S Washington BLVD SW , Washington, DC, 20011, Female),(Nicholas, Nicholas, 24 N California Ave NE Apt 4D, Washington, DC,20018, Male),(Liz, Steward, 120 S Ohio Ave SE, Washington, DC,20022, Female),(Austin, Martin, 11 W New Mexico Ave NW APT 21B, Washington, DC,20030, female),(LOPEZ, PALISBO, 85 S A ST SW, Washington, DC,20059, Male);The data has been sucessfully inserted to the database.(6 row(s) affected)insert into CUSTOMER(FIRST_NAME, LAST_NAME, CUST_ADDRESS , city, CUST_STATE, ZIP_CODE, GENDER)values (DEIDRA, SYDNEY, 24 18th ST SE , Washington, DC,20017, Female),(EAGLEN, EASTON, 120 S Jackson Ave SE, Washington,DC,20022, Female),(SHANDRA, TABOR, 101 W Georgia Ave NW , Washington, DC,20040, female),(DOROTHY, Paul-Walker, 35 E Delaware Ave SE, Washington, DC,20052, female),(DEINES, JOHN, 11 W Georgia Ave, Silver Springs, MD,20901 , Female),(WALTER, TIMOTHY, 35 E Pine Grove Ave NE, Washington, DC, 20052, Male),(JONATHAN, BRUCE, 10 justice Drive SW , Washington, DC, 20040, Male);The data has been sucessfully inserted to the database.(7 row(s) affected)insert into CUSTOMER(FIRST_NAME, LAST_NAME, CUST_ADDRESS , city, CUST_STATE, ZIP_CODE,GENDER)values (MILTON, ROLAND, 35 W Union Drive NE, Washington, DC,20051,Male);The data has been sucessfully inserted to the database.(1 row(s) affected)USE MSSQLSELECT * FROM CUSTOMERCUST_NO FIRST_NAME LAST_NAME GENDER CUST_ADDRESS CITYCUST_STATE ZIP_CODE----------- --------------------------------- ------------------------- ---------- ------------------------------------------------------- ---------------------------------------- ------------------------- -----------
  19. 19. 2348 Jackie Brown Female 1818 S Washington BLVD SE APT 227 WashingtonDC 20021(1 row(s) affected)insert into CUSTOMER(FIRST_NAME, LAST_NAME, CUST_ADDRESS , city, CUST_STATE, ZIP_CODE, GENDER)values (Sandy, Jack, 4040 N Washington BLVD SW , Washington, DC,20032, Male),(Christopher, McLean, 24 N.Illinios Ave NE, Washington, DC,20011, Male),(RoseMary, BlueGrass, 20 S Capitol RD SE, Washington, DC,20018, Female),(Cassandra, Morgan, 1010 W New York Ave NW , Washington, DC,20030, Female),(Diana, Mark, 85 S A ST SW, Washington, DC,20059, Female);insert into CUSTOMER(FIRST_NAME, LAST_NAME, CUST_ADDRESS , city, CUST_STATE, ZIP_CODE, GENDER)values (Jayson, Alex, 1830 S Cider BLVD NW, Washington, DC, 20032, Male),(JAEGERS, SHU, 40 S Washington BLVD SW , Washington, DC, 20011, Female),(Nicholas, Nicholas, 24 N California Ave NE Apt 4D, Washington, DC,20018, Male),(Liz, Steward, 120 S Ohio Ave SE, Washington, DC,20022, Female),(Austin, Martin, 11 W New Mexico Ave NW APT 21B, Washington, DC,20030, female),(LOPEZ, PALISBO, 85 S A ST SW, Washington, DC,20059, Male);insert into CUSTOMER(FIRST_NAME, LAST_NAME, CUST_ADDRESS , city, CUST_STATE, ZIP_CODE,GENDER)values (MILTON, ROLAND, 35 W Union Drive NE, Washington, DC,20051,Male);insert into CUSTOMER(FIRST_NAME, LAST_NAME, CUST_ADDRESS , city, CUST_STATE, ZIP_CODE,GENDER)values (FREDDIE, WADE, 2020 Rosevelt BLVD SW, Washington, DC,20049, Male),(ZADRA, WALCUTT, 25 E Balitmore Pk, Silver Springs, MD, 20901 , Female),(TERRENCE, KENT, 101 W Georgia Ave Apt 18B, Silver Springs, MD,20901, Male),(PHYLLIS, SERGIO, 45 Steveson Drv Apt 5G,Silver Springs, MD,20901, Female),(WALCH, GENE, 80 Baker BLVD, Silver Springs, DC,20049, Male);insert into CUSTOMER(FIRST_NAME, LAST_NAME, CUST_ADDRESS , city, CUST_STATE, ZIP_CODE,GENDER)values (BRETT, WALCUTT, 25 E Balitmore Pk, Silver Springs, MD, 20901 , Female),(LABEAU, KENT, 50 W Ocean Drive Ave Apt 18B, Silver Springs, MD,20901, Male),(SARA, LABBADIA, 45 Steveson Drv Apt 5G,Silver Springs, MD,20901, Female),(ANDREA, LESTER, 12 St.Paul St, Oxon Hill , MD, 20745 , Female),(CHARLIE, Choice, 21 Adams BLVD, Oxon Hill, MD,20745, Male),(REGINALD, ANGEL, 25 E Washington Pk, Oxon Hil, MD, 20745 , Male);SELECT * FROM CUSTOMERCUST_NO FIRST_NAME LAST_NAME GENDER CUST_ADDRESS CITY CUST_STATE ZIP_CODE2348 Jackie Brown Female 1818 S Washington BLVD SE APT 227 Washington DC 200212460 Sandy Jack Male 4040 N Washington BLVD SW Washington DC 200322572 Christopher McLean Male 24 N.Illinios Ave NE Washington DC 200112684 RoseMary BlueGrass Female 20 S Capitol RD SE Washington DC 200182796 Cassandra Morgan Female 1010 W New York Ave NW Washington DC 200302908 Diana Mark Female 85 S A ST SW Washington DC 200593020 Jayson Alex Male 1830 S Cider BLVD NW Washington DC 200323132 JAEGERS SHU Female 40 S Washington BLVD SW Washington DC 200113244 Nicholas Nicholas Male 24 N California Ave NE Apt 4D Washington DC 200183356 Liz Steward Female 120 S Ohio Ave SE Washington DC 200223468 Austin Martin Female 11 W New Mexico Ave NW APT 21B Washington DC 200303580 LOPEZ PALISBO Male 85 S A ST SW Washington DC 200594476 MILTON ROLAND Male 35 W Union Drive NE Washington DC 20051
  20. 20. 4588 FREDDIE WADE Male 2020 Rosevelt BLVD SW Washington DC 200494700 ZADRA WALCUTT Female 25 E Balitmore Pk Silver Springs MD 209014812 TERRENCE KENT Male 101 W Georgia Ave Apt 18B Silver Springs MD 209014924 PHYLLIS SERGIO Female 45 Steveson Drv Apt 5G Silver Springs MD 209015036 WALCH GENE Male 80 Baker BLVD Silver Springs DC 200495148 BRETT WALCUTT Female 25 E Balitmore Pk Silver Springs MD 209015260 LABEAU KENT Male 50 W Ocean Drive Ave Apt 18B Silver Springs MD 209015372 SARA LABBADIA Female 45 Steveson Drv Apt 5G Silver Springs MD 209015484 ANDREA LESTER Female 12 St.Paul St Oxon Hill MD 207455596 CHARLIE Choice Male 21 Adams BLVD Oxon Hill MD 207455708 REGINALD ANGEL Male 25 E Washington Pk Oxon Hil MD 20745DELETE DBO.CUSTOMER;ALTER TRIGGER [dbo].[CUSTOMERS]ON[dbo].[CUSTOMER]AFTER INSERTas printDATA ERROR PLEASE RE-INSERT;ROLLBACK TRAN;USE [MSSQL]GOinsert into CUSTOMER(FIRST_NAME, LAST_NAME, CUST_ADDRESS , city, CUST_STATE, ZIP_CODE,GENDER)values(washington, Wells, 20 Lakeshore Driv ApT 34C, Chicago, IL, 60660, Male),(TED, SERGIO, 1818 N Paulina,Chicago, IL,60632, Male),(JULIA, QUNICY, 8O Chicago RD Apt 227, Chicago, IL,60633, Female),(EMILY, KELLY, 12 S Kedize Apt 33GH, Chicago, IL, 606421 , Female),(CLAYTON, Cliford, 20 Lasalle Street Apt 40b , Chicago, IL, 60602, Male),(RUBY, JESSIE, 2020 S State Street,Chicago, IL, 60639, Female),(CLAUDE, JAMIE, 12 S Dearbon Apt 29 H, Chicago, IL, 606421 , Male),(MARILYN, FERNANDO, 12 Jane Adams Blvd, Chicago, IL, 60604, Female),(NEIL, PAVLIKOWSKI, 20 N State Street,Chicago, IL, 60639, Female),(PAVLICK, JAVIER, 200 W Howard Ave Apt 40b , Chicago, IL, 606003, Male),(LOUISE, JESSIE, 40 S Abbott Street Apt 18H,Chicago, IL, 60629, Female);DATA ERROR PLEASE RE-INSERTMsg 3609, Level 16, State 1, Line 2The transaction ended in the trigger. The batch has been aborted.SELECT * FROM CUSTOMER;NO ROWSDROP TRIGGER CUSTOMERS;insert into CUSTOMER(FIRST_NAME, LAST_NAME, CUST_ADDRESS , city, CUST_STATE, ZIP_CODE,GENDER)values(washington, Wells, 20 Lakeshore Driv ApT 34C, Chicago, IL, 60660, Male),(TED, SERGIO, 1818 N Paulina,Chicago, IL,60632, Male),(JULIA, QUNICY, 8O Chicago RD Apt 227, Chicago, IL,60633, Female),(EMILY, KELLY, 12 S Kedize Apt 33GH, Chicago, IL, 606421 , Female),(CLAYTON, Cliford, 20 Lasalle Street Apt 40b , Chicago, IL, 60602, Male),(RUBY, JESSIE, 2020 S State Street,Chicago, IL, 60639, Female),(CLAUDE, JAMIE, 12 S Dearbon Apt 29 H, Chicago, IL, 606421 , Male),(MARILYN, FERNANDO, 12 Jane Adams Blvd, Chicago, IL, 60604, Female),(NEIL, PAVLIKOWSKI, 20 N State Street,Chicago, IL, 60639, Female),(PAVLICK, JAVIER, 200 W Howard Ave Apt 40b , Chicago, IL, 606003, Male),(LOUISE, JESSIE, 40 S Abbott Street Apt 18H,Chicago, IL, 60629, Female);
  21. 21. insert into CUSTOMER(FIRST_NAME, LAST_NAME, CUST_ADDRESS , city, CUST_STATE, ZIP_CODE, GENDER)OUTPUT INSERTED.*values (Sandy, Jack, 4040 N Washington BLVD SW , Washington, DC,20032, Male),(Christopher, McLean, 24 N.Illinios Ave NE, Washington, DC,20011, Male),(RoseMary, BlueGrass, 20 S Capitol RD SE, Washington, DC,20018, Female),(Cassandra, Morgan, 1010 W New York Ave NW , Washington, DC,20030, Female),(Diana, Mark, 85 S A ST SW, Washington, DC,20059, Female);CUST_NO FIRST_NAME LAST_NAME GENDER CUST_ADDRESS CITYCUST_STATE ZIP_CODE----------- --------------------------------- ------------------------- ---------- ------------------------------------------------------- ---------------------------------------- ------------------------- -----------10860 Sandy Jack Male 4040 N Washington BLVD SW WashingtonDC 2003210972 Christopher McLean Male 24 N.Illinios Ave NE WashingtonDC 2001111084 RoseMary BlueGrass Female 20 S Capitol RD SE WashingtonDC 2001811196 Cassandra Morgan Female 1010 W New York Ave NW WashingtonDC 2003011308 Diana Mark Female 85 S A ST SW WashingtonDC 20059(5 row(s) affected)insert into CUSTOMER(FIRST_NAME, LAST_NAME, CUST_ADDRESS , city, CUST_STATE, ZIP_CODE, GENDER)OUTPUT INSERTED.*values (Jayson, Alex, 1830 S Cider BLVD NW, Washington, DC, 20032, Male),(JAEGERS, SHU, 40 S Washington BLVD SW , Washington, DC, 20011, Female),(Nicholas, Nicholas, 24 N California Ave NE Apt 4D, Washington, DC,20018, Male),(Liz, Steward, 120 S Ohio Ave SE, Washington, DC,20022, Female),(Austin, Martin, 11 W New Mexico Ave NW APT 21B, Washington, DC,20030, female),(LOPEZ, PALISBO, 85 S A ST SW, Washington, DC,20059, Male);CUST_NO FIRST_NAME LAST_NAME GENDER CUST_ADDRESS CITYCUST_STATE ZIP_CODE----------- --------------------------------- ------------------------- ---------- ------------------------------------------------------- ---------------------------------------- ------------------------- -----------11420 Jayson Alex Male 1830 S Cider BLVD NW WashingtonDC 2003211532 JAEGERS SHU Female 40 S Washington BLVD SW WashingtonDC 2001111644 Nicholas Nicholas Male 24 N California Ave NE Apt 4D WashingtonDC 2001811756 Liz Steward Female 120 S Ohio Ave SE WashingtonDC 2002211868 Austin Martin female 11 W New Mexico Ave NW APT 21B WashingtonDC 2003011980 LOPEZ PALISBO Male 85 S A ST SW WashingtonDC 20059(6 row(s) affected)insert into CUSTOMER(FIRST_NAME, LAST_NAME, CUST_ADDRESS , city, CUST_STATE, ZIP_CODE,GENDER)OUTPUT INSERTED.*values (MILTON, ROLAND, 35 W Union Drive NE, Washington, DC,20051,Male),(FREDDIE, WADE, 2020 Rosevelt BLVD SW, Washington, DC,20049, Male),(ZADRA, WALCUTT, 25 E Balitmore Pk, Silver Springs, MD, 20901 , Female),(TERRENCE, KENT, 101 W Georgia Ave Apt 18B, Silver Springs, MD,20901, Male),(PHYLLIS, SERGIO, 45 Steveson Drv Apt 5G,Silver Springs, MD,20901, Female),(WALCH, GENE, 80 Baker BLVD, Silver Springs, DC,20049, Male),(BRETT, WALCUTT, 25 E Balitmore Pk, Silver Springs, MD, 20901 , Female),(LABEAU, KENT, 50 W Ocean Drive Ave Apt 18B, Silver Springs, MD,20901, Male),(SARA, LABBADIA, 45 Steveson Drv Apt 5G,Silver Springs, MD,20901, Female);CUST_NO FIRST_NAME LAST_NAME GENDER CUST_ADDRESS CITYCUST_STATE ZIP_CODE----------- --------------------------------- ------------------------- ---------- ------------------------------------------------------- ---------------------------------------- ------------------------- -----------12092 MILTON ROLAND Male 35 W Union Drive NE WashingtonDC 2005112204 FREDDIE WADE Male 2020 Rosevelt BLVD SW WashingtonDC 2004912316 ZADRA WALCUTT Female 25 E Balitmore Pk Silver SpringsMD 2090112428 TERRENCE KENT Male 101 W Georgia Ave Apt 18B Silver SpringsMD 20901
  22. 22. 12540 PHYLLIS SERGIO Female 45 Steveson Drv Apt 5G Silver SpringsMD 2090112652 WALCH GENE Male 80 Baker BLVD Silver SpringsDC 2004912764 BRETT WALCUTT Female 25 E Balitmore Pk Silver SpringsMD 2090112876 LABEAU KENT Male 50 W Ocean Drive Ave Apt 18B Silver SpringsMD 2090112988 SARA LABBADIA Female 45 Steveson Drv Apt 5G Silver SpringsMD 20901ALTER TRIGGER dbo.CUSTOMERSONdbo.CUSTOMERAFTER UPDATEASSELECT last_name + + record has been updated from inserted;GOUPDATE DBO.CUSTOMERSET LAST_NAME = RichardsonWHERE cust_no = 9852Richardson record has been updated(1 row(s) affected)insert into CUSTOMER(FIRST_NAME, LAST_NAME, CUST_ADDRESS , city, CUST_STATE, ZIP_CODE,GENDER)values (Derick, Rose, 12 S Racine Ave Apt 29 H, Chicago, IL, 606429 , Male),(Ralph, Nadim, 204 Kimball Rd Apt 20G, Chicago, IL, 60624, Male),(Latoya, Kadeem, 11 Addison Apt 20c , Chicago, IL, 60624, Female),(lisa, Brad, 80 West 83rd Apt 2 , Chicago, IL, 60654, Female),(Kadyn, Michaels, 200 East 73rd Apt 2 , Chicago, IL, 60624, Female),(Latrice, Abby, 40 Glenwood Aot 23, Chicago, IL, 60632, Female),(Thomas, Martin, 12 N Javis Apt 9 H3, Evaston, IL, 606721 , Male),(Bernice, Authur, 19 S Highway 6 Apt 3, Evaston, IL, 60704, Female),(Betsy, Chris, 20 N Loyola Street,Evaston, IL, 60639, Female),(Akon, Cider, 200 W Brodway Ave Apt 40 , Evaston, IL, 60704, Male),(Richard, King, 40 S Theather Street Apt 1, Evaston, IL, 60704, Female),(Derick, Stone, 12 S Race Ave Apt 29 H, Evaston, IL, 60704 , Male),(Mark, Harison, 1010 S Centinela Ave Apt 2B, Los Angles , CA, 90034 , Male),(TINA, Clark, 20 N SunSet Blvd 2C,Los Angeles, CA, 90034, Female),(MARILYN, Wash, 12 King Drv, Los Angeles, CA, 90034, Female),(MARILYN, Wash, 12 King Drv, Los Angeles, CA, 90034, Female),(Hamilton, Anderson, 050 N Ocean Drv, Los Angeles,CA, 90034, Female),(CHAD, ALFRED, 090 S liberity PK, Los Angeles,CA, 90034, Male),(Jones, Don, 4040 S Central ,Los Angeles,CA, 90034, Male),(Britney, Hancock, 90 S Hollywood, Los Angeles,CA, 90034, Female),(Geroge, Jefferson, 40 S Empire Drv ,Los Angeles,CA, 90034, Male),(Madilyn, Jaken, 10 Beach Drive, Los Angeles,CA, 90035, Female),(Chuck, Chi, 30 Mall Drive , Los Angeles,CA, 90035, Male),(Cesar, Carlos, 40 S Central ,Los Angeles,CA, 90034, Male),(Nadine, Corby, 19 Pine Lane , Los Angeles,CA, 90033, Female),(Abraham, Abraham, 40 S Airforce Rd ,Los Angeles ,CA, 90034, Male),(udo, udo, 21 E Disney Rd ,Los Angeles,CA, 90034, Male),(Octavia, Lee, 10 Santa Monica Drive, Los Angeles,CA, 90035, Female),(Abel, Adon, 30 santa jose drv , Los Angeles,CA, 90035, Male),(Nancy, William, 40 S lake Rd ,Los Angeles,CA, 90034, Male),(Monica, Corby, 19 Cider Lane , Los Angeles,CA, 90033, Female),(Chris, Abraham, 40 S Airforce Rd ,Los Angeles,CA, 90034, Male),(Diane, Austine, 90 Sony Drive , Los Angeles,CA, 90035, Female),(Darlene, Dion, 92 Church Blvd, Los Angeles,CA, 90035, Female),(Linda, Creed, 12 Airport Highway, Los Angeles,CA, 90035, Female ),(Carson, Creed, 12 Airport Highway, Los Angeles,CA, 90035, Male),(Perry, Tyson, 1150 S King Ave Apt 12B, Atlanta , GA, 30301 , Male),(Aaliyah, Clark, 20 N Flowery Branch Road,Los Angeles, CA, 30301 , Female),(Abbigail, Udeh, 980 Best Friend Road APT 7H, Atlanta , GA, 30301, Female),(Uehudah, Urban, 040 Sugar Grov, Atlanta , GA, 30303, Male),(Hamilton, Hakeem, 50 Tower Rd, Atlanta , GA, 30303, Male),
  23. 23. (Adrianna, Gary, 66 Settingdown Road Apt 11Y ,Atlanta , GA, 30304, Female),(Macie, Gabriel, 190 ClockTower Drv, Atlanta , GA, 90034, Female),(Jaiden, Jacquez, 40 Knights St ,Atlanta , GA, 30303, Male),(Felipe, Elton, 50 Music Rd, Atlanta , GA, 30304, Male);Select * from dbo.customerCUST_NO FIRST_NAME LAST_NAME GENDER CUST_ADDRESS CITY CUST_STATE ZIP_CODE9628 washington Wells Male 20 Lakeshore Driv ApT 34C Chicago IL 606609740 TED SERGIO Male 1818 N Paulina Chicago IL 606329852 JULIA Richardson Female 8O Chicago RD Apt 227 Chicago IL 606339964 EMILY KELLY Female 12 S Kedize Apt 33GH Chicago IL 60642110076 CLAYTON Cliford Male 20 Lasalle Street Apt 40b Chicago IL 6060210188 RUBY JESSIE Female 2020 S State Street Chicago IL 6063910300 CLAUDE JAMIE Male 12 S Dearbon Apt 29 H Chicago IL 60642110412 MARILYN FERNANDO Female 12 Jane Adams Blvd Chicago IL 6060410524 NEIL PAVLIKOWSKI Female 20 N State Street Chicago IL 6063910636 PAVLICK JAVIER Male 200 W Howard Ave Apt 40b Chicago IL 60600310748 LOUISE JESSIE Female 40 S Abbott Street Apt 18H Chicago IL 6062910860 Sandy Jack Male 4040 N Washington BLVD SW Washington DC 2003210972 Christopher Peterson Male 24 N.Illinios Ave NE Washington DC 2001111084 RoseMary BlueGrass Female 20 S Capitol RD SE Washington DC 2001811196 Cassandra Morgan Female 1010 W New York Ave NW Washington DC 2003011308 Diana Mark Female 85 S A ST SW Washington DC 2005911420 Jayson Alex Male 1830 S Cider BLVD NW Washington DC 2003211532 JAEGERS SHU Female 40 S Washington BLVD SW Washington DC 2001111644 Nicholas Nicholas Male 24 N California Ave NE Apt4DWashington DC 2001811756 Liz Steward Female 120 S Ohio Ave SE Washington DC 2002211868 Austin Martin Female 11 W New Mexico Ave NWAPT 21BWashington DC 2003011980 LOPEZ PALISBO Male 85 S A ST SW Washington DC 2005915900 Derick Rose Male 12 S Racine Ave Apt 29 H Chicago IL 60642916012 Ralph Nadim Male 204 Kimball Rd Apt 20G Chicago IL 6062416124 Latoya Kadeem Female 11 Addison Apt 20c Chicago IL 6062416236 lisa Brad Female 80 West 83rd Apt 2 Chicago IL 6065416348 Kadyn Michaels Female 200 East 73rd Apt 2 Chicago IL 6062416460 Latrice Abby Female 40 Glenwood Aot 23 Chicago IL 6063216572 Thomas Martin Male 12 N Javis Apt 9 H3 Evaston IL 60672116684 Bernice Authur Female 19 S Highway 6 Apt 3 Evaston IL 6070416796 Betsy Chris Female 20 N Loyola Street Evaston IL 6063916908 Akon Cider Male 200 W Brodway Ave Apt 40 Evaston IL 60704
  24. 24. 17020 Richard King Female 40 S Theather Street Apt 1 Evaston IL 6070417132 Derick Stone Male 12 S Race Ave Apt 29 H Evaston IL 6070417244 Mark Harison Male 1010 S Centinela Ave Apt 2B Los Angles CA 9003417356 TINA Clark Female 20 N SunSet Blvd 2C Los Angeles CA 9003417468 MARILYN Wash Female 12 King Drv Los Angeles CA 9003417580 MARILYN Wash Female 12 King Drv Los Angeles CA 9003417692 Hamilton Anderson Female 050 N Ocean Drv Los Angeles CA 9003417804 CHAD ALFRED Male 090 S liberity PK Los Angeles CA 9003417916 Jones Don Male 4040 S Central Los Angeles CA 9003418028 Britney Hancock Female 90 S Hollywood Los Angeles CA 9003418140 Geroge Jefferson Male 40 S Empire Drv Los Angeles CA 9003418252 Madilyn Jaken Female 10 Beach Drive Los Angeles CA 9003518364 Chuck Chi Male 30 Mall Drive Los Angeles CA 9003518476 Cesar Carlos Male 40 S Central Los Angeles CA 9003418588 Nadine Corby Female 19 Pine Lane Los Angeles CA 9003318700 Abraham Abraham Male 40 S Airforce Rd Los Angeles CA 9003418812 udo udo Male 21 E Disney Rd Los Angeles CA 9003418924 Octavia Lee Female 10 Santa Monica Drive Los Angeles CA 9003519036 Abel Adon Male 30 santa jose drv Los Angeles CA 9003519148 Nancy William Male 40 S lake Rd Los Angeles CA 9003419260 Monica Corby Female 19 Cider Lane Los Angeles CA 9003319372 Chris Abraham Male 40 S Airforce Rd Los Angeles CA 9003419484 Diane Austine Female 90 Sony Drive Los Angeles CA 9003519596 Darlene Dion Female 92 Church Blvd Los Angeles CA 9003519708 Linda Creed Female 12 Airport Highway Los Angeles CA 9003519820 Carson Creed Male 12 Airport Highway Los Angeles CA 9003519932 Perry Tyson Male 1150 S King Ave Apt 12B Atlanta GA 3030120044 Aaliyah Clark Female 20 N Flowery Branch Road Los Angeles CA 3030120156 Abbigail Udeh Female 980 Best Friend Road APT 7H Atlanta GA 3030120268 Uehudah Urban Male 040 Sugar Grov Atlanta GA 3030320380 Hamilton Hakeem Male 50 Tower Rd Atlanta GA 3030320492 Adrianna Gary Female 66 Settingdown Road Apt 11Y Atlanta GA 3030420604 Macie Gabriel Female 190 ClockTower Drv Atlanta GA 9003420716 Jaiden Jacquez Male 40 Knights St Atlanta GA 3030320828 Felipe Elton Male 50 Music Rd Atlanta GA 30304ALTER dbo.customerDisable trigger customersdelete dbo.customeroutput deleted.cust_no, deleted.first_name, deleted.last_name, deleted.gender, deleted.cust_address,deleted.city, deleted.cust_state, deleted.zip_codewhere cust_no = 15900or cust_no = 10412;cust_no first_name last_name gender cust_address city cust_state zip_code----------- --------------------------------- ------------------------- ---------- ------------------------------------------------------- ---------------------------------------- ------------------------- -----------10412 MARILYN FERNANDO Female 12 Jane Adams Blvd Chicago IL 6060415900 Derick Rose Male 12 S Racine Ave Apt 29 H Chicago IL 606429(2 row(s) affected)use AdventureWorksGOselect C.ContactID, C.FirstName + . + C.MiddleName + . + C.LastName AS EMPLOYEE_NAME, E.Title AS JOB_TITLE,CONVERT (DATE,E.HireDate) AS HIRE_DATE,
  25. 25. ROUND(( P.RATE),2)AS PAY_RATE, ROUND((P.RATE * 40),2)AS WEEK_PAY1, ROUND((P.RATE * 40),2)AS WEEK_PAY2, ROUND((P.RATE * 40),2)AS WEEK_PAY3,ROUND((P.RATE * 40),2)AS WEEK_PAY4,E.VacationHours, E.SickLeaveHours,ROUND((P.Rate * 160),2) AS MONTH_PAY_TOTAL, D.DepartmentID AS DEPT_ID, HD.Name AS DEPT_NAMEINTO SALARY_DEC1from HumanResources.EmployeePayHistory PINNER JOINHumanResources.Employee EON P.EmployeeID = E.EmployeeIDINNER JOINPerson.Contact CON E.ContactID = E.ContactIDINNER JOIN HumanResources.EmployeeDepartmentHistory DON E.EmployeeID = D.DepartmentIDINNER JOIN HumanResources.Department HDON D.DepartmentID = HD.DepartmentIDWHERE C.FirstName + .+ C.MiddleName +. +C.LastName IS NOT NULL(3717252 row(s) affected)USE AdventureWorksGOCREATE PROCEDURE DBO.SALARYDECASSELECT top(50)CONTACTID, EMPLOYEE_NAME, JOB_TITLE, HIRE_DATE, WEEK_PAY1, WEEK_PAY2, WEEK_PAY3, WEEK_PAY4, VacationHours, SickLeaveHours,MONTH_PAY_TOTAL,DEPT_NAMEFROM dbo.SALARY_DEC1goEXEC DBO.SALARYDECCONTACTID EMPLOYEE_NAME JOB_TITLE HIRE_DATE WEEK_PAY1 WEEK_PAY2 WEEK_PAY3 WEEK_PAY4 VacationHours SickLeaveHours MONTH_PAY_TOTAL DEPT_NAME6714 Isabelle . W.Long ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6716 Isabelle .C.PattersonProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6719 Carla . C.Chapman ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6720 Isabelle .A.FloresProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6721 Isabelle .C.WinstonProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6726 Carla . C.Van ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6728 Carla .W.MalhotraProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6729 Isabelle .L.AlexanderProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering
  26. 26. 6730 Isabelle .S.RussellProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6733 Kelly . W.Price ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6735 Damien . M.Ye ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6736 Franklin .L.ZhangProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6738 Kelly . G.Wood ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6739 Carla . D.Madan ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6743 Kelly .L.HendersonProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6747 Kelly . E.Perry ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6748 Carla . K.Rana ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6749 Kelly . L.Powell ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6750 Kelly . G.Long ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6751 Carla . L.Raman ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6754 Carla .A.SullivanProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6755 Kelly . R.Flores ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6756 Kelly .M.WashingtonProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6757 Carla . L.Mehta ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6759 Kelly . S.Simmons ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6761 Franklin . S.Wang ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6763 Carla . G.Lopez ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6766 Carla .J.GonzalezProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6769 Carla .V.RodriguezProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6770 Kelly . E.Russell ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering
  27. 27. 6772 Kelly . F.Griffin ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6774 Virginia . J.Suri ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6776 Virginia .S.KapoorProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6777 Kelly . M.Hayes ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6779 Virginia .M.ChandraProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6780 Marissa .J.BennettProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6782 Franklin . A.Chen ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6783 Virginia .R.PatelProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6784 Melanie .P.ButlerProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6785 Melanie .T.SimmonsProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6786 Virginia .E.VanceProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6789 Melanie .C.GonzalesProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6790 Melanie .L.BryantProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6792 Melanie .N.AlexanderProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6793 Melanie .B.RussellProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6794 Melanie .A.GriffinProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6797 Melanie . G.Hayes ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6802 Alexandria .M.WoodProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6806 Alexandria .R.HendersonProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 Engineering6807 Virginia . C.Sara ProductionTechnician -WC607/31/1996 498 498 498 498 21 30 1992 EngineeringEXEC sp_helptext dbo.SALARYDEC
  28. 28. Text---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------CREATE PROCEDURE DBO.SALARYDECASSELECT top(50)CONTACTID, EMPLOYEE_NAME, JOB_TITLE, HIRE_DATE, WEEK_PAY1, WEEK_PAY2, WEEK_PAY3, WEEK_PAY4, VacationHours, SickLeaveHours,MONTH_PAY_TOTAL,DEPT_NAMEFROM dbo.SALARY_DEC1update SALARY_DEC1set pay_rate = 50where dept_id in (1,13,10,11)update SALARY_DEC1set pay_rate = 40where dept_id in (7, 6, 5, 4, 8,9, 14, 15)update SALARY_DEC1set pay_rate = 60where dept_id = 16update SALARY_DEC1set pay_rate = 30where dept_id not in (7,6,5,4,8,9,14,15,16,1,13,10,11)/* salary_dec1 was renamed to employee_salary*/Alter table employee_salaryadd Hours_Worked int;Update employee_salaryset hours_worked = 40;use AdventureWorksgoCREATE TRIGGER salarydbempONdbo.Employee_Salaryinstead of updateas printThe data has been sucessfully updated to the database.;use AdventureWorksgoupdate dbo.Employee_Salaryset VacationHours = 500where contactid in(6714, 7095, 711)The data has been sucessfully updated to the database.(648 row(s) affected)/* Only contact 6714 records was copied from excel to make it easier to understand that the update operation did not work */Select * from dbo.Employee_SalaryContactID EMPLOYEE_NAME JOB_TITLE HIRE_DATE PAY_RATE WEEK_PAY1 WEEK_PAY2 WEEK_PAY3 WEEK_PAY4 VacationHours SickLeaveHours MONTH_PAY_TOTAL DEPT_ID DEPT_NAME6714 Isabelle .W.LongProductionSupervisor- WC601/26/1998 40 1000 1000 1000 1000 82 61 4000 7 Production6714 Isabelle .W.Long Production 1/26/1998 40 1000 1000 1000 1000 82 61 4000 7 Production
  29. 29. Supervisor- WC606714 Isabelle .W.LongProductionSupervisor- WC601/26/1998 40 1000 1000 1000 1000 82 61 4000 7 Production6714 Isabelle .W.LongProductionSupervisor- WC601/26/1998 40 1000 1000 1000 1000 82 61 4000 7 Production6714 Isabelle .W.LongProductionSupervisor- WC601/26/1998 40 1000 1000 1000 1000 82 61 4000 7 Production6714 Isabelle .W.LongProductionSupervisor- WC601/26/1998 40 1000 1000 1000 1000 82 61 4000 7 Production6714 Isabelle .W.LongProductionSupervisor- WC601/26/1998 40 1000 1000 1000 1000 82 61 4000 7 Production6714 Isabelle .W.LongProductionSupervisor- WC601/26/1998 40 1000 1000 1000 1000 82 61 4000 7 ProductionUSE [AdventureWorks]GO/****** Object: Trigger [dbo].[salarydbemp] Script Date: 12/17/2011 07:42:01 ******/SET ANSI_NULLS onGOSET QUOTED_IDENTIFIER onGOALTER TRIGGER [dbo].[salarydbemp]ON[dbo].[Employee_Salary]instead of deleteas printThe data has been sucessfully deleted from the database.;GOCommand(s) completed successfully.update dbo.Employee_Salaryset VacationHours = 500where contactid in(6714, 7095)(648 row(s) affected)delete dbo.Employee_Salarywhere contactid = 7095The data has been sucessfully deleted from the database.(324 row(s) affected)Select * from dbo.Employee_Salarywhere contactid in(6714, 7095)ContactID EMPLOYEE_NAME JOB_TITLE HIRE_DATEPAY_RATE WEEK_PAY1 WEEK_PAY2 WEEK_PAY3 WEEK_PAY4VacationHours SickLeaveHoursMONTH_PAY_TOTALDEPT_ID DEPT_NAME Hours_Worked6714 Isabelle .W.LongProductionSupervisor- WC601/26/1998 40 1000 1000 1000 1000 500 61 4000 7 Production 407095 Joan .D.WatsonProductionSupervisor- WC601/26/1998 40 1000 1000 1000 1000 500 61 4000 7 Production 40
  30. 30. 7095 Joan .D.WatsonProductionSupervisor- WC601/26/1998 40 1000 1000 1000 1000 500 61 4000 7 Production 407095 Joan .D.WatsonProductionSupervisor- WC601/26/1998 40 1000 1000 1000 1000 500 61 4000 7 Production 407095 Joan .D.WatsonProductionSupervisor- WC601/26/1998 40 1000 1000 1000 1000 500 61 4000 7 Production 40use AdventureWorksgoALTER table dbo.Employee_SalaryDisable trigger salarydbemp;Command(s) completed successfully.delete dbo.Employee_Salaryoutput deleted.ContactID, deleted.EMPLOYEE_NAME, deleted.DEPT_ID, deleted.JOB_TITLEwhere contactid = 7095ContactID EMPLOYEE_NAME DEPT_ID JOB_TITLE7095 Joan . D.Watson 1 Production Technician - WC607095 Joan . D.Watson 1 Production Technician - WC607095 Joan . D.Watson 1 Production Technician - WC607095 Joan . D.Watson 1 Production Technician - WC607095 Joan . D.Watson 1 Production Technician - WC607095 Joan . D.Watson 1 Production Technician - WC607095 Joan . D.Watson 2 Marketing Assistant7095 Joan . D.Watson 2 Marketing Assistant7095 Joan . D.Watson 2 Marketing Assistant7095 Joan . D.Watson 2 Marketing AssistantUSE [AdventureWorks]GO/****** Object: Trigger [dbo].[salarydbemp] Script Date: 12/17/2011 12:17:23 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER TRIGGER [dbo].[salarydbemp]ON[dbo].[Employee_Salary]AFTER UPDATE, INSERTASIF UPDATE(PAY_RATE)BEGIN;PRINT PAY_RATE HAS BEEN UPDATEDEND;ELSEIF UPDATE(JOB_TITLE)BEGIN ;PRINT JOB TITLE HAS BEEN UPDATEDEND;ELSEIF UPDATE(VacationHours)BEGIN;PRINT VACTION HOURS HAS BEEN UPDATEDEND;ELSEIF UPDATE (SickLeaveHours)BEGIN;PRINT SICK HOURS UPDATEDend;
  31. 31. elsebegin;print The record has been updatedend;GOuse AdventureWorksupdate dbo.employee_salaryset PAY_RATE = 55where dept_name = Quality Assurance;goupdate dbo.employee_salaryset JOB_TITLE = Database Systemswhere dept_id = 7;goupdate dbo.employee_salaryset VacationHours = 340where contactid between 2 and 2005;goupdate dbo.employee_salaryset SickLeaveHours = 340where dept_id = 15;goPAY_RATE HAS BEEN UPDATED(80304 row(s) affected)JOB TITLE HAS BEEN UPDATED(2064960 row(s) affected)VACTION HOURS HAS BEEN UPDATED(353160 row(s) affected)SICK HOURS UPDATED(68832 row(s) affected)/* Results are shortened */SELECT CONTACTID, PAY_RATEFROM dbo.employee_salaryWHERE DEPT_NAME = Quality Assurance;CONTACTID PAY_RATE3602 553603 553604 553605 553608 553609 553616 553617 553619 553620 553629 55SELECT CONTACTID, JOB_TITLEFROM dbo.employee_salaryWHERE DEPT_ID = 7;CONTACTID JOB_TITLE
  32. 32. 721 Database Systems718 Database SystemsSELECT CONTACTID, SickLeaveHoursFROM dbo.employee_salaryWHERE DEPT_ID = 15;CONTACTID SickLeaveHours18 34019 34020 34021 34022 34024 34025 34026 340SELECT CONTACTID, VACATIONHOURSFROM dbo.employee_salaryWHERE contactid between 2 and 2005;use AdventureWorksGOCREATE PROCEDURE DBO.DEPT_EMPOLYEE1ASSELECT C.CONTACTID, C.EMPLOYEE_NAME, C.JOB_TITLE, C.HIRE_DATE, SUBSTRING(P.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(P.Phone,-7,11)AS AREA_CODE,E.EmployeeID, E.NationalIDNumber AS SNN, E.GenderFROM dbo.Employee_Salary CINNER JOIN HumanResources.Employee EON C.ContactID = E.ContactIDINNER JOIN Person.Contact PON E.ContactID =P.ContactIDWHERE DEPT_ID = 1AND C.CONTACTID NOT IN (1025, 1022, 1023, 1024);GoCommand(s) completed successfully.use AdventureWorksGOCREATE PROCEDURE DBO.DEPT_EMPOLYEES2CONTACTID VACATIONHOURS1836 3401836 3401836 3401836 3401836 3401836 3401836 3401836 3401836 3401836 3401836 3401836 3401836 3401836 3401836 3401836 3401836 340
  33. 33. ASSELECT C.CONTACTID, C.EMPLOYEE_NAME, C.JOB_TITLE, C.HIRE_DATE, SUBSTRING(P.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(P.Phone,-7,11)AS AREA_CODE,E.EmployeeID, E.NationalIDNumber AS SNN, E.GenderFROM dbo.Employee_Salary CINNER JOIN HumanResources.Employee EON C.ContactID = E.ContactIDINNER JOIN Person.Contact PON E.ContactID =P.ContactIDWHERE DEPT_ID = 2AND C.CONTACTID NOT IN (1025, 1022, 1023, 1024);GoCommand(s) completed successfully.use AdventureWorksGOCREATE PROCEDURE DBO.DEPT_EMPOLYEES3ASSELECT C.CONTACTID, C.EMPLOYEE_NAME, C.JOB_TITLE, C.HIRE_DATE, SUBSTRING(P.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(P.Phone,-7,11)AS AREA_CODE,E.EmployeeID, E.NationalIDNumber AS SNN, E.GenderFROM dbo.Employee_Salary CINNER JOIN HumanResources.Employee EON C.ContactID = E.ContactIDINNER JOIN Person.Contact PON E.ContactID =P.ContactIDWHERE DEPT_ID = 3;GoCREATE PROCEDURE DBO.DEPT_EMPOLYEES4ASSELECT C.CONTACTID, C.EMPLOYEE_NAME, C.JOB_TITLE, C.HIRE_DATE, SUBSTRING(P.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(P.Phone,-7,11)AS AREA_CODE,E.EmployeeID, E.NationalIDNumber AS SNN, E.GenderFROM dbo.Employee_Salary CINNER JOIN HumanResources.Employee EON C.ContactID = E.ContactIDINNER JOIN Person.Contact PON E.ContactID =P.ContactIDWHERE DEPT_ID = 4;GOCREATE PROCEDURE DBO.DEPT_EMPOLYEES5ASSELECT C.CONTACTID, C.EMPLOYEE_NAME, C.JOB_TITLE, C.HIRE_DATE, SUBSTRING(P.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(P.Phone,-7,11)AS AREA_CODE,E.EmployeeID, E.NationalIDNumber AS SNN, E.GenderFROM dbo.Employee_Salary CINNER JOIN HumanResources.Employee EON C.ContactID = E.ContactIDINNER JOIN Person.Contact PON E.ContactID =P.ContactIDWHERE DEPT_ID = 5;GOCREATE PROCEDURE DBO.DEPT_EMPOLYEES6ASSELECT C.CONTACTID, C.EMPLOYEE_NAME, C.JOB_TITLE, C.HIRE_DATE, SUBSTRING(P.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(P.Phone,-7,11)AS AREA_CODE,E.EmployeeID, E.NationalIDNumber AS SNN, E.GenderFROM dbo.Employee_Salary CINNER JOIN HumanResources.Employee EON C.ContactID = E.ContactIDINNER JOIN Person.Contact PON E.ContactID =P.ContactIDWHERE DEPT_ID = 6;GoCREATE PROCEDURE DBO.DEPT_EMPOLYEES7AS
  34. 34. SELECT C.CONTACTID, C.EMPLOYEE_NAME, C.JOB_TITLE, C.HIRE_DATE, SUBSTRING(P.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(P.Phone,-7,11)AS AREA_CODE,E.EmployeeID, E.NationalIDNumber AS SNN, E.GenderFROM dbo.Employee_Salary CINNER JOIN HumanResources.Employee EON C.ContactID = E.ContactIDINNER JOIN Person.Contact PON E.ContactID =P.ContactIDWHERE DEPT_ID = 7AND C.CONTACTID NOT IN (1025, 1022, 1023, 1024);GOCREATE PROCEDURE DBO.DEPT_EMPOLYEES8ASSELECT C.CONTACTID, C.EMPLOYEE_NAME, C.JOB_TITLE, C.HIRE_DATE, SUBSTRING(P.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(P.Phone,-7,11)AS AREA_CODE,E.EmployeeID, E.NationalIDNumber AS SNN, E.GenderFROM dbo.Employee_Salary CINNER JOIN HumanResources.Employee EON C.ContactID = E.ContactIDINNER JOIN Person.Contact PON E.ContactID =P.ContactIDWHERE DEPT_ID = 8AND C.CONTACTID NOT IN (1025, 1022, 1023, 1024);GOCREATE PROCEDURE DBO.DEPT_EMPOLYEES9ASSELECT C.CONTACTID, C.EMPLOYEE_NAME, C.JOB_TITLE, C.HIRE_DATE, SUBSTRING(P.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(P.Phone,-7,11)AS AREA_CODE,E.EmployeeID, E.NationalIDNumber AS SNN, E.GenderFROM dbo.Employee_Salary CINNER JOIN HumanResources.Employee EON C.ContactID = E.ContactIDINNER JOIN Person.Contact PON E.ContactID =P.ContactIDWHERE DEPT_ID = 9AND C.CONTACTID NOT IN (1025, 1022, 1023, 1024);GOCREATE PROCEDURE DBO.DEPT_EMPOLYEES10ASSELECT C.CONTACTID, C.EMPLOYEE_NAME, C.JOB_TITLE, C.HIRE_DATE, SUBSTRING(P.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(P.Phone,-7,11)AS AREA_CODE,E.EmployeeID, E.NationalIDNumber AS SNN, E.GenderFROM dbo.Employee_Salary CINNER JOIN HumanResources.Employee EON C.ContactID = E.ContactIDINNER JOIN Person.Contact PON E.ContactID =P.ContactIDWHERE DEPT_ID = 10;GOCREATE PROCEDURE DBO.DEPT_EMPOLYEES11ASSELECT C.CONTACTID, C.EMPLOYEE_NAME, C.JOB_TITLE, C.HIRE_DATE, SUBSTRING(P.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(P.Phone,-7,11)AS AREA_CODE,E.EmployeeID, E.NationalIDNumber AS SNN, E.GenderFROM dbo.Employee_Salary CINNER JOIN HumanResources.Employee EON C.ContactID = E.ContactIDINNER JOIN Person.Contact PON E.ContactID =P.ContactIDWHERE DEPT_ID = 11AND C.CONTACTID NOT IN (1025, 1022, 1023, 1024);GOCREATE PROCEDURE DBO.DEPT_EMPOLYEES12ASSELECT C.CONTACTID, C.EMPLOYEE_NAME, C.JOB_TITLE, C.HIRE_DATE, SUBSTRING(P.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(P.Phone,-7,11)AS AREA_CODE,E.EmployeeID, E.NationalIDNumber AS SNN, E.GenderFROM dbo.Employee_Salary CINNER JOIN HumanResources.Employee EON C.ContactID = E.ContactIDINNER JOIN Person.Contact PON E.ContactID =P.ContactID
  35. 35. WHERE DEPT_ID = 12AND C.CONTACTID NOT IN (1025, 1022, 1023, 1024);GOCREATE PROCEDURE DBO.DEPT_EMPOLYEES13ASSELECT C.CONTACTID, C.EMPLOYEE_NAME, C.JOB_TITLE, C.HIRE_DATE, SUBSTRING(P.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(P.Phone,-7,11)AS AREA_CODE,E.EmployeeID, E.NationalIDNumber AS SNN, E.GenderFROM dbo.Employee_Salary CINNER JOIN HumanResources.Employee EON C.ContactID = E.ContactIDINNER JOIN Person.Contact PON E.ContactID =P.ContactIDWHERE DEPT_ID = 13AND C.CONTACTID NOT IN (1025, 1022, 1023, 1024);GOCREATE PROCEDURE DBO.DEPT_EMPOLYEES14ASSELECT C.CONTACTID, C.EMPLOYEE_NAME, C.JOB_TITLE, C.HIRE_DATE, SUBSTRING(P.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(P.Phone,-7,11)AS AREA_CODE,E.EmployeeID, E.NationalIDNumber AS SNN, E.GenderFROM dbo.Employee_Salary CINNER JOIN HumanResources.Employee EON C.ContactID = E.ContactIDINNER JOIN Person.Contact PON E.ContactID =P.ContactIDWHERE DEPT_ID = 14AND C.CONTACTID NOT IN (1025, 1022, 1023, 1024);GOCREATE PROCEDURE DBO.DEPT_EMPOLYEES15ASSELECT C.CONTACTID, C.EMPLOYEE_NAME, C.JOB_TITLE, C.HIRE_DATE, SUBSTRING(P.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(P.Phone,-7,11)AS AREA_CODE,E.EmployeeID, E.NationalIDNumber AS SNN, E.GenderFROM dbo.Employee_Salary CINNER JOIN HumanResources.Employee EON C.ContactID = E.ContactIDINNER JOIN Person.Contact PON E.ContactID =P.ContactIDWHERE DEPT_ID = 15AND C.CONTACTID NOT IN (1025, 1022, 1023, 1024);GOCREATE PROCEDURE DBO.DEPT_EMPOLYEES16ASSELECT C.CONTACTID, C.EMPLOYEE_NAME, C.JOB_TITLE, C.HIRE_DATE, SUBSTRING(P.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(P.Phone,-7,11)AS AREA_CODE,E.EmployeeID, E.NationalIDNumber AS SNN, E.GenderFROM dbo.Employee_Salary CINNER JOIN HumanResources.Employee EON C.ContactID = E.ContactIDINNER JOIN Person.Contact PON E.ContactID =P.ContactIDWHERE DEPT_ID = 15AND C.CONTACTID NOT IN (1025, 1022, 1023, 1024);GOCommand(s) completed successfully.USE AdventureWorksGOEXEC dbo.DEPT_EMPOLYEES10;GOCONTACTID EMPLOYEE_NAME JOB_TITLE HIRE_DATE PHONE_NUMBER AREA_CODE EmployeeID SNN Gender
  36. 36. 1005 Gail . A.Erickson Production Technician - WC10 2/7/1998 555-0139 849 9 695256908 F1004 Sharon .B.SalavariaProduction Technician - WC10 2/7/1998 555-0138 970 270 56920285 F1005 Gail . A.Erickson Production Technician - WC10 2/7/1998 555-0139 849 9 695256908 F1004 Sharon .B.SalavariaProduction Technician - WC10 2/7/1998 555-0138 970 270 56920285 F1008 Janice . M.Galvin Production Technician - WC10 2/7/1998 555-0117 473 265 486228782 F1001 Terri . Lee.Duffy Production Technician - WC10 2/7/1998 555-0175 819 12 245797967 F1005 Gail . A.Erickson Production Technician - WC10 2/7/1998 555-0139 849 9 695256908 FRESULTS (ABRIDGED)USE AdventureWorksGOEXEC SP_HELPTEXT dbo.DEPT_EMPOLYEES10;GOText---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------CREATE PROCEDURE DBO.DEPT_EMPOLYEES10ASSELECT C.CONTACTID, C.EMPLOYEE_NAME, C.JOB_TITLE, C.HIRE_DATE, SUBSTRING(P.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(P.Phone,-7,11)AS AREA_CODE,E.EmployeeID, E.NationalIDNumber AS SNN, E.GenderFROM dbo.Employee_Salary CINNER JOIN HumanResources.Employee EON C.ContactID = E.ContactIDINNER JOIN Person.Contact PON E.ContactID =P.ContactIDWHERE DEPT_ID = 10;USE [AdventureWorks]GO/****** Object: StoredProcedure [dbo].[DEPT_EMPOLYEES10] Script Date: 12/18/2011 21:13:38 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[DEPT_EMPOLYEES10]WITH ENCRYPTIONASSELECT C.CONTACTID, C.EMPLOYEE_NAME, C.JOB_TITLE, C.HIRE_DATE, SUBSTRING(P.Phone,5,11)AS PHONE_NUMBER, SUBSTRING(P.Phone,-7,11)AS AREA_CODE,E.EmployeeID, E.NationalIDNumber AS SNN, E.GenderFROM dbo.Employee_Salary CINNER JOIN HumanResources.Employee EON C.ContactID = E.ContactIDINNER JOIN Person.Contact PON E.ContactID =P.ContactIDWHERE DEPT_ID = 10;GOThe text for object dbo.DEPT_EMPOLYEES10 is encrypted.USE AdventureWorksGOUSE AdventureWorksGO
  37. 37. CREATE PROCEDURE EMPOLYEE_GENDERF@SEARCH nchar(1) = FasSELECT E.EmployeeID, E.Gender, P.FirstName + + P.LastName + *******+ E.NationalIDNumber AS EMPLOYEE, CAST(E.HIREDATE AS CHAR(12))AS HIREDATEFROM HumanResources.Employee EINNER JOINPerson.Contact PON P.ContactID = E.ContactIDwhere e.Gender =@SEARCHGOEXEC EMPOLYEE_GENDERFEmployeeID Gender EMPLOYEE HIREDATE7 F JoLynn Dobney *******309738752 Jan 26 19988 F Ruth Ellerbrock *******690627818 Feb 6 19989 F Gail Erickson *******695256908 Feb 6 199812 F Terri Duffy *******245797967 Mar 3 199816 F Jo Brown *******446466105 Mar 30 199817 F Doris Hartwig *******565090917 Apr 11 199819 F Diane Glimp *******9659517 Apr 29 199830 F Paula Barreto de Mattos *******535145551 Jan 7 199934 F Susan Eaton *******586486572 Jan 8 199936 F Alice Ciccu *******113695504 Jan 8 199940 F Yvonne McKay *******713403643 Jan 10 199942 F Jean Trenary *******441044382 Jan 12 199950 F Susan Metters *******351069889 Jan 15 1999USE AdventureWorksGOCREATE PROCEDURE EMPDEPTRECORDS(@DEPTNAME NVARCHAR(50))ASSELECT E.EmployeeID, D.Name AS DEPT_NAME, E.Gender, P.FirstName + + P.LastName + ---+ SUBSTRING (E.NationalIDNumber,5,3) AS EMPLOYEE, CAST(E.HIREDATE AS CHAR(12))ASHIREDATE, E.NationalIDNumberFROM HumanResources.Employee EINNER JOINPerson.Contact PON P.ContactID = E.ContactIDINNER JOIN HumanResources.EmployeeDepartmentHistory HON E.EmployeeID = H.EmployeeIDINNER JOIN HumanResources.Department DON D.DepartmentID = H.DepartmentIDWHERE D.Name =@DEPTNAME;GOCommand(s) completed successfully.EmployeeID DEPT_NAME Gender EMPLOYEE HIREDATE NationalIDNumber
  38. 38. 268 Sales M Stephen Jiang ---978 Feb 4 2001 502097814273 Sales M Brian Welcker ---321 Mar 18 2001 112432117275 Sales M Michael Blythe ---601 Jul 1 2001 841560125276 Sales F Linda Mitchell ---447 Jul 1 2001 191644724277 Sales F Jillian Carson ---898 Jul 1 2001 615389812278 Sales M Garrett Vargas ---742 Jul 1 2001 234474252279 Sales M Tsvi Reiter ---743 Jul 1 2001 716374314280 Sales F Pamela Ansman-Wolfe ---166 Jul 1 2001 61161660281 Sales M Shu Ito ---978 Jul 1 2001 139397894282 Sales M José Saraiva ---714 Jul 1 2001 399771412283 Sales M David Campbell ---542 Jul 1 2001 987554265284 Sales F Amy Alberts ---104 May 18 2002 982310417285 Sales F Jae Pak ---913 Jul 1 2002 668991357286 Sales M Ranjit Varkey Chudukatil ---197 Jul 1 2002 134219713287 Sales M Tete Mensa-Annan ---619 Nov 1 2002 90836195288 Sales M Syed Abbas ---449 Apr 15 2003 481044938289 Sales F Rachel Valdez ---762 Jul 1 2003 954276278290 Sales F Lynn Tsoflias ---967 Jul 1 2003 758596752EXEC EMPDEPTRECORDS PRODUCTION;EmployeeID DEPT_NAME Gender EMPLOYEE HIREDATE NationalIDNumber1 Production M Guy Gilbert ---780 Jul 31 1996 1441780710 Production M Barry Johnson ---658 Feb 7 1998 91226582513 Production M Sidney Higa ---736 Mar 5 1998 84497362514 Production M Taylor Maxwell ---693 Mar 11 1998 23306930215 Production M Jeffrey Ford ---748 Mar 23 1998 13267482318 Production M John Campbell ---703 Apr 18 1998 49417034220 Production M Steven Selikoff ---689 Jan 2 1999 44396895522 Production M Stuart Munson ---601 Jan 3 1999 83546018023 Production M Greg Alderson ---859 Jan 3 1999 68768594124 Production M David Johnson ---388 Jan 3 1999 49813886925 Production M Zheng Mu ---681 Jan 4 1999 36086812226 Production M Ivo Salmre ---892 Jan 5 1999 96408921827 Production M Paul Komosinski ---627 Jan 5 1999 38416278829 Production M Kendall Keil ---587 Jan 6 1999 57165879731 Production M Alejandro McGuel ---977 Jan 7 1999 761597760
  39. 39. 32 Production M Garrett Young ---384 Jan 8 1999 27143843133 Production M Jian Shuo Wang ---392 Jan 8 1999 160739235use AdventureWorksDWCREATE FUNCTION EMPLOYEE_QUOTA()RETURNS TABLEASRETURN(SELECT E.EmployeeKey AS EMP_ID, E.FirstName + ,+ E.LastName AS EMP_NAME, F.CalendarYear, F.CalendarQuarter, F.SalesAmountQuota,CAST(T.FullDateAlternateKey AS NVARCHAR(12) ) AS DATE_RELASED,T.FiscalQuarter, T.FiscalYear,SUM(F.SalesAmountQuota) OVER(PARTITION BY E.EmployeeKey) AS TOTAL_SALES_EMPLOYEEFROM dbo.FactSalesQuota FINNER JOINdbo.DimTime TONF.TimeKey = T.TimeKeyINNER JOIN dbo.DimEmployee EONE.EmployeeKey = F.EmployeeKey);Select * from EMPLOYEE_QUOTA()Order by CalendarYearEMP_ID EMP_NAME CalendarYear CalendarQuarter SalesAmountQuota DATE_RELASED FiscalQuarter FiscalYear TOTAL_SALES_EMPLOYEE272 Stephen,Jiang 2001 3 $ 28,000.00 Jul 1 2001 1 2002 $ 1,305,000.00272 Stephen,Jiang 2001 4 $ 7,000.00 Oct 1 2001 2 2002 $ 1,305,000.00281 Michael,Blythe 2001 3 $ 367,000.00 Jul 1 2001 1 2002 $11,162,000.00281 Michael,Blythe 2001 4 $ 556,000.00 Oct 1 2001 2 2002 $11,162,000.00282 Linda,Mitchell 2001 3 $ 637,000.00 Jul 1 2001 1 2002 $11,786,000.00282 Linda,Mitchell 2001 4 $ 781,000.00 Oct 1 2001 2 2002 $11,786,000.00283 Jillian,Carson 2001 3 $ 565,000.00 Jul 1 2001 1 2002 $12,198,000.00283 Jillian,Carson 2001 4 $ 872,000.00 Oct 1 2001 2 2002 $12,198,000.00284 Garrett,Vargas 2001 3 $ 244,000.00 Jul 1 2001 1 2002 $ 4,365,000.00284 Garrett,Vargas 2001 4 $ 356,000.00 Oct 1 2001 2 2002 $ 4,365,000.00285 Tsvi,Reiter 2001 3 $ 669,000.00 Jul 1 2001 1 2002 $ 8,541,000.00285 Tsvi,Reiter 2001 4 $ 917,000.00 Oct 1 2001 2 2002 $ 8,541,000.00286 Pamela,Ansman-Wolfe 2001 3 $ 165,000.00 Jul 1 2001 1 2002 $ 3,551,000.00286 Pamela,Ansman-Wolfe 2001 4 $ 469,000.00 Oct 1 2001 2 2002 $ 3,551,000.00287 Shu,Ito 2001 3 $ 460,000.00 Jul 1 2001 1 2002 $ 7,804,000.00287 Shu,Ito 2001 4 $ 549,000.00 Oct 1 2001 2 2002 $ 7,804,000.00289 David,Campbell 2001 3 $ 226,000.00 Jul 1 2001 1 2002 $ 4,025,000.00289 David,Campbell 2001 4 $ 353,000.00 Oct 1 2001 2 2002 $ 4,025,000.00288 José,Saraiva 2001 3 $ 525,000.00 Jul 1 2001 1 2002 $ 7,098,000.00288 José,Saraiva 2001 4 $ 767,000.00 Oct 1 2001 2 2002 $ 7,098,000.00288 José,Saraiva 2004 1 $ 569,000.00 Jan 1 2004 3 2004 $ 7,098,000.00288 José,Saraiva 2004 2 $ 830,000.00 Apr 1 2004 4 2004 $ 7,098,000.00289 David,Campbell 2004 1 $ 234,000.00 Jan 1 2004 3 2004 $ 4,025,000.00289 David,Campbell 2004 2 $ 403,000.00 Apr 1 2004 4 2004 $ 4,025,000.00290 Amy,Alberts 2004 1 $ 116,000.00 Jan 1 2004 3 2004 $ 876,000.00290 Amy,Alberts 2004 2 $ 1,000.00 Apr 1 2004 4 2004 $ 876,000.00291 Jae,Pak 2004 1 $ 883,000.00 Jan 1 2004 3 2004 $10,514,000.00291 Jae,Pak 2004 2 $ 1,329,000.00 Apr 1 2004 4 2004 $10,514,000.00
  40. 40. 292 Ranjit,VarkeyChudukatil2004 1 $ 707,000.00 Jan 1 2004 3 2004 $ 5,557,000.00292 Ranjit,VarkeyChudukatil2004 2 $ 908,000.00 Apr 1 2004 4 2004 $ 5,557,000.00293 Tete,Mensa-Annan 2004 1 $ 454,000.00 Jan 1 2004 3 2004 $ 2,753,000.00293 Tete,Mensa-Annan 2004 2 $ 497,000.00 Apr 1 2004 4 2004 $ 2,753,000.00RESULTS (ABRIDGED)SELECT CUST_NO,CONVERT(DATE, DOB)AS DATE_OF_BIRTH,DATEDIFF(YY, DOB, CURRENT_TIMESTAMP) AS AGE, GENDER,F_NAME + ,+L_NAME AS CUSTOMER_NAMEINTOCUSTOMER_5FROM dbo.CUSTOMER_DIM009(18484 row(s) affected)use MSSQLGOcreate function customerAge8l(@CUST_NO INT)returns integerasbegindeclare @custAGE INTdeclare @custno intSELECT @custAGE= AGE FROMDB665. dbo.CUSTOMER_5WHERE CUST_NO= @CUST_NORETURN(@custAGE)ENDCommand(s) completed successfully.select dbo.customerAge8l(29483)52(1 row(s) affected)use MSSQLGOcreate function customerAges(@CUST_NO INT)returns integerasbegindeclare @custAGE INTSELECT @custAGE= DATEDIFF(YY, DOB, CURRENT_TIMESTAMP)FROM DB665.dbo.CUSTOMER_DIM009WHERE CUST_NO= @CUST_NORETURN(@custAGE)ENDCommand(s) completed successfully.
  41. 41. select dbo.customerAge8l(29483)-----------52(1 row(s) affected)USE MSSQLGOSELECT E.EmployeeID, E.Gender, E.Title, P.FirstName, P.LastName, E.BirthDate,D.DepartmentID, D.Name, DATEDIFF(YY, E.BirthDate, CURRENT_TIMESTAMP) AS AGE,CONVERT(DATE, E.HireDate) AS HIRE_DATE , DATEDIFF(YY, E.HireDate, CURRENT_TIMESTAMP) AS NUM_YRS_EMPLOYINTOEMP_RECORDSFROM AdventureWorks.HumanResources.Employee E INNERJOIN AdventureWorks.Person.Contact PON E.ContactID = P.ContactIDINNER JOIN AdventureWorks.HumanResources.EmployeeDepartmentHistory HONH.EmployeeID = E.EmployeeIDINNER JOIN AdventureWorks.HumanResources.Department DOND.DepartmentID = H.DepartmentID(296 row(s) affected)USE MSSQLGOcreate function AVG_AGE_DEPT(@DepartmentID SMALLINT)returns integerasbegindeclare @AVGAGE INTSELECT @AVGAGE= AVG(AGE)FROM EMP_RECORDSRETURN(@AVGAGE)ENDSELECT dbo.AVG_AGE_DEPT(7)-----------42(1 row(s) affected)SELECT dbo.AVG_AGE_DEPT(6)-----------42(1 row(s) affected)/* SOMETHING WAS WRONG WITH THE FUNCTION SO I DECIEDED TO FIX IT */ALTER function [dbo].[AVG_AGE_DEPT](@DepartmentID SMALLINT)returns integerasbegindeclare @AVGAGE INT
  42. 42. SELECT @AVGAGE= AVG(AGE)FROM EMP_RECORDSWHERE DepartmentID =@DepartmentIDRETURN(@AVGAGE)ENDUSE MSSQLSELECT dbo.AVG_AGE_DEPT(7)-----------41(1 row(s) affected)SELECT dbo.AVG_AGE_DEPT(6)-----------36(1 row(s) affected)USE MSSQLGOcreate function student_grad(@STUDENT_ID decimal,@SECTION_ID decimal)returns integerasbegindeclare @Final_grade INTSELECT @Final_grade= ROUND(sum(numeric_grade)/1000,2)* 100 from dbo.gradewhere student_id = @STUDENT_IDAND SECTION_ID = @SECTION_IDRETURN(@Final_grade)ENDCommand(s) completed successfully.SELECT dbo.student_grad(202,105)81(1 row(s) affected)SELECT dbo.student_grad(200,106)-----------93(1 row(s) affected)USE MSSQLGOCREATE FUNCTION [dbo].[PROFRECORD](@INSTRUCTOR_ID DECIMAL)RETURNS TABLEASRETURN(SELECT I.SALUTATION, I.FIRST_NAME +,+ I.LAST_NAME AS PROFESSOR_NAME,
  43. 43. S.SECTION_NO, S.COURSE_NO, S.LOCATION, S.CAPACITY, C.DESCRIPTION, S.SECTION_IDFROM DBO.INSTRUCTOR I INNER JOIN dbo.section SON I.INSTRUCTOR_ID = S.INSTRUCTOR_IDINNER JOIN dbo.course CON S.COURSE_NO = C. COURSE_NOINNER JOIN dbo.enrollment EONS.SECTION_ID = E.SECTION_IDWHERE I.INSTRUCTOR_ID = @INSTRUCTOR_ID)GOSELECT I.SALUTATION, I.FIRST_NAME +,+ I.LAST_NAME AS PROFESSOR_NAME,S.SECTION_NO, S.COURSE_NO, S.LOCATION, S.CAPACITY, C.DESCRIPTION, S.SECTION_IDFROM DBO.INSTRUCTOR I INNER JOIN dbo.section SON I.INSTRUCTOR_ID = S.INSTRUCTOR_IDINNER JOIN dbo.course CON S.COURSE_NO = C. COURSE_NOINNER JOIN dbo.enrollment EONS.SECTION_ID = E.SECTION_IDCROSS APPLY dbo.PROFRECORD(I.INSTRUCTOR_ID)ORDER BY C.DESCRIPTIONSALUTATION PROFESSOR_NAME SECTION_NO COURSE_NO LOCATION CAPACITY DESCRIPTION SECTION_IDMr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASICLanguage101Mr Fernand,Hanks 1 240 L509 10 Intro to the BASIC 101
  44. 44. LanguageRESULTS (ABRIDGED)CREATE VIEW DEPTSUMMARYASSELECT DISTINCT(DepartmentID),NAME,AVG(AGE) OVER(PARTITION BY DepartmentID) AS Avg_Age,MIN(AGE) OVER(PARTITION BY DepartmentID) AS Smallest_Age,MAX(AGE)OVER(PARTITION BY DepartmentID) AS Oldest_AgeFROMEMP_RECORDSSELECT * FROM dbo.DEPTSUMMARY;DepartmentID NAME Avg_Age Smallest_Age Oldest_Age1 Engineering 53 42 692 Tool Design 45 32 623 Sales 50 41 704 Marketing 42 34 595 Purchasing 48 37 696 Research and Development 36 34 427 Production 41 30 698 Production Control 39 33 539 Human Resources 41 37 4510 Finance 43 37 5711 Information Services 41 33 4612 Document Control 37 31 4513 Quality Assurance 42 32 5414 Facilities and Maintenance 51 45 6715 Shipping and Receiving 42 36 4916 Executive 48 45 52CREATE FUNCTION DEPT_SUMMARRY()RETURNS @SUMMARY TABLE(DEPT_ID INTEGER,DEPT_NAME NVARCHAR(50),AVG_AGE INT,SMALLEST_AGE INT,OLDEST_AGE INT)ASBEGININSERT @SUMMARY(DEPT_ID, Avg_Age, Smallest_Age, Oldest_Age, DEPT_NAME)SELECT DepartmentID, Avg_Age, Smallest_Age, Oldest_Age, NAMEFROMdbo.DEPTSUMMARYORDER BY NAMERETURN;END;SELECT * FROM DEPT_SUMMARRY()
  45. 45. DEPT_ID DEPT_NAME AVG_AGE SMALLEST_AGE OLDEST_AGE12 Document Control 37 31 451 Engineering 53 42 6916 Executive 48 45 5214 Facilities and Maintenance 51 45 6710 Finance 43 37 579 Human Resources 41 37 4511 Information Services 41 33 464 Marketing 42 34 597 Production 41 30 698 Production Control 39 33 535 Purchasing 48 37 6913 Quality Assurance 42 32 546 Research and Development 36 34 423 Sales 50 41 7015 Shipping and Receiving 42 36 492 Tool Design 45 32 62USE MSSQLGOCREATE FUNCTION productDetails()RETURNS @Details TABLE(ProductID int,Name nvarchar(50),color nvarchar(15),StandardCost money,ListPrice money,Size nvarchar(5),weight decimal,WeightUnitMeasureCode nchar(3),SizeUnitMeasureCode nchar(3),PROD_SUB_CATEGORY_NAME nvarchar(50),PROD_CATEGORY_NAME nvarchar(50),MODEL_NAME NVARCHAR(50))ASBEGININSERT @Details(ProductID,Name,StandardCost,ListPrice,Size,Weight, WeightUnitMeasureCode, SizeUnitMeasureCode,PROD_SUB_CATEGORY_NAME,PROD_CATEGORY_NAME,MODEL_NAME)SELECT p.ProductID, P.name,ROUND( p.StandardCost,2)StandardCost,p.ListPrice,p.Size,p.Weight, p.WeightUnitMeasureCode,p.SizeUnitMeasureCode,C.Name AS PROD_SUB_CATEGORY_NAME, G.NAME AS PROD_CATEGORY_NAME,M.Name AS MODEL_NAMEfromAdventureWorks.Production.Product pinnerjoinAdventureWorks.Production.ProductSubcategory CONp.ProductSubcategoryID = C.ProductSubcategoryIDINNER JOINAdventureWorks.Production.ProductCategory GON C.ProductCategoryID = G.ProductCategoryIDINNER JOINAdventureWorks.Production.ProductModel MON P.ProductModelID = M.ProductModelIDORDER BY P.NAME
  46. 46. RETURN;END;SELECT * FROM dbo.productDetails()ProductID Name color StandardCostListPrice Size weight WeightUnitMeasureCodeSizeUnitMeasureCodePROD_SUB_CATEGORY_NAME PROD_CATEGORY_NAME MODEL_NAME879 All-Purpose BikeStand59.47 159 Bike Stands Accessories All-Purpose Bike Stand712 AWC Logo Cap 6.92 8.99 Caps Clothing Cycling Cap877 Bike Wash -Dissolver2.97 7.95 Cleaners Accessories Bike Wash843 Cable Lock 10.31 25 Locks Accessories Cable Lock952 Chain 8.99 20.24 Chains Components Chain866 Classic Vest, L 23.75 63.5 L Vests Clothing Classic Vest865 Classic Vest, M 23.75 63.5 M Vests Clothing Classic Vest864 Classic Vest, S 23.75 63.5 S Vests Clothing Classic Vest878 Fender Set -Mountain8.22 21.98 Fenders Accessories Fender Set - Mountain863 Full-Finger Gloves,L15.67 37.99 L Gloves Clothing Full-Finger Gloves862 Full-Finger Gloves,M15.67 37.99 M Gloves Clothing Full-Finger GlovesRESULTS (ABRIDGED)/* DEFAULT DATABASE MASTER. ADVENTUREWORKS DATABASE WAS EXPORTED FROM MICROSOFT SQL SERVER 2008 INTO MICROSOFT SQL SERVER 2008R2 */select E.EmployeeID, P.FirstName + , + P.LastName AS Employee_Name,E.BirthDate, CONVERT(nvarchar(12), E.BirthDate, 101) as Birthdate_American,CONVERT(nvarchar(12), E.BirthDate, 103) as Birthdate_European,CONVERT(nvarchar(12), E.BirthDate, 100) as Birthdate_defaultfrom HumanResources.Employee EINNER JOINPerson.Contact PON E.ContactID = P.ContactIDorder by e.EmployeeI
  47. 47. RESULTS (ABRIDGED)select E.EmployeeID, P.FirstName + , + P.LastName AS Employee_Name,E.BirthDate, DATENAME(MONTH,E.BIRTHDATE)+ -+ DATENAME(DAY,E.BIRTHDATE)+ - + DATENAME(YEAR,E.BIRTHDATE) AS BIRTH_DATEfrom HumanResources.Employee EINNER JOINPerson.Contact PON E.ContactID = P.ContactIDorder by e.EmployeeIDEmployeeID Employee_Name BirthDate BIRTH_DATE1 Guy,Gilbert 1972-05-15 00:00:00.000 May-15-1972EmployeeID Employee_Name BirthDate Birthdate_American Birthdate_European Birthdate_default1 Guy,Gilbert 5/15/1972 5/15/1972 15/05/1972 May 15 19722 Kevin,Brown 6/3/1977 6/3/1977 3/6/1977 Jun 3 19773 Roberto,Tamburello 12/13/1964 12/13/1964 13/12/1964 Dec 13 19644 Rob,Walters 1/23/1965 1/23/1965 23/01/1965 Jan 23 19655 Thierry,DHers 8/29/1949 8/29/1949 29/08/1949 Aug 29 19496 David,Bradley 4/19/1965 4/19/1965 19/04/1965 Apr 19 19657 JoLynn,Dobney 2/16/1946 2/16/1946 16/02/1946 Feb 16 19468 Ruth,Ellerbrock 7/6/1946 7/6/1946 6/7/1946 Jul 6 19469 Gail,Erickson 10/29/1942 10/29/1942 29/10/1942 Oct 29 194210 Barry,Johnson 4/27/1946 4/27/1946 27/04/1946 Apr 27 194611 Jossef,Goldberg 4/11/1949 4/11/1949 11/4/1949 Apr 11 194912 Terri,Duffy 9/1/1961 9/1/1961 1/9/1961 Sep 1 196113 Sidney,Higa 10/1/1946 10/1/1946 1/10/1946 Oct 1 194614 Taylor,Maxwell 5/3/1946 5/3/1946 3/5/1946 May 3 194615 Jeffrey,Ford 8/12/1946 8/12/1946 12/8/1946 Aug 12 194616 Jo,Brown 11/9/1946 11/9/1946 9/11/1946 Nov 9 194617 Doris,Hartwig 5/6/1946 5/6/1946 6/5/1946 May 6 194618 John,Campbell 9/8/1946 9/8/1946 8/9/1946 Sep 8 194619 Diane,Glimp 4/30/1946 4/30/1946 30/04/1946 Apr 30 1946
  48. 48. 2 Kevin,Brown 1977-06-03 00:00:00.000 June-3-19773 Roberto,Tamburello 1964-12-13 00:00:00.000 December-13-19644 Rob,Walters 1965-01-23 00:00:00.000 January-23-1965RESULTS (ABRIDGED)SELECT PT.ProductID,PT.Name, PT.ProductNumber,PT.Color, PT.SIZE,PT.SizeUnitMeasureCode, $ + CONVERT(VARCHAR(30), CONVERT(MONEY, PT.ListPrice))AS LISTPRICE,$ + CONVERT(VARCHAR(40), CONVERT(MONEY, PT.StandardCost))AS STAMDARDCOST,$ + CONVERT(VARCHAR(40), CONVERT(MONEY, P.LineTotal))AS LINETOTAL,$ + CONVERT(VARCHAR(40), CONVERT(MONEY, P.UnitPrice))AS UNITPRICE,P.OrderQty, P.StockedQty,H.PurchaseOrderID,CONVERT( DATE, H.ORDERDATE ) AS ORDER_DATE,$ + CONVERT(VARCHAR(40), CONVERT(MONEY, H.SubTotal))AS SUBTOTAL,$ + CONVERT(VARCHAR(40), CONVERT(MONEY, H.TaxAmt))AS TAXAMOUNT,$ + CONVERT(VARCHAR(40), CONVERT(MONEY, H.Freight))AS FREIGHT,$ + CONVERT(VARCHAR(40), CONVERT(MONEY, H.TotalDue))AS TOTAL,H.EmployeeID, H.VendorIDFROM Purchasing.PurchaseOrderDetail PINNER JOINProduction.Product PTON P.ProductID = PT.ProductIDINNER JOINPurchasing.PurchaseOrderHeader HON P.PurchaseOrderID = H.PurchaseOrderIDRESULTS (ABRIDGED)SELECT c.CustomerID, right(c.AccountNumber,5) as Account_Number, p.FirstName + ,+ p.LastName as Customer_Name,h.CreditCardID,right(s.CardNumber,5) + ****** as credit_Number, s.ExpMonth , s.ExpYearFROM Sales.Customer cProductID Name ProductNumberColor SIZE SizeUnitMeasureCodeLISTPRICE STAMDARDCOSTLINETOTALUNITPRICEOrderQtyStockedQtyPurchaseOrderIDORDERDATESUBTOTALTAXAMOUNTFREIGHT TOTAL EmployeeIDVendorID403 ExternalLockWasher 3LE-1000$0.00 $0.00 $136.11 $45.37 3 3 33 2/16/2002 $421.16 $33.69 $10.53 $465.38 241 87404 ExternalLockWasher 4LE-1200$0.00 $0.00 $148.93 $49.64 3 3 33 2/16/2002 $421.16 $33.69 $10.53 $465.38 241 87405 ExternalLockWasher 9LE-1201$0.00 $0.00 $136.11 $45.37 3 3 33 2/16/2002 $421.16 $33.69 $10.53 $465.38 241 87463 LockWasher 4LW-1000$0.00 $0.00 $142.57 $47.52 3 3 43 2/16/2002 $427.93 $34.23 $10.70 $472.86 241 37464 LockWasher 5LW-1200$0.00 $0.00 $136.27 $45.42 3 3 43 2/16/2002 $427.93 $34.23 $10.70 $472.86 241 37465 LockWasher10LW-1201$0.00 $0.00 $149.09 $49.70 3 3 43 2/16/2002 $427.93 $34.23 $10.70 $472.86 241 37422 Thin-JamLock Nut 9LJ-1213 $0.00 $0.00 $143.04 $47.68 3 3 63 2/24/2002 $429.35 $34.35 $10.73 $474.43 241 25423 Thin-JamLock Nut10LJ-1220 $0.00 $0.00 $136.74 $45.58 3 3 63 2/24/2002 $429.35 $34.35 $10.73 $474.43 241 25424 Thin-JamLock Nut 1LJ-1420 $0.00 $0.00 $149.56 $49.85 3 3 63 2/24/2002 $429.35 $34.35 $10.73 $474.43 241 254 HeadsetBallBearingsBE-2908$0.00 $0.00 $171.08 $57.03 3 3 83 3/12/2002 $171.08 $13.69 $4.28 $189.04 241 85
  49. 49. inner joinSales.SalesOrderHeader honc.CustomerID = h.CustomerIDinner joinSales.CreditCard sonh.CreditCardID = s.CreditCardIDinner joinSales.Individual ionc.CustomerID = i.CustomerIDinner joinPerson.Contact pon i.ContactID =p.ContactIDCustomerID Account_Number Customer_Name CreditCardID Credit_Number ExpMonth ExpYear11176 11176 Mason,Roberts 11843 85388****** 4 200711142 11142 Eduardo,Patterson 3510 02051****** 4 200716597 16597 Kelli,Xu 10296 62475****** 4 200711176 11176 Mason,Roberts 11843 85388****** 4 200719873 19873 Dylan,Thomas 7280 35362****** 4 200711176 11176 Mason,Roberts 11843 85388****** 4 200725299 25299 Hector,Romero 6605 07877****** 4 200723364 23364 Natalie,Young 3521 57777****** 4 200714554 14554 Isaac,Mitchell 10579 43251****** 4 200713249 13249 Ryan,Perry 12654 21331****** 4 200720162 20162 Kelsey,Nath 5226 88245****** 4 200724452 24452 Megan,Coleman 4633 22326****** 4 200711176 11176 Mason,Roberts 11843 85388****** 4 200717717 17717 Jeremiah,Miller 9537 95878****** 4 200729292 29292 Diana,Dominguez 12559 17791****** 4 200714953 14953 Adrienne,Gill 5845 32486****** 4 200711766 11766 Candace,Raman 12270 54265****** 4 200714067 14067 Larry,Sanz 18734 60169****** 4 200715886 15886 Matthew,Brown 8756 97719****** 4 2007RESULTS (ABRIDGED)USE MASTEREXEC (select * from Purchasing.PurchaseOrderHeader WHERE VendorID = 29;);PurchaseOrderID RevisionNumber Status EmployeeID VendorID ShipMethodID OrderDate ShipDate SubTotal TaxAmt Freight TotalDue ModifiedDate4003 3 2 198 29 3 2004-06-14 00:00:00.000 2004-07-09 00:00:00.000 27453.60 2196.288 549.072 30198.96 2005-09-12 12:25:46.3604004 4 2 198 29 3 2003-12-05 00:00:00.000 2003-12-30 00:00:00.000 46383.00 3710.64 927.66 51021.30 2005-09-12 12:25:46.3604005 2 2 198 29 3 2004-02-28 00:00:00.000 2004-03-24 00:00:00.000 7425.00 594.00 148.50 8167.50 2005-09-12 12:25:46.3734012 5 2 231 29 3 2004-07-25 00:00:00.000 2004-08-19 00:00:00.000 997680.00 79814.40 19953.60 1097448.00 2005-09-12 12:25:46.483EXECsp_executesqlN select * from Purchasing.PurchaseOrderHeader WHERE VendorID = @VENDORID;,N@VENDORID INT,@VENDORID = 83;PurchaseOrderID RevisionNumber Status EmployeeID VendorID ShipMethodID OrderDate ShipDate SubTotal TaxAmt Freight TotalDue ModifiedDate1 0 4 244 83 3 2001-05-17 00:00:00.000 2001-05-26 00:00:00.000 201.04 16.0832 5.026 222.1492 2001-05-26 00:00:00.00080 0 4 264 83 5 2002-03-12 00:00:00.000 2002-03-21 00:00:00.000 150.7905 12.0632 3.7698 166.6235 2002-03-21 00:00:00.000159 0 4 261 83 5 2002-07-01 00:00:00.000 2002-07-10 00:00:00.000 150.7905 12.0632 3.7698 166.6235 2002-07-10 00:00:00.000238 0 4 238 83 5 2002-10-07 00:00:00.000 2002-10-16 00:00:00.000 150.7905 12.0632 3.7698 166.6235 2002-10-16 00:00:00.000317 0 4 233 83 5 2003-05-26 00:00:00.000 2003-06-04 00:00:00.000 150.7905 12.0632 3.7698 166.6235 2003-06-04 00:00:00.000396 0 4 223 83 5 2003-07-13 00:00:00.000 2003-07-22 00:00:00.000 150.7905 12.0632 3.7698 166.6235 2003-07-22 00:00:00.000426 0 4 238 83 5 2003-09-05 00:00:00.000 2003-09-14 00:00:00.000 150.7905 12.0632 3.7698 166.6235 2003-09-14 00:00:00.000505 0 4 198 83 5 2003-09-12 00:00:00.000 2003-09-21 00:00:00.000 150.7905 12.0632 3.7698 166.6235 2003-09-21 00:00:00.000588 0 4 223 83 5 2003-09-16 00:00:00.000 2003-09-25 00:00:00.000 150.7905 12.0632 3.7698 166.6235 2003-09-25 00:00:00.000

×