10. 9
FROM HumanResources.Employee HRE
BusinessEntityID BirthDate Gender MaritalStatus NationalIDNumber SickLeaveHours VacationHours AGE
1 1959-03-02 M S 295847284 69 99 54
2 1961-09-01 F S 245797967 20 1 52
3 1964-12-13 M M 509647174 21 2 49
4 1965-01-23 M S 112457891 80 48 48
5 1942-10-29 F M 695256908 22 5 71
6 1949-04-11 M M 998320692 23 6 64
7 1977-03-27 M M 134969118 50 61 36
8 1976-07-06 F S 811994146 51 62 37
9 1969-02-21 F M 658797903 51 63 44
10 1975-01-01 M M 879342154 64 16 38
11 1968-02-18 M S 974026903 23 7 45
12 1949-08-29 M M 480168528 24 9 64
13 1979-06-29 F M 486228782 24 8 34
14 1969-07-17 M S 42487730 21 3 44
15 1951-06-03 F M 56920285 22 4 62
16 1965-04-19 M S 24756624 40 40 48
17 1977-06-03 M S 253022876 41 42 36
Results Abridged
SELECT SOH.CustomerID, CAST (SOH.OrderDate AS DATE)OrderDate, SOH.SalesOrderID,
CAST(SOH.Freight AS DECIMAL(30,2))AS Freight, CAST(SOH.TaxAmt AS DECIMAL(30,2))AS TaxAmt,
CAST(SOH.TotalDue AS DECIMAL(30,2))AS TotalDue,
SCC.CardType, encrypt(SCC.CardNumber, 'AES256') AS CardNum, SCC.ExpMonth||'-'||SCC.ExpYear AS EXP_DATE,
SCY.Name AS Currency_Name, STR.CountryRegionCode, STR.Name AS Territory_Name, STR."Group" AS TerritoryGroup
FROM Sales.SalesOrderHeader SOH
INNER JOIN
Sales.Customer SC
ON
SOH. CUSTOMERID = SC.CUSTOMERID
INNER JOIN
Sales.CreditCard SCC
ON
SCC.CREDITCARDID = SOH.CREDITCARDID
INNER JOIN
SALES.CurrencyRate SCR
ON
SOH.CurrencyRateID = SCR.CurrencyRateID
INNER JOIN
SALES.SalesTerritory STR
ON
STR.TerritoryID = SOH.TerritoryID
INNER JOIN
SALES.Currency SCY
ON
SCY.CurrencyCode = SCR.FromCurrencyCode
OR
SCY.CurrencyCode = SCR.ToCurrencyCode
CustomerID OrderDate SalesOrderID Freight TaxAmt TotalDue CardType CardNum EXP_DATE Currency_Name CountryRegionCode Territory_Name TerritoryGroup
29734 2001-07-01 43661 985.55 3153.77 43561.44 Distinguish 0xefb7785a52c60cb9c9a3d3f0f28f3a3b3fd0867fa05bdb18db2f989ce78d0894 9-2007 US Dollar CA Canada North America
29734 2001-07-01 43661 985.55 3153.77 43561.44 Distinguish 0xefb7785a52c60cb9c9a3d3f0f28f3a3b3fd0867fa05bdb18db2f989ce78d0894 9-2007 Canadian Dollar CA Canada North America
29994 2001-07-01 43662 867.24 2775.16 38331.96 ColonialVoice 0x48dc87c0e811fa57175e155dd70fbf1955c087036917ff8264b9939a8c3e54d4 1-2006 US Dollar CA Canada North America
29994 2001-07-01 43662 867.24 2775.16 38331.96 ColonialVoice 0x48dc87c0e811fa57175e155dd70fbf1955c087036917ff8264b9939a8c3e54d4 1-2006 Canadian Dollar CA Canada North America
29614 2001-07-01 43668 1081.80 3461.77 47815.63 ColonialVoice 0x35897b5ee7b21cf16e3a9a7960a4c153552917e14c6094338c808810207de212 11-2008 US Dollar CA Canada North America
29614 2001-07-01 43668 1081.80 3461.77 47815.63 ColonialVoice 0x35897b5ee7b21cf16e3a9a7960a4c153552917e14c6094338c808810207de212 11-2008 Canadian Dollar CA Canada North America
30067 2001-07-01 43672 183.63 587.60 8116.26 Vista 0x64a8d7a7dab2cecd5f6c22668b736db6bc47dd8bc6d1abc5d16c2e11ec58c4bd 3-2006 US Dollar CA Canada North America
30067 2001-07-01 43672 183.63 587.60 8116.26 Vista 0x64a8d7a7dab2cecd5f6c22668b736db6bc47dd8bc6d1abc5d16c2e11ec58c4bd 3-2006 Canadian Dollar CA Canada North America
29596 2001-07-01 43674 78.73 251.94 3479.93 SuperiorCard 0x8219aaaac5c8bf1d0e92301fefb2f876244494067ff7c8357f29ce4cd2b9ced8 4-2005 US Dollar CA Canada North America
29596 2001-07-01 43674 78.73 251.94 3479.93 SuperiorCard 0x8219aaaac5c8bf1d0e92301fefb2f876244494067ff7c8357f29ce4cd2b9ced8 4-2005 Canadian Dollar CA Canada North America
29824 2001-07-01 43677 233.47 747.10 10319.33 SuperiorCard 0xae39eae197c2691c9a7b5e3356175a8f53f1d7bc5e6dbed1359c0ca358098495 4-2006 US Dollar CA Canada North America
29824 2001-07-01 43677 233.47 747.10 10319.33 SuperiorCard 0xae39eae197c2691c9a7b5e3356175a8f53f1d7bc5e6dbed1359c0ca358098495 4-2006 Canadian Dollar CA Canada North America
29761 2001-07-01 43679 39.31 125.80 1737.66 SuperiorCard 0x394b3d44f4e5b7fccbd26d6d4e3fba6d4121dcf0472f7a4ac9eb922222f9a020 9-2008 US Dollar CA Canada North America
29761 2001-07-01 43679 39.31 125.80 1737.66 SuperiorCard 0x394b3d44f4e5b7fccbd26d6d4e3fba6d4121dcf0472f7a4ac9eb922222f9a020 9-2008 Canadian Dollar CA Canada North America
21768 2001-07-01 43697 89.46 286.26 3953.99 SuperiorCard 0x477e43dc99fc3fc8e8dc127b735cf3c51b37b17addfb8f0064d3e3c2fffc0d0e 7-2008 US Dollar CA Canada North America
Results Abridged
SELECT SOH.SalesOrderID, SOH.CustomerID,CAST(SOH.TotalDue AS DECIMAL(30,2))AS TotalDue,
STR.CountryRegionCode, STR.Name AS Territory_Name, STR."Group" AS TerritoryGroup,
SUM(CAST(SOH.TotalDue AS DECIMAL(30,2)))
over(PARTITION BY STR.CountryRegionCode) AS TOTAL_Territor_Sales_Region,
SUM(CAST(SOH.TotalDue AS DECIMAL(30,2)))
over(PARTITION BY STR."Group") AS TOTAL_Territor_Sales_Group,
SUM(CAST(SOH.TotalDue AS DECIMAL(30,2)))
over(PARTITION BY STR.Name) AS TOTAL_Territor_Sales_Name
FROM Sales.SalesOrderHeader SOH
INNER JOIN
SALES.SalesTerritory STR
ON
STR.TerritoryID = SOH.TerritoryID
where STR.CountryRegionCode = 'US'
11. 10
SalesOrderID CustomerID TotalDue CountryRegionCode Territory_Name TerritoryGroup TOTAL_Territor_Sales_Region TOTAL_Territor_Sales_Group TOTAL_Territor_Sales_Name
66560 21246 1320.44 US Southwest North America 82425339.37 82425339.37 31213458.71
70656 24831 2649.85 US Southwest North America 82425339.37 82425339.37 31213458.71
70912 17441 2657.47 US Southwest North America 82425339.37 82425339.37 31213458.71
44544 29522 27847.06 US Southwest North America 82425339.37 82425339.37 31213458.71
45568 29637 20205.63 US Southwest North America 82425339.37 82425339.37 31213458.71
46336 29944 556.20 US Southwest North America 82425339.37 82425339.37 31213458.71
47104 13279 2410.63 US Southwest North America 82425339.37 82425339.37 31213458.71
47360 29696 1051.23 US Southwest North America 82425339.37 82425339.37 31213458.71
48128 26534 2288.92 US Southwest North America 82425339.37 82425339.37 31213458.71
48384 29562 90625.93 US Southwest North America 82425339.37 82425339.37 31213458.71
48640 26681 2288.92 US Southwest North America 82425339.37 82425339.37 31213458.71
49152 29792 30842.05 US Southwest North America 82425339.37 82425339.37 31213458.71
49408 15535 865.20 US Southwest North America 82425339.37 82425339.37 31213458.71
50688 29793 77584.01 US Southwest North America 82425339.37 82425339.37 31213458.71
51968 13171 132.60 US Southwest North America 82425339.37 82425339.37 31213458.71
55296 29850 2916.30 US Southwest North America 82425339.37 82425339.37 31213458.71
57344 12047 38.68 US Southwest North America 82425339.37 82425339.37 31213458.71
58368 25208 30.14 US Southwest North America 82425339.37 82425339.37 31213458.71
60160 13454 2590.41 US Southwest North America 82425339.37 82425339.37 31213458.71
Results Abridged
User Defined Function
CREATE OR REPLACE FUNCTION EMPAGE( )
RETURNS INTEGER
BEGIN
SELECT DATEDIFF(YY, HRE.BirthDate , CURRENT_TIMESTAMP) AS EAGE
FROM HumanResources.Employee HRE
END;
SELECT SELECT "DBA"."EMPAGE"();
CREATE OR REPLACE FUNCTION EMPAGE( in @SNN integer)
RETURNS INTEGER
BEGIN
DECLARE @EMPAGE1 integer;
SELECT DATEDIFF(YY, HRE.BirthDate , CURRENT_TIMESTAMP) AS EAGE
INTO
@EMPAGE1
FROM HumanResources.Employee HRE
WHERE HRE.NationalIDNumber = @SNN;
RETURN (@EMPAGE1);
END;
SELECT "DBA"."EMPAGE"(509647174 ) AS EMP_AGE;
12. 11
Stored Procedures
CREATE OR REPLACE PROCEDURE SalesTerritory_Report
(@Region_Code Char(8))
AS
SELECT SOH.SalesOrderNumber, CAST (SOH.OrderDate AS DATE) AS OrderDate , CAST (SOH.DueDate AS DATE) AS DueDate, CAST (SOH.ShipDate AS DATE) AS ShipDate, SOD.ProductID
,SOH.CUSTOMERID AS CustomerID, cast (SOH.SUBTOTAL as Decimal (35,2))AS SubTotal,
cast (SOH.TAXAMT as Decimal (35,2))AS TaxAmt, cast (SOH.FREIGHT as Decimal (35,2))AS Freight, cast (SOH.TOTALDUE as Decimal (35,2))AS TotalDue, PD.Name AS ProductName, ST.NAME as TerritoryName,
ST.CountryRegionCode,
ST."Group" AS TerritoryGroup, ST.TerritoryID
FROM Sales.SalesOrderDetail SOD
INNER JOIN Sales.SalesOrderHeader SOH
ON
SOD.SalesOrderID = SOH.SalesOrderID
INNER JOIN Production.Product PD
ON SOD.PRODUCTID = PD.PRODUCTID
INNER JOIN
SALES.SalesTerritory ST
ON
SOH.TerritoryID = ST.TerritoryID
where CountryRegionCode = @Region_Code
ORDER BY PD.NAME;
EXEC SalesTerritory_Report('GB')
SalesOrderNumber OrderDate DueDate ShipDate ProductID CustomerID SubTotal TaxAmt Freight TotalDue ProductName TerritoryName CountryRegionCode TerritoryGroup TerritoryID
SO73803 2004-06-26 2004-07-08 2004-07-03 879 16938 928.49 74.28 23.21 1025.98 All-Purpose Bike Stand United Kingdom GB Europe 10
SO70784 2004-05-17 2004-05-29 2004-05-24 879 14156 195.59 15.65 4.89 216.13 All-Purpose Bike Stand United Kingdom GB Europe 10
SO71313 2004-05-25 2004-06-06 2004-06-01 879 16325 166.28 13.30 4.16 183.74 All-Purpose Bike Stand United Kingdom GB Europe 10
SO74525 2004-07-12 2004-07-24 2004-07-19 879 13830 195.59 15.65 4.89 216.13 All-Purpose Bike Stand United Kingdom GB Europe 10
SO69201 2004-04-28 2004-05-10 2004-05-05 879 25482 162.99 13.04 4.07 180.10 All-Purpose Bike Stand United Kingdom GB Europe 10
SO69205 2004-04-28 2004-05-10 2004-05-05 879 13664 195.27 15.62 4.88 215.77 All-Purpose Bike Stand United Kingdom GB Europe 10
SO55484 2003-10-03 2003-10-15 2003-10-10 879 17840 188.98 15.12 4.72 208.82 All-Purpose Bike Stand United Kingdom GB Europe 10
SO60357 2003-12-19 2003-12-31 2003-12-26 879 12513 182.78 14.62 4.57 201.97 All-Purpose Bike Stand United Kingdom GB Europe 10
SO57657 2003-11-09 2003-11-21 2003-11-16 879 25470 162.99 13.04 4.07 180.10 All-Purpose Bike Stand United Kingdom GB Europe 10
SO64640 2004-02-21 2004-03-04 2004-02-28 879 12496 204.58 16.37 5.11 226.06 All-Purpose Bike Stand United Kingdom GB Europe 10
SO62727 2004-01-25 2004-02-06 2004-02-01 879 21847 187.98 15.04 4.70 207.72 All-Purpose Bike Stand United Kingdom GB Europe 10
SO56027 2003-10-13 2003-10-25 2003-10-20 879 20054 2638.94 211.12 65.97 2916.03 All-Purpose Bike Stand United Kingdom GB Europe 10
SO59644 2003-12-08 2003-12-20 2003-12-15 879 26938 722.77 57.82 18.07 798.66 All-Purpose Bike Stand United Kingdom GB Europe 10
SO57775 2003-11-11 2003-11-23 2003-11-18 879 29373 184.48 14.76 4.61 203.85 All-Purpose Bike Stand United Kingdom GB Europe 10
SO64183 2004-02-15 2004-02-27 2004-02-22 879 16589 2481.28 198.50 62.03 2741.81 All-Purpose Bike Stand United Kingdom GB Europe 10
Results Abridged
CREATE OR REPLACE PROCEDURE PRODUCTS()
AS
SELECT PD.ProductID, PD.NAME, PD.COLOR , PD.Size, PD.SizeUnitMeasureCode,
PPD.Description,PSC.Name AS SubCatgo_Name
FROM Production.ProductDescription PPD
13. 12
Inner join Production. ProductModelProductDescriptionCulture PMPDC
ON PPD.PRODUCTDESCRIPTIONID = PMPDC.PRODUCTDESCRIPTIONID
Inner Join
Production.ProductModel PM
ON
PMPDC.ProductModelID = PM.ProductModelID
INNER JOIN
PRODUCTION.Product PD
ON
PD.ProductModelID = PM.ProductModelID
INNER JOIN PRODUCTION.Culture CUL
ON
PMPDC.CultureID = CUL.CultureID
INNER JOIN
PRODUCTION.ProductSubcategory PSC
ON
PSC.ProductSubcategoryID = PD.ProductSubcategoryID
WHERE CUL.NAME = 'ENGLISH'
exec PRODUCTS;
ProductID NAME COLOR Size SizeUnitMeasureCode Description SubCatgo_Name
680 HL Road Frame - Black, 58 Black 58 CM Our lightest and best quality aluminum frame made from the newest alloy; it is welded and heat-treated for strength. Our innovative design results in maximum comfort and performance. Road Frames
706 HL Road Frame - Red, 58 Red 58 CM Our lightest and best quality aluminum frame made from the newest alloy; it is welded and heat-treated for strength. Our innovative design results in maximum comfort and performance. Road Frames
707 Sport-100 Helmet, Red Red Universal fit, well-vented, lightweight , snap-on visor. Helmets
708 Sport-100 Helmet, Black Black Universal fit, well-vented, lightweight , snap-on visor. Helmets
709 Mountain Bike Socks, M White M Combination of natural and synthetic fibers stays dry and provides just the right cushioning. Socks
710 Mountain Bike Socks, L White L Combination of natural and synthetic fibers stays dry and provides just the right cushioning. Socks
711 Sport-100 Helmet, Blue Blue Universal fit, well-vented, lightweight , snap-on visor. Helmets
712 AWC Logo Cap Multi Traditional style with a flip-up brim; one-size fits all. Caps
713 Long-Sleeve Logo Jersey, S Multi S Unisex long-sleeve AWC logo microfiber cycling jersey Jerseys
714 Long-Sleeve Logo Jersey, M Multi M Unisex long-sleeve AWC logo microfiber cycling jersey Jerseys
715 Long-Sleeve Logo Jersey, L Multi L Unisex long-sleeve AWC logo microfiber cycling jersey Jerseys
716 Long-Sleeve Logo Jersey, XL Multi XL Unisex long-sleeve AWC logo microfiber cycling jersey Jerseys
717 HL Road Frame - Red, 62 Red 62 CM Our lightest and best quality aluminum frame made from the newest alloy; it is welded and heat-treated for strength. Our innovative design results in maximum comfort and performance. Road Frames
718 HL Road Frame - Red, 44 Red 44 CM Our lightest and best quality aluminum frame made from the newest alloy; it is welded and heat-treated for strength. Our innovative design results in maximum comfort and performance. Road Frames
719 HL Road Frame - Red, 48 Red 48 CM Our lightest and best quality aluminum frame made from the newest alloy; it is welded and heat-treated for strength. Our innovative design results in maximum comfort and performance. Road Frames
720 HL Road Frame - Red, 52 Red 52 CM Our lightest and best quality aluminum frame made from the newest alloy; it is welded and heat-treated for strength. Our innovative design results in maximum comfort and performance. Road Frames
721 HL Road Frame - Red, 56 Red 56 CM Our lightest and best quality aluminum frame made from the newest alloy; it is welded and heat-treated for strength. Our innovative design results in maximum comfort and performance. Road Frames
Results Abridged
CREATE OR REPLACE PROCEDURE PRODUCTS2
(@sub_catgo_name char(35)
)
AS
SELECT PD.ProductID, PD.NAME, PD.COLOR , PD.Size, PD.SizeUnitMeasureCode,
PPD.Description,PSC.Name AS SubCatgo_Name
FROM Production.ProductDescription PPD
Inner join Production. ProductModelProductDescriptionCulture PMPDC
ON PPD.PRODUCTDESCRIPTIONID = PMPDC.PRODUCTDESCRIPTIONID
Inner Join
Production.ProductModel PM
ON
PMPDC.ProductModelID = PM.ProductModelID
INNER JOIN
PRODUCTION.Product PD
ON
PD.ProductModelID = PM.ProductModelID
INNER JOIN PRODUCTION.Culture CUL
ON
PMPDC.CultureID = CUL.CultureID
INNER JOIN
PRODUCTION.ProductSubcategory PSC
ON
PSC.ProductSubcategoryID = PD.ProductSubcategoryID
WHERE CUL.NAME = 'ENGLISH'
AND PSC.NAME = @sub_catgo_name;
Exec PRODUCTS2( ‘Jerseys’);
ProductID NAME COLOR Size SizeUnitMeasureCode Description SubCatgo_Name
713 Long-Sleeve Logo Jersey, S Multi S Unisex long-sleeve AWC logo microfiber cycling jersey Jerseys
714 Long-Sleeve Logo Jersey, M Multi M Unisex long-sleeve AWC logo microfiber cycling jersey Jerseys
715 Long-Sleeve Logo Jersey, L Multi L Unisex long-sleeve AWC logo microfiber cycling jersey Jerseys
716 Long-Sleeve Logo Jersey, XL Multi XL Unisex long-sleeve AWC logo microfiber cycling jersey Jerseys
881 Short-Sleeve Classic Jersey, S Yellow S Short sleeve classic breathable jersey with superior moisture control, front zipper, and 3 back pockets. Jerseys
882 Short-Sleeve Classic Jersey, M Yellow M Short sleeve classic breathable jersey with superior moisture control, front zipper, and 3 back pockets. Jerseys
883 Short-Sleeve Classic Jersey, L Yellow L Short sleeve classic breathable jersey with superior moisture control, front zipper, and 3 back pockets. Jerseys
884 Short-Sleeve Classic Jersey, XL Yellow XL Short sleeve classic breathable jersey with superior moisture control, front zipper, and 3 back pockets. Jerseys
/* Sybase Watcon SQL Syntax*/
CREATE OR REPLACE PROCEDURE SALES_TERRITORY_GB(
14. 13
in @COUNTRYCODE char(8))
RESULT(SalesOrderNumber NVARCHAR(25),AccountNumber VARCHAR (10),OrderDate DATE ,DueDate DATE,ShipDate DATE,ProductID INTEGER,
CustomerID INTEGER,SubTotal DECIMAL(35,2),TaxAmt DECIMAL (35,2),Freight DECIMAL(35,2),TotalDue DECIMAL(32,2),
PRODUCTNAME NAME,"Description" NVARCHAR(400),COLOR NVARCHAR (15),"Size" NVARCHAR(5),
SizeUnitMeasureCode NVARCHAR (3),SubCatgoName NAME,ProductSubcategoryID INTEGER,CategoryName NAME,ProductCategoryID INTEGER ,
TerritoryName NAME,CountryRegionCode NVARCHAR (3),TerritoryGroup NVARCHAR (50),TerritoryID INTEGER )
--on exception resume--
begin
CASE WHEN @COUNTRYCODE = 'GB'
THEN
select SalesOrderNumber,AccountNumber,OrderDate,DueDate,ShipDate,ProductID,
CustomerID,SubTotal,TaxAmt,Freight,TotalDue,
PRODUCTNAME,Description,COLOR,Size,SizeUnitMeasureCode,SubCatgoName,ProductSubcategoryID,CategoryName,ProductCategoryID,
TerritoryName,CountryRegionCode,TerritoryGroup,TerritoryID
from SalesReports
where CountryRegionCode = @COUNTRYCODE
END CASE;
End
EXEC SALES_TERRITORY_GB('US')
Execution time: 0.064 seconds
EXEC SALES_TERRITORY_GB('UK')
SalesOrderNumber OrderDate DueDate ShipDate ProductID CustomerID SubTotal TaxAmt Freight TotalDue ProductName TerritoryName CountryRegionCode TerritoryGroup TerritoryID
SO73803 2004-06-26 2004-07-08 2004-07-03 879 16938 928.49 74.28 23.21 1025.98 All-Purpose Bike Stand United Kingdom GB Europe 10
SO70784 2004-05-17 2004-05-29 2004-05-24 879 14156 195.59 15.65 4.89 216.13 All-Purpose Bike Stand United Kingdom GB Europe 10
SO71313 2004-05-25 2004-06-06 2004-06-01 879 16325 166.28 13.30 4.16 183.74 All-Purpose Bike Stand United Kingdom GB Europe 10
SO74525 2004-07-12 2004-07-24 2004-07-19 879 13830 195.59 15.65 4.89 216.13 All-Purpose Bike Stand United Kingdom GB Europe 10
SO69201 2004-04-28 2004-05-10 2004-05-05 879 25482 162.99 13.04 4.07 180.10 All-Purpose Bike Stand United Kingdom GB Europe 10
SO69205 2004-04-28 2004-05-10 2004-05-05 879 13664 195.27 15.62 4.88 215.77 All-Purpose Bike Stand United Kingdom GB Europe 10
SO55484 2003-10-03 2003-10-15 2003-10-10 879 17840 188.98 15.12 4.72 208.82 All-Purpose Bike Stand United Kingdom GB Europe 10
SO60357 2003-12-19 2003-12-31 2003-12-26 879 12513 182.78 14.62 4.57 201.97 All-Purpose Bike Stand United Kingdom GB Europe 10
SO57657 2003-11-09 2003-11-21 2003-11-16 879 25470 162.99 13.04 4.07 180.10 All-Purpose Bike Stand United Kingdom GB Europe 10
Results Abridged
CREATE OR REPLACE PROCEDURE SALES_TERRITORY_GB(
in @COUNTRYCODE char(8),
OUT SalesOrderNumber NVARCHAR(25),
OUT AccountNumber VARCHAR (10),
OUT OrderDate DATE ,
OUT DueDate DATE,
OUT ShipDate DATE,
OUT ProductID INTEGER,
OUT CustomerID INTEGER,
OUT SubTotal DECIMAL(35,2),
OUT TaxAmt DECIMAL (35,2),
OUT Freight DECIMAL(35,2),
OUT TotalDue DECIMAL(32,2),
OUT PRODUCTNAME NAME,
OUT "Description" NVARCHAR(400),
OUT COLOR NVARCHAR (15),
OUT "Size" NVARCHAR(5),
OUT SizeUnitMeasureCode NVARCHAR (3),
OUT SubCatgoName NAME,
OUT ProductSubcategoryID INTEGER,
OUT CategoryName NAME,
OUT ProductCategoryID INTEGER ,
OUT TerritoryName NAME,
OUT CountryRegionCode NVARCHAR (3),
OUT TerritoryGroup NVARCHAR (50),
OUT TerritoryID INTEGER )
--on exception resume--
begin
CASE WHEN @COUNTRYCODE = 'GB'
THEN
select SalesOrderNumber,AccountNumber,OrderDate,DueDate,ShipDate,ProductID,
CustomerID,SubTotal,TaxAmt,Freight,TotalDue,
PRODUCTNAME,Description,COLOR,Size,SizeUnitMeasureCode,SubCatgoName,ProductSubcategoryID,CategoryName,ProductCategoryID,
TerritoryName,CountryRegionCode,TerritoryGroup,TerritoryID
from SalesReports
where CountryRegionCode = @COUNTRYCODE
END CASE;
End
SalesOrderNumber OrderDate DueDate ShipDate ProductID CustomerID SubTotal TaxAmt Freight TotalDue ProductName TerritoryName CountryRegionCode TerritoryGroup TerritoryID
SO73803 2004-06-26 2004-07-08 2004-07-03 879 16938 928.49 74.28 23.21 1025.98 All-Purpose Bike Stand United Kingdom GB Europe 10
15. 14
SO70784 2004-05-17 2004-05-29 2004-05-24 879 14156 195.59 15.65 4.89 216.13 All-Purpose Bike Stand United Kingdom GB Europe 10
SO71313 2004-05-25 2004-06-06 2004-06-01 879 16325 166.28 13.30 4.16 183.74 All-Purpose Bike Stand United Kingdom GB Europe 10
SO74525 2004-07-12 2004-07-24 2004-07-19 879 13830 195.59 15.65 4.89 216.13 All-Purpose Bike Stand United Kingdom GB Europe 10
SO69201 2004-04-28 2004-05-10 2004-05-05 879 25482 162.99 13.04 4.07 180.10 All-Purpose Bike Stand United Kingdom GB Europe 10
SO69205 2004-04-28 2004-05-10 2004-05-05 879 13664 195.27 15.62 4.88 215.77 All-Purpose Bike Stand United Kingdom GB Europe 10
SO55484 2003-10-03 2003-10-15 2003-10-10 879 17840 188.98 15.12 4.72 208.82 All-Purpose Bike Stand United Kingdom GB Europe 10
SO60357 2003-12-19 2003-12-31 2003-12-26 879 12513 182.78 14.62 4.57 201.97 All-Purpose Bike Stand United Kingdom GB Europe 10
SO57657 2003-11-09 2003-11-21 2003-11-16 879 25470 162.99 13.04 4.07 180.10 All-Purpose Bike Stand United Kingdom GB Europe 10
Results Abridged
DDL
CREATE VIEW ORDERS
AS
SELECT c.CustomerID, c.AccountNumber, soh.SalesOrderID, convert (date, soh.DueDate) as DueDate, cast (soh.ShipDate as date) as ShipDate, soh.SalesOrderNumber,
soh.PurchaseOrderNumber,
j.Name as TerritoryName, soh.ShipMethodID,
cast( soh.SubTotal as decimal(35,2))as Subtotal, cast( soh.Freight as decimal(35,2))as Freight, cast(soh.TaxAmt as decimal(35,2))as TaxAmt , soh.TotalDue,
year( soh.OrderDate) ||'-'|| monthname( soh.OrderDate) ||'-'|| day( soh.OrderDate) ||'-'||dayname( soh.OrderDate)||','|| quarter( soh.OrderDate) as OrderDate
FROM Sales.Customer c
inner join
Sales.SalesOrderHeader soh
on c.CustomerID = soh.CustomerID
inner join
Sales.SalesOrderDetail sod
on soh.SalesOrderID = sod.SalesOrderID
inner join Sales.SalesTerritory j
on
soh.TerritoryID = j.TerritoryID
SELECT CustomerID AS CUSTOMER_ID, CAST (AVG(TotalDue)AS DECIMAL (35,2)) AS AVG_TOTAL_DUE, CAST (SUM(TOTALDUE)AS DECIMAL(35,2)) SUM_TOTAL,
CAST (MAX(TotalDue)AS DECIMAL(35,2))AS MAX_TOTAL_DUE, CAST (MIN(TotalDue)AS DECIMAL(35,2))AS MIN_TOTAL_DUE2
FROM ORDERS
GROUP BY CustomerID
CUSTOMER_ID AVG_TOTAL_DUE SUM_TOTAL MAX_TOTAL_DUE MIN_TOTAL_DUE
20818 1259.80 7558.83 3953.99 720.97
24921 1254.27 5017.08 1303.87 1105.48
12648 2596.35 20770.84 2699.01 2264.25
16727 2708.69 5417.37 2708.69 2708.69
29012 87.27 174.55 87.27 87.27
12655 2814.05 30954.56 3953.99 2673.06
20817 2275.34 4550.68 3953.99 596.69
24920 1219.70 3659.09 1276.81 1105.48
16726 8.04 16.09 8.04 8.04
29019 1890.64 5671.91 3953.99 858.96
20816 1257.95 7547.72 3953.99 718.75
12654 32.54 130.17 40.08 9.93
24927 211.35 845.41 211.35 211.35
29018 70.69 212.06 70.69 70.69
16725 15.45 30.90 15.45 15.45
29017 3953.99 3953.99 3953.99 3953.99
Results Abridged
SELECT SOH.SalesOrderNumber, CAST (SOH.OrderDate AS DATE) AS OrderDate , CAST (SOH.DueDate AS DATE) AS DueDate, CAST (SOH.ShipDate AS DATE) AS ShipDate, SOD.ProductID
,SOH.CUSTOMERID AS CustomerID, cast (SOH.SUBTOTAL as Decimal (35,2))AS SubTotal,
cast (SOH.TAXAMT as Decimal (35,2))AS TaxAmt, cast (SOH.FREIGHT as Decimal (35,2))AS Freight, cast (SOH.TOTALDUE as Decimal (35,2))AS TotalDue, PD.Name AS ProductName, ST.NAME as TerritoryName,ST.CountryRegionCode,
ST."Group" AS TerritoryGroup, ST.TerritoryID
INTO ORDERS_RECORD
FROM Sales.SalesOrderDetail SOD
INNER JOIN Sales.SalesOrderHeader SOH
ON
SOD.SalesOrderID = SOH.SalesOrderID
INNER JOIN Production.Product PD
ON SOD.PRODUCTID = PD.PRODUCTID
INNER JOIN
SALES.SalesTerritory ST
ON
SOH.TerritoryID = ST.TerritoryID
ORDER BY ST.CountryRegionCode
Note Group is a reserved word and to use that column with name group, a double quotation has to be embraced to allow Sybase SQL Anywhere database to process the query.
Select * from ORDERS_RECORD
SalesOrderNumber OrderDate DueDate ShipDate ProductID CustomerID SubTotal TaxAmt Freight TotalDue ProductName TerritoryName CountryRegionCode TerritoryGroup TerritoryID
SO43701 2001-07-01 2001-07-13 2001-07-08 773 11003 3399.99 272.00 85.00 3756.99 Mountain-100 Silver, 44 Australia AU Pacific 9
16. 15
SO43703 2001-07-02 2001-07-14 2001-07-09 749 16624 3578.27 286.26 89.46 3953.99 Road-150 Red, 62 Australia AU Pacific 9
SO43704 2001-07-02 2001-07-14 2001-07-09 778 11005 3374.99 270.00 84.37 3729.36 Mountain-100 Black, 48 Australia AU Pacific 9
SO43705 2001-07-02 2001-07-14 2001-07-09 771 11011 3399.99 272.00 85.00 3756.99 Mountain-100 Silver, 38 Australia AU Pacific 9
SO43709 2001-07-03 2001-07-15 2001-07-10 752 16351 3578.27 286.26 89.46 3953.99 Road-150 Red, 52 Australia AU Pacific 9
SO43710 2001-07-03 2001-07-15 2001-07-10 753 16517 3578.27 286.26 89.46 3953.99 Road-150 Red, 56 Australia AU Pacific 9
SO43715 2001-07-05 2001-07-17 2001-07-12 753 16483 3578.27 286.26 89.46 3953.99 Road-150 Red, 56 Australia AU Pacific 9
SO43716 2001-07-05 2001-07-17 2001-07-12 750 16529 3578.27 286.26 89.46 3953.99 Road-150 Red, 44 Australia AU Pacific 9
SO43717 2001-07-05 2001-07-17 2001-07-12 767 25249 699.10 55.93 17.48 772.50 Road-650 Black, 62 Australia AU Pacific 9
SO43724 2001-07-07 2001-07-19 2001-07-14 750 16520 3578.27 286.26 89.46 3953.99 Road-150 Red, 44 Australia AU Pacific 9
SO43727 2001-07-08 2001-07-20 2001-07-15 750 16607 3578.27 286.26 89.46 3953.99 Road-150 Red, 44 Australia AU Pacific 9
SO43731 2001-07-09 2001-07-21 2001-07-16 753 16629 3578.27 286.26 89.46 3953.99 Road-150 Red, 56 Australia AU Pacific 9
SO43732 2001-07-09 2001-07-21 2001-07-16 777 11025 3374.99 270.00 84.37 3729.36 Mountain-100 Black, 44 Australia AU Pacific 9
SO43735 2001-07-10 2001-07-22 2001-07-17 749 16522 3578.27 286.26 89.46 3953.99 Road-150 Red, 62 Australia AU Pacific 9
SO43736 2001-07-10 2001-07-22 2001-07-17 773 11002 3399.99 272.00 85.00 3756.99 Mountain-100 Silver, 44 Australia AU Pacific 9
SO43740 2001-07-11 2001-07-23 2001-07-18 751 16527 3578.27 286.26 89.46 3953.99 Road-150 Red, 48 Australia AU Pacific 9
SO43743 2001-07-12 2001-07-24 2001-07-19 774 11007 3399.99 272.00 85.00 3756.99 Mountain-100 Silver, 48 Australia AU Pacific 9
SO43744 2001-07-12 2001-07-24 2001-07-19 752 16631 3578.27 286.26 89.46 3953.99 Road-150 Red, 52 Australia AU Pacific 9
SO43745 2001-07-13 2001-07-25 2001-07-20 750 16514 3578.27 286.26 89.46 3953.99 Road-150 Red, 44 Australia AU Pacific 9
SO43746 2001-07-13 2001-07-25 2001-07-20 751 16616 3578.27 286.26 89.46 3953.99 Road-150 Red, 48 Australia AU Pacific 9
Results Abridged
select top(5) * from ORDERS_RECORD
SalesOrderNumber OrderDate DueDate ShipDate ProductID CustomerID SubTotal TaxAmt Freight TotalDue ProductName TerritoryName CountryRegionCode TerritoryGroup TerritoryID
SO43701 2001-07-01 2001-07-13 2001-07-08 773 11003 3399.99 272.00 85.00 3756.99 Mountain-100 Silver, 44 Australia AU Pacific 9
SO43703 2001-07-02 2001-07-14 2001-07-09 749 16624 3578.27 286.26 89.46 3953.99 Road-150 Red, 62 Australia AU Pacific 9
SO43704 2001-07-02 2001-07-14 2001-07-09 778 11005 3374.99 270.00 84.37 3729.36 Mountain-100 Black, 48 Australia AU Pacific 9
SO43705 2001-07-02 2001-07-14 2001-07-09 771 11011 3399.99 272.00 85.00 3756.99 Mountain-100 Silver, 38 Australia AU Pacific 9
SO43709 2001-07-03 2001-07-15 2001-07-10 752 16351 3578.27 286.26 89.46 3953.99 Road-150 Red, 52 Australia AU Pacific 9
Results Abridged
/*This table would be used to hold data relating to GB customers*/
Create table Territory_ReportGB
(SalesOrderNumber NVARCHAR(25),
ORDERDATE DATE,
DUEDATE DATE,
SHIPDATE DATE,
PRODUCTID INTEGER ,
CUSTOMERID INTEGER ,
SUBTOTAL DECIMAL (35,2),
TAXAMT DECIMAL(35,2),
FREIGHT DECIMAL(35,2),
TOTALDUE DECIMAL(35,2),
PRODUCTNAME NAME,
DESCRIPTION NVARCHAR(400),
COLOR NVARCHAR(15) ,
SIZE NVARCHAR(15) ,
SizeUnitMeasureCode NCHAR(3),
SubCatgoName NAME,
ProductSubcategoryID INTEGER,
CategoryName NAME ,
ProductCategoryID INTEGER,
TerritoryName NAME ,
CountryRegionCode NVARCHAR(3),
TerritoryGroup NVARCHAR(50),
TerritoryID INTEGER,
REPORT_ID INTEGER IDENTITY ,
REPORT_DATE TIMESTAMP default TIMESTAMP
);
ALTER TABLE Territory_ReportGB
ADD CONSTRAINT TERRITRPORT_RPID_PK PRIMARY KEY(REPORT_ID);
ALTER TABLE Territory_ReportGB
add AccountNumber varchar(30);
/* A stored procedure will call this view to populate the report table for GB customers*/
create view SalesReports
AS
SELECT SOH.SalesOrderNumber, CU.AccountNumber, CAST (SOH.OrderDate AS DATE) AS OrderDate , CAST (SOH.DueDate AS DATE) AS DueDate, CAST (SOH.ShipDate AS DATE) AS ShipDate, PD.ProductID
,SOH.CUSTOMERID AS CustomerID, cast (SOH.SUBTOTAL as Decimal (35,2))AS SubTotal,
cast (SOH.TAXAMT as Decimal (35,2))AS TaxAmt, cast (SOH.FREIGHT as Decimal (35,2))AS Freight, cast (SOH.TOTALDUE as Decimal (35,2))AS TotalDue,
PD.NAME AS PRODUCTNAME, PPD.Description,PD.COLOR,PD.Size, PD.SizeUnitMeasureCode,
PSC.Name AS SubCatgoName,PSC.ProductSubcategoryID,
PC.Name AS CategoryName, PC.ProductCategoryID,
ST.NAME as TerritoryName,ST.CountryRegionCode ,ST."Group" AS TerritoryGroup,
ST.TerritoryID
FROM Sales.SalesOrderDetail SOD
INNER JOIN Sales.SalesOrderHeader SOH
ON
SOD.SalesOrderID = SOH.SalesOrderID
INNER JOIN Production.Product PD
ON SOD.PRODUCTID = PD.PRODUCTID
17. 16
INNER JOIN
SALES.SalesTerritory ST
ON
SOH.TerritoryID = ST.TerritoryID
INNER JOIN
Production.ProductModel PM
ON
PM.ProductModelID = PD.ProductModelID
INNER JOIN
Production. ProductModelProductDescriptionCulture PMPDC
ON
PM.ProductModelID = PMPDC.ProductModelID
INNER JOIN
Production.ProductDescription PPD
ON
PPD.PRODUCTDESCRIPTIONID = PMPDC.PRODUCTDESCRIPTIONID
INNER JOIN PRODUCTION.Culture CUL
ON
PMPDC.CultureID = CUL.CultureID
INNER JOIN
PRODUCTION.ProductSubcategory PSC
ON
PSC.ProductSubcategoryID = PD.ProductSubcategoryID
INNER JOIN SALES.Customer CU
ON SOH.CustomerID = CU.CustomerID
INNER JOIN
PRODUCTION.ProductCategory PC
ON
PSC.ProductCategoryID = PC.ProductCategoryID;
/* Report audit table that would be used to keep tracks of the records inserted, deleted and updated to the territory report GB table*/
CREATE TABLE REPORT_ADUIT
(
SalesOrderNumber NVARCHAR(25),
ORDERDATE DATE,
DUEDATE DATE,
SHIPDATE DATE,
PRODUCTID INTEGER ,
CUSTOMERID INTEGER ,
SUBTOTAL DECIMAL (35,2),
TAXAMT DECIMAL(35,2),
FREIGHT DECIMAL(35,2),
TOTALDUE DECIMAL(35,2),
PRODUCTNAME NAME,
DESCRIPTION NVARCHAR(400),
COLOR NVARCHAR(15) ,
SIZE NVARCHAR(15) ,
SizeUnitMeasureCode NCHAR(3),
SubCatgoName NAME,
ProductSubcategoryID INTEGER,
CategoryName NAME ,
ProductCategoryID INTEGER,
TerritoryName NAME ,
CountryRegionCode NVARCHAR(3),
TerritoryGroup NVARCHAR(50),
TerritoryID INTEGER,
REPORT_ID INTEGER,
ADUIT_ID INTEGER IDENTITY ,
ADUIT_DATE TIMESTAMP DEFAULT TIMESTAMP,
REPORT_DATE TIMESTAMP
);
ALTER TABLE REPORT_ADUIT
ADD CONSTRAINT REPORTAD_ADID_PK PRIMARY KEY(ADUIT_ID);
ALTER TABLE REPORT_ADUIT
add AccountNumber varchar(30);
/* This table would hold data associated with customers from different regions */
Create table Territory_ReportS
(SalesOrderNumber NVARCHAR(25),
ORDERDATE DATE,
DUEDATE DATE,
SHIPDATE DATE,
PRODUCTID INTEGER ,
CUSTOMERID INTEGER ,
SUBTOTAL DECIMAL (35,2),
TAXAMT DECIMAL(35,2),
FREIGHT DECIMAL(35,2),
18. 17
TOTALDUE DECIMAL(35,2),
PRODUCTNAME NAME,
DESCRIPTION NVARCHAR(400),
COLOR NVARCHAR(15) ,
SIZE NVARCHAR(15) ,
SizeUnitMeasureCode NCHAR(3),
SubCatgoName NAME,
ProductSubcategoryID INTEGER,
CategoryName NAME ,
ProductCategoryID INTEGER,
TerritoryName NAME ,
CountryRegionCode NVARCHAR(3),
TerritoryGroup NVARCHAR(50),
TerritoryID INTEGER,
REPORT_ID INTEGER IDENTITY ,
REPORT_DATE TIMESTAMP default TIMESTAMP ,
AccountNumber varchar(30),
CONSTRAINT REPORTID_PK PRIMARY KEY(REPORT_ID)
);
INSERT INTO "DBA"."Territory_ReportGB"(SalesOrderNumber,AccountNumber, ORDERDATE,DUEDATE,SHIPDATE, PRODUCTID,
CUSTOMERID ,SUBTOTAL,TAXAMT,FREIGHT, TOTALDUE,
PRODUCTNAME,"DESCRIPTION",COLOR,"SIZE",SizeUnitMeasureCode,SubCatgoName,
ProductSubcategoryID, CategoryName, ProductCategoryID, TerritoryName,
CountryRegionCode,TerritoryGroup,TerritoryID)
SELECT SalesOrderNumber, AccountNumber, OrderDate , DueDate, ShipDate, ProductID,
CustomerID, SubTotal,TaxAmt, Freight, TotalDue,
PRODUCTNAME, "Description","COLOR" ,"Size", SizeUnitMeasureCode,SubCatgoName,ProductSubcategoryID, CategoryName, ProductCategoryID,
TerritoryName,CountryRegionCode , TerritoryGroup, TerritoryID
FROM SalesReports
WHERE CountryRegionCode ='GB'
select * from REPORT_ADUIT
/*The null column has been removed to allow the report fit to the document*/
SalesOrderNu
mber
ORDERD
ATE
DUEDAT
E
SHIPDA
TE
PRODUC
TID
CUSTOME
RID
SUBTOT
AL
TAXA
MT
FREIG
HT
TOTALD
UE
PRODUCTN
AME
DESCRIPTION COL
OR
SubCatgoN
ame
ProductSubcateg
oryID
CategoryN
ame
ProductCateg
oryID
TerritoryN
ame
CountryRegion
Code
TerritoryGr
oup
Territor
yID
REPORT
_ID
ADUIT
_ID
ADUIT_D
ATE
REPORT_D
ATE
SO57240 11/1/200
3
11/13/2
003
11/8/20
03
712 11238 562.96 45.04 14.07 622.07 AWC Logo
Cap
经典式样,带有翻边;
均码。
Multi Caps 19 Clothing 3 United
Kingdom
GB Europe 10 8 3 5/6/2013
0:02
5/6/2013
0:02
SO57240 11/1/200
3
11/13/2
003
11/8/20
03
712 11238 562.96 45.04 14.07 622.07 AWC Logo
Cap
Multi Caps 19 Clothing 3 United
Kingdom
GB Europe 10 9 4 5/6/2013
0:02
5/6/2013
0:02
Results Abridged
/* A trigger will populate the territory reports for GB customer*/
INSERT INTO Territory_ReportS(SalesOrderNumber,AccountNumber, ORDERDATE,DUEDATE,SHIPDATE, PRODUCTID,
CUSTOMERID ,SUBTOTAL,TAXAMT,FREIGHT, TOTALDUE,
PRODUCTNAME,"DESCRIPTION",COLOR,"SIZE",SizeUnitMeasureCode,SubCatgoName,
ProductSubcategoryID, CategoryName, ProductCategoryID, TerritoryName,
CountryRegionCode,TerritoryGroup,TerritoryID)
SELECT SalesOrderNumber, AccountNumber, OrderDate , DueDate, ShipDate, ProductID,
CustomerID, SubTotal,TaxAmt, Freight, TotalDue,
PRODUCTNAME, "Description","COLOR" ,"Size", SizeUnitMeasureCode,SubCatgoName,ProductSubcategoryID, CategoryName, ProductCategoryID,
TerritoryName,CountryRegionCode , TerritoryGroup, TerritoryID
FROM SalesReports
WHERE CountryRegionCode ='GB'
INSERT INTO Territory_ReportS(SalesOrderNumber,AccountNumber, ORDERDATE,DUEDATE,SHIPDATE, PRODUCTID,
CUSTOMERID ,SUBTOTAL,TAXAMT,FREIGHT, TOTALDUE,
PRODUCTNAME,"DESCRIPTION",COLOR,"SIZE",SizeUnitMeasureCode,SubCatgoName,
ProductSubcategoryID, CategoryName, ProductCategoryID, TerritoryName,
CountryRegionCode,TerritoryGroup,TerritoryID)
SELECT SalesOrderNumber, AccountNumber, OrderDate , DueDate, ShipDate, ProductID,
19. 18
CustomerID, SubTotal,TaxAmt, Freight, TotalDue,
PRODUCTNAME, "Description","COLOR" ,"Size", SizeUnitMeasureCode,SubCatgoName,ProductSubcategoryID, CategoryName, ProductCategoryID,
TerritoryName,CountryRegionCode , TerritoryGroup, TerritoryID
FROM SalesReports
WHERE CountryRegionCode ='US'
select TOP(2) * from
Territory_ReportGB;
SalesOrder
Number
ORDER
DATE
DUED
ATE
SHIPD
ATE
PRODU
CTID
CUSTO
MERID
SUBT
OTAL
TAX
AMT
FREI
GHT
TOTA
LDUE
PRODUC
TNAME
DESCRIPTION COL
OR
SI
ZE
SizeUnitMea
sureCode
SubCatg
oName
ProductSubc
ategoryID
Category
Name
ProductCat
egoryID
Territor
yName
CountryReg
ionCode
Territory
Group
Territo
ryID
REPOR
T_ID
REPORT
_DATE
AccountN
umber
SO51411 2003-07-
15
2003-
07-27
2003-
07-22
712 11239 2353.95 188.3
2
58.85 2601.11 AWC Logo
Cap
经典式样,带有
翻边;均码。
Multi Caps 19 Clothing 3 United
Kingdom
GB Europe 10 88 2013-05-
09
16:55:53.7
63
AW00011
239
SO51411 2003-07-
15
2003-
07-27
2003-
07-22
712 11239 2353.95 188.3
2
58.85 2601.11 AWC Logo
Cap
עם מסורתי סגנון
גודל ;מתרוממת מצחייה
.לכולם מתאים אחד
Multi Caps 19 Clothing 3 United
Kingdom
GB Europe 10 89 2013-05-
09
16:55:53.7
64
AW00011
239
select * from Territory_ReportGB
WHERE CountryRegionCode = 'US'
SalesOrderNumber ORDERDATE DUEDATE SHIPDATE PRODUCTID CUSTOMERID SUBTOTAL TAXAMT FREIGHT TOTALDUE PRODUCTNAME DESCRIPTION
COLOR SIZE SizeUnitMeasureCode SubCatgoName ProductSubcategoryID CategoryName ProductCategoryID TerritoryName CountryRegionCode TerritoryGroup TerritoryID REPORT_ID
REPORT_DATE AccountNumber
------------------------- ---------- ---------- ---------- ----------- ----------- ------------------------------------- ------------------------------------- ------------------------------------- ------------------------------------- -------------------------------------------------- -----------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------- --------------- ------------------- -------------------------------------------------- -------------------- -------------------------------------------------- -------
---------- -------------------------------------------------- ----------------- -------------------------------------------------- ----------- ----------- ----------------------- ------------------------------
(0 rows)
Execution time: 0.208 seconds
select TOP(5) * from
Territory_ReportS
WHERE CountryRegionCode ='GB’
SalesOrder
Number
ORDER
DATE
DUED
ATE
SHIPD
ATE
PRODU
CTID
CUSTO
MERID
SUBT
OTAL
TAX
AMT
FREI
GHT
TOTA
LDUE
PRODUC
TNAME
DESCRIPTION COL
OR
SI
ZE
SizeUnitMea
sureCode
SubCatg
oName
ProductSubc
ategoryID
Categor
yName
ProductCat
egoryID
Territor
yName
CountryReg
ionCode
Territory
Group
Territo
ryID
REPO
RT_ID
REPORT
_DATE
AccountN
umber
SO57240 2003-11-
01
2003-
11-13
2003-
11-08
712 11238 562.96 45.04 14.07 622.07 AWC Logo
Cap
经典式样,带有翻边
;均码。
Mult
i
Caps 19 Clothing 3 United
Kingdom
GB Europe 10 4 2013-05-
09
16:55:53.5
52
AW00011
238
SO57240 2003-11-
01
2003-
11-13
2003-
11-08
712 11238 562.96 45.04 14.07 622.07 AWC Logo
Cap
מצחייה עם מסורתי סגנון
מתאים אחד גודל ;מתרוממת
.לכולם
Mult
i
Caps 19 Clothing 3 United
Kingdom
GB Europe 10 5 2013-05-
09
16:55:53.6
56
AW00011
238
SO57240 2003-11-
01
2003-
11-13
2003-
11-08
712 11238 562.96 45.04 14.07 622.07 AWC Logo
Cap
Mult
i
Caps 19 Clothing 3 United
Kingdom
GB Europe 10 6 2013-05-
09
16:55:53.6
57
AW00011
238
SO57240 2003-11-
01
2003-
11-13
2003-
11-08
712 11238 562.96 45.04 14.07 622.07 AWC Logo
Cap
Style classique avec
une visière relevable ;
taille unique.
Mult
i
Caps 19 Clothing 3 United
Kingdom
GB Europe 10 7 2013-05-
09
16:55:53.6
60
AW00011
238
SO57240 2003-11-
01
2003-
11-13
2003-
11-08
712 11238 562.96 45.04 14.07 622.07 AWC Logo
Cap
رو ٍذي ه ق ت مط و ح حاف
،هى ألع البق الو ن هح ات ق
م الئ ٌ واحذ حجم ت
.ٍع جم ان
Mult
i
Caps 19 Clothing 3 United
Kingdom
GB Europe 10 8 2013-05-
09
16:55:53.6
61
AW00011
238
select TOP(5) * from
Territory_ReportS
WHERE CountryRegionCode ='US'
alesOrde
rNumber
ORDE
RDAT
E
DUE
DAT
E
SHIP
DAT
E
PROD
UCTI
D
CUSTO
MERID
SUBT
OTAL
TAX
AMT
FREI
GHT
TOTA
LDUE
PRODU
CTNAM
E
DESCRIPTION CO
LO
R
SI
Z
E
SizeUnitMe
asureCode
SubCat
goName
ProductSub
categoryID
Catego
ryNam
e
ProductC
ategoryID
Territo
ryName
CountryR
egionCode
Territor
yGroup
Territ
oryID
REPO
RT_ID
REPOR
T_DAT
E
Account
Number
SO43670 2001-
07-01
2001-
07-13
2001-
07-08
773 29566 7344.5
0
587.5
6
183.6
1
8115.6
8
Mountain-
100
Silver, 44 HL
Silv
er
44 CM Mountai
n Bikes
1 Bikes 1 Central US North
America
3 62796 2013-05-
09
17:06:14.
134
AW0002
9566
SO43670 2001-
07-01
2001-
07-13
2001-
07-08
773 29566 7344.5
0
587.5
6
183.6
1
8115.6
8
Mountain-
100
Silver, 44
VTT de compétition haut de gamme. Plusieurs
options d'amélioration des performances : cadre HL,
suspension avant particulièrement souple et traction
adaptée à tous les terrains.
Silv
er
44 CM Mountai
n Bikes
1 Bikes 1 Central US North
America
3 62797 2013-05-
09
17:06:14.
134
AW0002
9566
SO43670 2001-
07-01
2001-
07-13
2001-
07-08
773 29566 7344.5
0
587.5
6
183.6
1
8115.6
8
Mountain-
100
Silver, 44
هى أع مه ثال ج ان ً ف ٍادج ق ه ن صح ص مخ اخ ثاق س دراجح
emarF LH كم ٍ ه األداء ٍه س ح ت ٍاراخ خ ضمه ت ت .طراز
وج وق ،قح فائ ان سح الس ان ري ًاماألم ٍق ه عت وان ،ًذاع اإلت
.ًض األرا واع أو ٍع جم ن ثح س ىا م ان جر ان
Silv
er
44 CM Mountai
n Bikes
1 Bikes 1 Central US North
America
3 62798 2013-05-
09
17:06:14.
134
AW0002
9566
20. 19
SO43670 2001-
07-01
2001-
07-13
2001-
07-08
773 29566 7344.5
0
587.5
6
183.6
1
8115.6
8
Mountain-
100
Silver, 44
Top-of-the-line competition mountain bike.
Performance-enhancing options include the
innovative HL Frame, super-smooth front suspension,
and traction for all terrain.
Silv
er
44 CM Mountai
n Bikes
1 Bikes 1 Central US North
America
3 62799 2013-05-
09
17:06:14.
134
AW0002
9566
SO43670 2001-
07-01
2001-
07-13
2001-
07-08
776 29566 7344.5
0
587.5
6
183.6
1
8115.6
8
Mountain-
100
Black, 42
高档竞赛山地车。性能得到进一步增强,包括创
新的 HL
车架、极其平稳的前悬架以及适用于所有地形的
出色牵引力。
Blac
k
42 CM Mountai
n Bikes
1
CREATE or replace VIEW PRODUCTH
AS
SELECT PD.ProductID, PD.NAME, PD.COLOR , PD.Size, PD.SizeUnitMeasureCode,
PPD.Description,PSC.Name AS SubCatgo_Name, PSC.ProductSubcategoryID AS SubCatgo_ID,
PPI.Quantity,PPI.Shelf, PPI.LocationID, CAST(PPI.ModifiedDate AS DATE)AS ModifiedDate_SubCatgo,
PPI.Bin ,CUL.Name AS Culture_Name,PCI.Name AS Catego_Name, PCI.ProductCategoryID , cast( PCI.ModifiedDate as date)AS ModifiedDate_Catego
FROM Production.ProductDescription PPD
Inner join Production. ProductModelProductDescriptionCulture PMPDC
ON PPD.PRODUCTDESCRIPTIONID = PMPDC.PRODUCTDESCRIPTIONID
Inner Join
Production.ProductModel PM
ON
PMPDC.ProductModelID = PM.ProductModelID
INNER JOIN
PRODUCTION.Product PD
ON
PD.ProductModelID = PM.ProductModelID
INNER JOIN PRODUCTION.Culture CUL
ON
PMPDC.CultureID = CUL.CultureID
INNER JOIN
PRODUCTION.ProductSubcategory PSC
ON
PSC.ProductSubcategoryID = PD.ProductSubcategoryID
INNER JOIN
PRODUCTION.ProductInventory PPI
ON
PD.ProductID = PPI.ProductID
INNER JOIN
PRODUCTION.ProductCategory PCI
ON
PSC.ProductCategoryID = PCI.ProductCategoryID
CREATE TABLE PRODHISTORY
(Product_ID INTEGER,
Product_Name NAME,
Product_Color Nvarchar(15),
Product_Size Nvarchar(5),
SizeUnitMeasureCode nchar(3),
Product_Description nvarchar(400),
SubCatgo_Name name,
Quantity char(8),
Shelf nvarchar(10),
LocationID char(8),
ModifiedDate_SubCatgo Timestamp,
P_Bin CHAR(8),
Culture_Name NAME,
History_Date Timestamp Default Timestamp ,
History_ID INTEGER IDENTITY ,
Category_Name Name,
ModifiedDate_Catgo Timestamp,
SubCatgo_ID integer,
Prod_CatgoID INTEGER,
constraint PHISTORY_PRODID2_PK PRIMARY KEY (HISTORY_ID)
)
/*This stored procedure will populate the above table*/
CREATE OR REPLACE PROCEDURE ProdHistory(
in @ProductID INTEGER
)
BEGIN
IF @ProductID IS NOT NULL
THEN
INSERT INTO PRODHISTORY (Product_ID, Product_Name ,Product_Color , Product_Size , SizeUnitMeasureCode , Product_Description,
SubCatgo_Name, Quantity ,Shelf ,LocationID , ModifiedDate_SubCatgo, P_Bin , Culture_Name, Category_Name,
21. 20
ModifiedDate_Catgo, SubCatgo_ID,Prod_CatgoID)
SELECT ProductID, "Name" ,Color , "Size" , SizeUnitMeasureCode , "Description",
SubCatgo_Name,Quantity, Shelf, LocationID, ModifiedDate_SubCatgo,
Bin ,Culture_Name,Catego_Name, ModifiedDate_Catego,SubCatgo_ID,
ProductCategoryID
FROM PRODUCTH
WHERE ProductID = @ProductID ;
END IF ;
End
SELECT * FROM PRODHISTORY
Product_I
D
Product_Nam
e
Product_Colo
r
Product_Siz
e
SizeUnitMeasur
eCode
Product_Description SubCatgo_Nam
e
Quantit
y
Shel
f
LocationI
D
ModifiedDate_SubCatg
o
P_Bi
n
Culture_Nam
e
History_Dat
e
History_I
D
Category_Nam
e
ModifiedDate_Catg
o
SubCatgo_I
D
Prod_CatgoI
D
865 Classic Vest,
M
Blue M Light-weight, wind-
resistant, packs to fit into
a pocket.
Vests 216 N/A 7 2003-06-01
00:00:00.000
0 English 2013-05-09
21:06:18.654
1 Clothing 1998-06-01
00:00:00.000
25 3
865 Classic Vest,
M
Blue M قاومح وم ،وزن ان فح ٍ ف خ هة ع
حجم سة ىا ت ،ح ٌهر ن
.ٍة ج ان
Vests 216 N/A 7 2003-06-01
00:00:00.000
0 Arabic 2013-05-09
21:06:18.655
2 Clothing 1998-06-01
00:00:00.000
25 3
865 Classic Vest,
M
Blue M Sacs légers et résistants au
vent ; tiennent dans la
poche.
Vests 216 N/A 7 2003-06-01
00:00:00.000
0 French 2013-05-09
21:06:18.655
3 Clothing 1998-06-01
00:00:00.000
25 3
865 Classic Vest,
M
Blue M Vests 216 N/A 7 2003-06-01
00:00:00.000
0 Thai 2013-05-09
21:06:18.655
4 Clothing 1998-06-01
00:00:00.000
25 3
865 Classic Vest,
M
Blue M -קל מתקפל ,מרוח מגן ,משקל
.לכיס המתאים לגודל
Vests 216 N/A 7 2003-06-01
00:00:00.000
0 Hebrew 2013-05-09
21:06:18.655
5 Clothing 1998-06-01
00:00:00.000
25 3
865 Classic Vest,
M
Blue M 轻型抗风,可折叠放入
口袋。
Vests 216 N/A 7 2003-06-01
00:00:00.000
0 Chinese 2013-05-09
21:06:18.656
6 Clothing 1998-06-01
00:00:00.000
25 3
Triggers
CREATE OR REPLACE TRIGGER T_Reports
BEFORE INSERT ON Territory_ReportS
REFERENCING NEW AS N
FOR EACH ROW
BEGIN
SET self_recursion OFF;
CASE WHEN N.CountryRegionCode ='GB'
THEN
INSERT INTO Territory_ReportGB
(SalesOrderNumber,AccountNumber, ORDERDATE,DUEDATE,SHIPDATE, PRODUCTID,
CUSTOMERID ,SUBTOTAL,TAXAMT,FREIGHT, TOTALDUE,
PRODUCTNAME,DESCRIPTION,COLOR,SIZE,SizeUnitMeasureCode,SubCatgoName,
ProductSubcategoryID, CategoryName, ProductCategoryID, TerritoryName,
CountryRegionCode,TerritoryGroup,TerritoryID,REPORT_ID,
REPORT_DATE
)
VALUES(N.SalesOrderNumber,N.AccountNumber,N.ORDERDATE,N.DUEDATE,N.SHIPDATE, N.PRODUCTID,
N.CUSTOMERID ,N.SUBTOTAL,N.TAXAMT,N.FREIGHT, N.TOTALDUE,
N.PRODUCTNAME,N.DESCRIPTION,N.COLOR,N.SIZE,N.SizeUnitMeasureCode,N.SubCatgoName,
N.ProductSubcategoryID, N.CategoryName, N.ProductCategoryID, N.TerritoryName,
N.CountryRegionCode,N.TerritoryGroup,N.TerritoryID,N.REPORT_ID,
N.REPORT_DATE);
END CASE
END
CREATE OR REPLACE TRIGGER TERRITORYRPT_GB
AFTER INSERT ON Territory_ReportGB
REFERENCING NEW AS N
FOR EACH ROW
BEGIN
SET self_recursion OFF;
INSERT INTO REPORT_ADUIT
(SalesOrderNumber,AccountNumber, ORDERDATE,DUEDATE,SHIPDATE, PRODUCTID,
CUSTOMERID ,SUBTOTAL,TAXAMT,FREIGHT, TOTALDUE,
PRODUCTNAME,DESCRIPTION,COLOR,SIZE,SizeUnitMeasureCode,SubCatgoName,
ProductSubcategoryID, CategoryName, ProductCategoryID, TerritoryName,
CountryRegionCode,TerritoryGroup,TerritoryID,REPORT_ID,
REPORT_DATE
)
VALUES(N.SalesOrderNumber,N.AccountNumber,N.ORDERDATE,N.DUEDATE,N.SHIPDATE, N.PRODUCTID,
N.CUSTOMERID ,N.SUBTOTAL,N.TAXAMT,N.FREIGHT, N.TOTALDUE,
N.PRODUCTNAME,N.DESCRIPTION,N.COLOR,N.SIZE,N.SizeUnitMeasureCode,N.SubCatgoName,
22. 21
N.ProductSubcategoryID, N.CategoryName, N.ProductCategoryID, N.TerritoryName,
N.CountryRegionCode,N.TerritoryGroup,N.TerritoryID,N.REPORT_ID,
N.REPORT_DATE);
END
XML
SELECT PD.ProductID, PD.NAME, PD.COLOR , PD.Size, PD.SizeUnitMeasureCode,PSC.Name AS SubCatgo_Name
FROM Production.ProductDescription PPD
Inner join Production. ProductModelProductDescriptionCulture PMPDC
ON PPD.PRODUCTDESCRIPTIONID = PMPDC.PRODUCTDESCRIPTIONID
Inner Join
Production.ProductModel PM
ON
PMPDC.ProductModelID = PM.ProductModelID
INNER JOIN
PRODUCTION.Product PD
ON
PD.ProductModelID = PM.ProductModelID
INNER JOIN PRODUCTION.Culture CUL
ON
PMPDC.CultureID = CUL.CultureID
INNER JOIN
PRODUCTION.ProductSubcategory PSC
ON
PSC.ProductSubcategoryID = PD.ProductSubcategoryID
WHERE CUL.NAME = 'ENGLISH'
AND PD.ProductID = 713
FOR XML AUTO
forxml(2,'PD',forxml_sep(),'ProductID',xmltab."1",'NAME',xmltab."2",'COLOR',xmltab."3",'Size',xmltab."4",'SizeUnitMeasureCode',xmltab."5",'PSC',forxml_sep(),'SubCatgo_Name',xmltab."6")
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<PD ProductID="713" NAME="Long-Sleeve Logo Jersey, S" COLOR="Multi" Size="S"><PSC SubCatgo_Name="Jerseys"/></PD>
(1 rows)
Execution time: 0.125 seconds
SELECT PD.ProductID, PD.NAME, PD.COLOR , PD.Size, PD.SizeUnitMeasureCode,PSC.Name AS SubCatgo_Name
FROM Production.ProductDescription PPD
Inner join Production. ProductModelProductDescriptionCulture PMPDC
ON PPD.PRODUCTDESCRIPTIONID = PMPDC.PRODUCTDESCRIPTIONID
Inner Join
Production.ProductModel PM
ON
PMPDC.ProductModelID = PM.ProductModelID
INNER JOIN
PRODUCTION.Product PD
ON
PD.ProductModelID = PM.ProductModelID
INNER JOIN PRODUCTION.Culture CUL
ON
PMPDC.CultureID = CUL.CultureID
INNER JOIN
PRODUCTION.ProductSubcategory PSC
ON
PSC.ProductSubcategoryID = PD.ProductSubcategoryID
WHERE CUL.NAME = 'ENGLISH'
AND PD.ProductID = 713
FOR XML RAW
forxml(1,'ProductID',xmltab."1",'NAME',xmltab."2",'COLOR',xmltab."3",'Size',xmltab."4",'SizeUnitMeasureCode',xmltab."5",'SubCatgo_Name',xmltab."6")
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<row ProductID="713" NAME="Long-Sleeve Logo Jersey, S" COLOR="Multi" Size="S" SubCatgo_Name="Jerseys"/>
(1 rows)
Execution time: 0.044 seconds
SELECT PD.ProductID, PD.NAME, PD.COLOR , PD.Size, PD.SizeUnitMeasureCode,PSC.Name AS SubCatgo_Name
FROM Production.ProductDescription PPD
Inner join Production. ProductModelProductDescriptionCulture PMPDC
ON PPD.PRODUCTDESCRIPTIONID = PMPDC.PRODUCTDESCRIPTIONID
Inner Join
Production.ProductModel PM
ON
PMPDC.ProductModelID = PM.ProductModelID
INNER JOIN
PRODUCTION.Product PD
ON
PD.ProductModelID = PM.ProductModelID
INNER JOIN PRODUCTION.Culture CUL
ON
PMPDC.CultureID = CUL.CultureID
INNER JOIN
PRODUCTION.ProductSubcategory PSC
23. 22
ON
PSC.ProductSubcategoryID = PD.ProductSubcategoryID
WHERE CUL.NAME = 'ENGLISH'
AND PD.ProductID = 713
FOR XML RAW, ELEMENTS
forxml(33,'ProductID',xmltab."1",'NAME',xmltab."2",'COLOR',xmltab."3",'Size',xmltab."4",'SizeUnitMeasureCode',xmltab."5",'SubCatgo_Name',xmltab."6")
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<row><ProductID>713</ProductID><NAME>Long-Sleeve Logo Jersey, S</NAME><COLOR>Multi</COLOR><Size>S</Size><SubCatgo_Name>Jerseys</SubCatgo_Name></row>
(1 rows)
Execution time: 0.065 seconds
SELECT ProductID, xmlelement(NAME DESCRIPTION,
xmlforest(
PD.ProductID, PD.NAME, PD.COLOR , PD.Size, PD.SizeUnitMeasureCode,PSC.Name AS SubCatgo_Name
) ) AS Prod_Info
FROM Production.ProductDescription PPD
Inner join Production. ProductModelProductDescriptionCulture PMPDC
ON PPD.PRODUCTDESCRIPTIONID = PMPDC.PRODUCTDESCRIPTIONID
Inner Join
Production.ProductModel PM
ON
PMPDC.ProductModelID = PM.ProductModelID
INNER JOIN
PRODUCTION.Product PD
ON
PD.ProductModelID = PM.ProductModelID
INNER JOIN PRODUCTION.Culture CUL
ON
PMPDC.CultureID = CUL.CultureID
INNER JOIN
PRODUCTION.ProductSubcategory PSC
ON
PSC.ProductSubcategoryID = PD.ProductSubcategoryID
WHERE CUL.NAME = 'ENGLISH'
AND PD.ProductID = 713
FOR XML RAW, ELEMENTS
forxml(1,'ProductID',xmltab."1",'Prod_Info',xmltab."2")
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<row ProductID="713" Prod_Info="<DESCRIPTION><ProductID>713</ProductID><NAME>Long-Sleeve Logo Jersey,
S</NAME><COLOR>Multi</COLOR><Size>S</Size><SubCatgo_Name>Jerseys</SubCatgo_Name></DES
(1 rows)
Execution time: 0.029 seconds
SELECT ProductID, xmlelement(NAME DESCRIPTION,
xmlforest(
PD.ProductID, PD.NAME, PD.COLOR , PD.Size, PD.SizeUnitMeasureCode,PSC.Name AS SubCatgo_Name
) ) AS Prod_Info
FROM Production.ProductDescription PPD
Inner join Production. ProductModelProductDescriptionCulture PMPDC
ON PPD.PRODUCTDESCRIPTIONID = PMPDC.PRODUCTDESCRIPTIONID
Inner Join
Production.ProductModel PM
ON
PMPDC.ProductModelID = PM.ProductModelID
INNER JOIN
PRODUCTION.Product PD
ON
PD.ProductModelID = PM.ProductModelID
INNER JOIN PRODUCTION.Culture CUL
ON
PMPDC.CultureID = CUL.CultureID
INNER JOIN
PRODUCTION.ProductSubcategory PSC
ON
PSC.ProductSubcategoryID = PD.ProductSubcategoryID
WHERE CUL.NAME = 'ENGLISH'
AND PD.ProductID = 713
ProductID Prod_Info
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
713 <DESCRIPTION><ProductID>713</ProductID><NAME>Long-Sleeve Logo Jersey, S</NAME><COLOR>Multi</COLOR><Size>S</Size><SubCatgo_Name>Jerseys</SubCatgo_Name></DESCRIPTION>
(1 rows)
Execution time: 0.036 seconds
Cursors
CREATE VIEW EMOINFO
AS
SELECT HRE.BusinessEntityID,PER.FirstName, PER.MiddleName , PER.LastName,
HRE.BirthDate, HRE.Gender, HRE.MaritalStatus, HRE.NationalIDNumber,
HRE.SickLeaveHours, HRE.VacationHours,
DATEDIFF(YY, BirthDate , CURRENT_TIMESTAMP) AS AGE
FROM HumanResources.Employee HRE
24. 23
INNER JOIN PERSON.Person PER
ON
HRE.BusinessEntityID = PER.BusinessEntityID
BEGIN
DECLARE @BusinessEntityID INTEGER;
DECLARE @EMP_NMAME VARCHAR(55);
DECLARE @BDATE DATE;
DECLARE @GENDER CHAR(2);
DECLARE @MSTATUS CHAR(8);
DECLARE @NATIONALID INTEGER;
DECLARE @SICKH INTEGER;
DECLARE @VHOURS INTEGER;
DECLARE @EMPAGE INTEGER;
DECLARE @EMPRED CURSOR FOR
SELECT BusinessEntityID,FirstName||','|| MiddleName||','||LastName as Emp_Name,
BirthDate, Gender, MaritalStatus, NationalIDNumber,
SickLeaveHours, VacationHours,AGE
FROM EMOINFO
where BusinessEntityID = 19;
OPEN @EMPRED;
fetch next @EMPRED
into @BusinessEntityID, @EMP_NMAME, @BDATE, @GENDER,@MSTATUS, @NATIONALID, @SICKH,@VHOURS, @EMPAGE;
select @BusinessEntityID , @EMP_NMAME , @BDATE , @GENDER ,@MSTATUS , @NATIONALID , @SICKH ,@VHOURS,
@EMPAGE ;
CLOSE @EMPRED;
END;
@BusinessEntityID @EMP_NMAME @BDATE @GENDER @MSTATUS @NATIONALID @SICKH @VHOURS @EMPAGE
----------------- ------------------------------------------------------- ---------- ------- -------- ----------- ----------- ----------- -----------
19 Mary,A,Dempsey 1968-03-01 F S 52541318 41 43 45
Execution time: 0.047 seconds
25. 24
DBA
Copying data from Sybase SQL AnyWhere AdventureWorks Database to Sybase SQL AnyWhere Rock Database using Sybase Central
27. 26
SELECT * FROM "SUNNY"."PRODHISTORY"
Product_I
D
Product_Na
me
Product_Col
or
Product_Si
ze
SizeUnitMeasureC
ode
Product_Description SubCatgo_Na
me
Quantit
y
Shel
f
LocationI
D
ModifiedDate_SubCa
tgo
P_Bi
n
Culture_Na
me
History_Da
te
History_I
D
Category_Na
me
ModifiedDate_Cat
go
SubCatgo_I
D
Prod_CatgoI
D
865 Classic Vest,
M
Blue M Light-weight, wind-resistant, packs to
fit into a pocket.
Vests 216 N/A 7 2003-06-01
00:00:00.000
0 English 2013-05-09
21:06:18.65
4
1 Clothing 1998-06-01
00:00:00.000
25 3
865 Classic Vest,
M
Blue M ،ح ٌهر ن قاومح وم ،وزن ان فح ٍ ف خ هة ع
.ٍة ج ان حجم سة ىا ت
Vests 216 N/A 7 2003-06-01
00:00:00.000
0 Arabic 2013-05-09
21:06:18.65
5
2 Clothing 1998-06-01
00:00:00.000
25 3
865 Classic Vest,
M
Blue M Sacs légers et résistants au vent ;
tiennent dans la poche.
Vests 216 N/A 7 2003-06-01
00:00:00.000
0 French 2013-05-09
21:06:18.65
5
3 Clothing 1998-06-01
00:00:00.000
25 3
865 Classic Vest,
M
Blue M Vests 216 N/A 7 2003-06-01
00:00:00.000
0 Thai 2013-05-09
21:06:18.65
5
4 Clothing 1998-06-01
00:00:00.000
25 3
865 Classic Vest,
M
Blue M -קל המתאים לגודל מתקפל ,מרוח מגן ,משקל
.לכיס
Vests 216 N/A 7 2003-06-01
00:00:00.000
0 Hebrew 2013-05-09
21:06:18.65
5
5 Clothing 1998-06-01
00:00:00.000
25 3
865 Classic Vest,
M
Blue M 轻型抗风,可折叠放入口袋。 Vests 216 N/A 7 2003-06-01
00:00:00.000
0 Chinese 2013-05-09
21:06:18.65
6
6 Clothing 1998-06-01
00:00:00.000
25 3
select * from sysprocedure where PROC_NAME ='ProdHistory' OR
PROC_NAME ='PRODUCTS'
OR
PROC_NAME ='PRODUCTS2'
OR
PROC_NAME ='SALES_TERRITORY_GB'
OR
PROC_NAME = 'SalesTerritory_Report'
proc
_id
crea
tor
objec
t_id
proc_name proc_defn rema
rks
repli
cate
srv
id
source avg_num
_rows
avg_
cost
stats
353 1 4728 ProdHistory create procedure DBA.ProdHistory(
in @ProductID integer )
begin
if @ProductID is not null then
insert into PRODHISTORY( Product_ID,Product_Name,Product_Color,Product_Size,SizeUnitMeasureCode,Product_Description,
SubCatgo_Name,Quantity,Shelf,LocationID,ModifiedDate_SubCatgo,P_Bin,Culture_Name,Category_Name,
ModifiedDate_Catgo,SubCatgo_ID,Prod_CatgoID )
select ProductID,Name,Color,Size,SizeUnitMeasureCode,Description,
SubCatgo_Name,Quantity,Shelf,LocationID,ModifiedDate_SubCatgo,
Bin,Culture_Name,Catego_Name,ModifiedDate_Catego,SubCatgo_ID,
ProductCategoryID
from PRODUCTH
where ProductID = @ProductID
end if
end
N create PROCEDURE DBA."ProdHistory"(
in @ProductID INTEGER
)
BEGIN
IF @ProductID IS NOT NULL
THEN
INSERT INTO PRODHISTORY (Product_ID, Product_Name ,Product_Color ,
Product_Size , SizeUnitMeasureCode , Product_Description,
SubCatgo_Name, Quantity ,Shelf ,LocationID , ModifiedDate_SubCatgo, P_Bin ,
Culture_Name, Category_Name,
ModifiedDate_Catgo, SubCatgo_ID,Prod_CatgoID)
SELECT ProductID, "Name" ,Color , "Size" , SizeUnitMeasureCode , "Description",
SubCatgo_Name,Quantity, Shelf, LocationID, ModifiedDate_SubCatgo,
Bin ,Culture_Name,Catego_Name, ModifiedDate_Catego,SubCatgo_ID,
ProductCategoryID
FROM PRODUCTH
WHERE ProductID = @ProductID ;
END IF ;
End
Bin
ary
Dat
a
347 1 4384 PRODUCTS create procedure DBA.PRODUCTS
as
select PD.ProductID,PD.NAME,PD.COLOR,PD.Size,PD.SizeUnitMeasureCode,
PPD.Description,SubCatgo_Name=PSC.Name
from Production.ProductDescription as PPD
join Production.ProductModelProductDescriptionCulture as PMPDC
on PPD.PRODUCTDESCRIPTIONID = PMPDC.PRODUCTDESCRIPTIONID
join Production.ProductModel as PM
on PMPDC.ProductModelID = PM.ProductModelID
join PRODUCTION.Product as PD
on PD.ProductModelID = PM.ProductModelID
join PRODUCTION.Culture as CUL
on PMPDC.CultureID = CUL.CultureID
join PRODUCTION.ProductSubcategory as PSC
on PSC.ProductSubcategoryID = PD.ProductSubcategoryID
where CUL.NAME = 'ENGLISH'
N create PROCEDURE PRODUCTS()
AS
SELECT PD.ProductID, PD.NAME, PD.COLOR , PD.Size, PD.SizeUnitMeasureCode,
PPD.Description,PSC.Name AS SubCatgo_Name
FROM Production.ProductDescription PPD
Inner join Production. ProductModelProductDescriptionCulture PMPDC
ON PPD.PRODUCTDESCRIPTIONID = PMPDC.PRODUCTDESCRIPTIONID
Inner Join
Production.ProductModel PM
ON
PMPDC.ProductModelID = PM.ProductModelID
INNER JOIN
PRODUCTION.Product PD
ON
PD.ProductModelID = PM.ProductModelID
INNER JOIN PRODUCTION.Culture CUL
ON
PMPDC.CultureID = CUL.CultureID
INNER JOIN
PRODUCTION.ProductSubcategory PSC
ON
PSC.ProductSubcategoryID = PD.ProductSubcategoryID
Bin
ary
Dat
a
28. 27
WHERE CUL.NAME = 'ENGLISH'
348 1 4385 PRODUCTS2 create procedure DBA.PRODUCTS2(
@sub_catgo_name char(35) )
as
select PD.ProductID,PD.NAME,PD.COLOR,PD.Size,PD.SizeUnitMeasureCode,
PPD.Description,SubCatgo_Name=PSC.Name
from Production.ProductDescription as PPD
join Production.ProductModelProductDescriptionCulture as PMPDC
on PPD.PRODUCTDESCRIPTIONID = PMPDC.PRODUCTDESCRIPTIONID
join Production.ProductModel as PM
on PMPDC.ProductModelID = PM.ProductModelID
join PRODUCTION.Product as PD
on PD.ProductModelID = PM.ProductModelID
join PRODUCTION.Culture as CUL
on PMPDC.CultureID = CUL.CultureID
join PRODUCTION.ProductSubcategory as PSC
on PSC.ProductSubcategoryID = PD.ProductSubcategoryID
where CUL.NAME = 'ENGLISH'
and PSC.NAME = @sub_catgo_name
N create PROCEDURE PRODUCTS2
(@sub_catgo_name char(35)
)
AS
SELECT PD.ProductID, PD.NAME, PD.COLOR , PD.Size, PD.SizeUnitMeasureCode,
PPD.Description,PSC.Name AS SubCatgo_Name
FROM Production.ProductDescription PPD
Inner join Production. ProductModelProductDescriptionCulture PMPDC
ON PPD.PRODUCTDESCRIPTIONID = PMPDC.PRODUCTDESCRIPTIONID
Inner Join
Production.ProductModel PM
ON
PMPDC.ProductModelID = PM.ProductModelID
INNER JOIN
PRODUCTION.Product PD
ON
PD.ProductModelID = PM.ProductModelID
INNER JOIN PRODUCTION.Culture CUL
ON
PMPDC.CultureID = CUL.CultureID
INNER JOIN
PRODUCTION.ProductSubcategory PSC
ON
PSC.ProductSubcategoryID = PD.ProductSubcategoryID
WHERE CUL.NAME = 'ENGLISH'
AND PSC.NAME = @sub_catgo_name
Bin
ary
Dat
a
352 1 4659 SALES_TERRIT
ORY_GB
create procedure DBA.SALES_TERRITORY_GB(
in @COUNTRYCODE char(8),
out SalesOrderNumber nvarchar(25),
out AccountNumber varchar(10),
out OrderDate date,
out DueDate date,
out ShipDate date,
out ProductID integer,
out CustomerID integer,
out SubTotal decimal(35,2),
out TaxAmt decimal(35,2),
out Freight decimal(35,2),
out TotalDue decimal(32,2),
out PRODUCTNAME NAME,
out Description nvarchar(400),
out COLOR nvarchar(15),
out Size nvarchar(5),
out SizeUnitMeasureCode nvarchar(3),
out SubCatgoName NAME,
out ProductSubcategoryID integer,
out CategoryName NAME,
out ProductCategoryID integer,
out TerritoryName NAME,
out CountryRegionCode nvarchar(3),
out TerritoryGroup nvarchar(50),
out TerritoryID integer )
--on exception resume--
begin
case when @COUNTRYCODE = 'GB' then
select SalesOrderNumber,AccountNumber,OrderDate,DueDate,ShipDate,ProductID,
CustomerID,SubTotal,TaxAmt,Freight,TotalDue,
PRODUCTNAME,Description,COLOR,Size,SizeUnitMeasureCode,SubCatgoName,ProductSubcategoryID,CategoryName,Product
CategoryID,
TerritoryName,CountryRegionCode,TerritoryGroup,TerritoryID
from SalesReports
where CountryRegionCode = @COUNTRYCODE
end case
end
N create PROCEDURE SALES_TERRITORY_GB(
in @COUNTRYCODE char(8),
OUT SalesOrderNumber NVARCHAR(25),
OUT AccountNumber VARCHAR (10),
OUT OrderDate DATE ,
OUT DueDate DATE,
OUT ShipDate DATE,
OUT ProductID INTEGER,
OUT CustomerID INTEGER,
OUT SubTotal DECIMAL(35,2),
OUT TaxAmt DECIMAL (35,2),
OUT Freight DECIMAL(35,2),
OUT TotalDue DECIMAL(32,2),
OUT PRODUCTNAME NAME,
OUT "Description" NVARCHAR(400),
OUT COLOR NVARCHAR (15),
OUT "Size" NVARCHAR(5),
OUT SizeUnitMeasureCode NVARCHAR (3),
OUT SubCatgoName NAME,
OUT ProductSubcategoryID INTEGER,
OUT CategoryName NAME,
OUT ProductCategoryID INTEGER ,
OUT TerritoryName NAME,
OUT CountryRegionCode NVARCHAR (3),
OUT TerritoryGroup NVARCHAR (50),
OUT TerritoryID INTEGER )
--on exception resume--
begin
CASE WHEN @COUNTRYCODE = 'GB'
THEN
select SalesOrderNumber,AccountNumber,OrderDate,DueDate,ShipDate,ProductID,
CustomerID,SubTotal,TaxAmt,Freight,TotalDue,
PRODUCTNAME,Description,COLOR,Size,SizeUnitMeasureCode,SubCatgoName,Product
SubcategoryID,CategoryName,ProductCategoryID,
TerritoryName,CountryRegionCode,TerritoryGroup,TerritoryID
from SalesReports
where CountryRegionCode = @COUNTRYCODE
END CASE;
end
Bin
ary
Dat
a
346 1 4383 SalesTerritory_R
eport
create procedure DBA.SalesTerritory_Report(
@Region_Code char(8) )
as
select
SOH.SalesOrderNumber,OrderDate=convert(date,SOH.OrderDate),DueDate=convert(date,SOH.DueDate),ShipDate=convert(date,SO
H.ShipDate),SOD.ProductID,
CustomerID=SOH.CUSTOMERID,SubTotal=convert(decimal(35,2),SOH.SUBTOTAL),
TaxAmt=convert(decimal(35,2),SOH.TAXAMT),Freight=convert(decimal(35,2),SOH.FREIGHT),TotalDue=convert(decimal(35,2),S
OH.TOTALDUE),ProductName=PD.Name,TerritoryName=ST.NAME,
ST.CountryRegionCode,
TerritoryGroup=ST."Group",ST.TerritoryID
from Sales.SalesOrderDetail as SOD
join Sales.SalesOrderHeader as SOH
on SOD.SalesOrderID = SOH.SalesOrderID
join Production.Product as PD
on SOD.PRODUCTID = PD.PRODUCTID
join SALES.SalesTerritory as ST
on SOH.TerritoryID = ST.TerritoryID
where CountryRegionCode = @Region_Code
order by PD.NAME asc
N create PROCEDURE SalesTerritory_Report
(@Region_Code Char(8))
AS
SELECT SOH.SalesOrderNumber, CAST (SOH.OrderDate AS DATE) AS OrderDate ,
CAST (SOH.DueDate AS DATE) AS DueDate, CAST (SOH.ShipDate AS DATE) AS
ShipDate, SOD.ProductID
,SOH.CUSTOMERID AS CustomerID, cast (SOH.SUBTOTAL as Decimal (35,2))AS
SubTotal,
cast (SOH.TAXAMT as Decimal (35,2))AS TaxAmt, cast (SOH.FREIGHT as Decimal
(35,2))AS Freight, cast (SOH.TOTALDUE as Decimal (35,2))AS TotalDue, PD.Name AS
ProductName, ST.NAME as TerritoryName,
ST.CountryRegionCode,
ST."Group" AS TerritoryGroup, ST.TerritoryID
FROM Sales.SalesOrderDetail SOD
INNER JOIN Sales.SalesOrderHeader SOH
ON
SOD.SalesOrderID = SOH.SalesOrderID
INNER JOIN Production.Product PD
ON SOD.PRODUCTID = PD.PRODUCTID
INNER JOIN
SALES.SalesTerritory ST
ON
SOH.TerritoryID = ST.TerritoryID
where CountryRegionCode = @Region_Code
ORDER BY PD.NAME
Bin
ary
Dat
a
29. 28
select * from sysTRIGGERS
WHERE
TRIGNAME = 'TERRITORYRPT_GB' OR TRIGNAME ='T_Reports'
owner trigname tname event trigtime trigdefn
DBA T_Reports Territory_ReportS INSERT BEFORE create trigger T_Reports before insert on
DBA.Territory_ReportS
referencing new as N
for each row
begin
set self_recursion off;
case when N.CountryRegionCode = 'GB' then
insert into Territory_ReportGB
( SalesOrderNumber,AccountNumber,ORDERDATE,DUEDATE,SHIPDATE,PRODUCTID,
CUSTOMERID,SUBTOTAL,TAXAMT,FREIGHT,TOTALDUE,
PRODUCTNAME,DESCRIPTION,COLOR,SIZE,SizeUnitMeasureCode,SubCatgoName,
ProductSubcategoryID,CategoryName,ProductCategoryID,TerritoryName,
CountryRegionCode,TerritoryGroup,TerritoryID,REPORT_ID,
REPORT_DATE ) values
( N.SalesOrderNumber,N.AccountNumber,N.ORDERDATE,N.DUEDATE,N.SHIPDATE,N.PRODUCTID,
N.CUSTOMERID,N.SUBTOTAL,N.TAXAMT,N.FREIGHT,N.TOTALDUE,
N.PRODUCTNAME,N.DESCRIPTION,N.COLOR,N.SIZE,N.SizeUnitMeasureCode,N.SubCatgoName,
N.ProductSubcategoryID,N.CategoryName,N.ProductCategoryID,N.TerritoryName,
N.CountryRegionCode,N.TerritoryGroup,N.TerritoryID,N.REPORT_ID,
N.REPORT_DATE )
end case
end
DBA TERRITORYRPT_GB Territory_ReportGB INSERT AFTER create trigger TERRITORYRPT_GB after insert on
DBA.Territory_ReportGB
referencing new as N
for each row
begin
set self_recursion off;
insert into REPORT_ADUIT
( SalesOrderNumber,AccountNumber,ORDERDATE,DUEDATE,SHIPDATE,PRODUCTID,
CUSTOMERID,SUBTOTAL,TAXAMT,FREIGHT,TOTALDUE,
PRODUCTNAME,DESCRIPTION,COLOR,SIZE,SizeUnitMeasureCode,SubCatgoName,
ProductSubcategoryID,CategoryName,ProductCategoryID,TerritoryName,
CountryRegionCode,TerritoryGroup,TerritoryID,REPORT_ID,
REPORT_DATE ) values
( N.SalesOrderNumber,N.AccountNumber,N.ORDERDATE,N.DUEDATE,N.SHIPDATE,N.PRODUCTID,
N.CUSTOMERID,N.SUBTOTAL,N.TAXAMT,N.FREIGHT,N.TOTALDUE,
N.PRODUCTNAME,N.DESCRIPTION,N.COLOR,N.SIZE,N.SizeUnitMeasureCode,N.SubCatgoName,
N.ProductSubcategoryID,N.CategoryName,N.ProductCategoryID,N.TerritoryName,
N.CountryRegionCode,N.TerritoryGroup,N.TerritoryID,N.REPORT_ID,
N.REPORT_DATE )
end
select * from sysprocedure where PROC_NAME ='ProdHistory' OR
PROC_NAME ='eMPaGE'
proc_id creator object_id proc_name proc_defn remarks replicate srvid source avg_num_rows avg_cost stats
351 1 4388 EMPAGE create function DBA.EMPAGE( in @SNN integer )
returns integer
begin
declare @EMPAGE1 integer;
select DATEDIFF(yy,HRE.BirthDate,current timestamp) as EAGE
into @EMPAGE1 from HumanResources.Employee as HRE
where HRE.NationalIDNumber = @SNN;
return(@EMPAGE1)
end
N create FUNCTION EMPAGE( in @SNN integer)
RETURNS INTEGER
BEGIN
DECLARE @EMPAGE1 integer;
SELECT DATEDIFF(YY, HRE.BirthDate , CURRENT_TIMESTAMP) AS EAGE
INTO
@EMPAGE1
FROM HumanResources.Employee HRE
WHERE HRE.NationalIDNumber = @SNN;
RETURN (@EMPAGE1);
END
Binary Data
Remote Server
30. 29
CREATE SERVER "MYSQL4" CLASS 'MYSQLODBC' USING 'Mysql-Sakila';
CREATE EXTERNLOGIN "DBA" TO "MYSQL4" REMOTE LOGIN 'root' IDENTIFIED BY '***';
CREATE SERVER "DB2" CLASS 'DB2ODBC' USING 'GSDB';
CREATE EXTERNLOGIN "DBA" TO "DB2" REMOTE LOGIN 'GOC-PC' IDENTIFIED BY '***';
A proxy table is needed to access the data in a remote server.
31. 30
SELECT * FROM "DBA"."customer"
customer_id store_id first_name last_name email address_id active create_date last_update
1 1 MARY SMITH MARY.SMITH@sakilacustomer.org 5 1 2006-02-14 22:04:36.000 2006-02-15 04:57:20.000
2 1 PATRICIA JOHNSON PATRICIA.JOHNSON@sakilacustomer.org 6 1 2006-02-14 22:04:36.000 2006-02-15 04:57:20.000
3 1 LINDA WILLIAMS LINDA.WILLIAMS@sakilacustomer.org 7 1 2006-02-14 22:04:36.000 2006-02-15 04:57:20.000
4 2 BARBARA JONES BARBARA.JONES@sakilacustomer.org 8 1 2006-02-14 22:04:36.000 2006-02-15 04:57:20.000
5 1 ELIZABETH BROWN ELIZABETH.BROWN@sakilacustomer.org 9 1 2006-02-14 22:04:36.000 2006-02-15 04:57:20.000
6 2 JENNIFER DAVIS JENNIFER.DAVIS@sakilacustomer.org 10 1 2006-02-14 22:04:36.000 2006-02-15 04:57:20.000
7 1 MARIA MILLER MARIA.MILLER@sakilacustomer.org 11 1 2006-02-14 22:04:36.000 2006-02-15 04:57:20.000
8 2 SUSAN WILSON SUSAN.WILSON@sakilacustomer.org 12 1 2006-02-14 22:04:36.000 2006-02-15 04:57:20.000