Final Project
Be sure to follow the instructions for each step as you will be graded accordingly. Each report should have a title and appropriate field headings for each column of data. If you list a team or player always list both the number and name. Be sure that your results are sorted as requested, and that your numbers make sense. Make all of your reports stored procedures, I will want a copy of each stored procedure, and your results which will be a query that runs all of them and the output they produce.
1. Produce a list of Teams including how many points they earned (determine this in a function that takes each team as its being read in and brings back the number of points) sort the output in highest to lowest order according to the points they received and create an additional column that designates who came in 1st place, 2nd place, and 3rd place. Include the name of the manager in your list.
2. Produce a list of Players, by Team, with a blank line in between each teams players. Include their starting average, and the average of what they bowled during the league (do this by building a function that you can call from your stored procedure that will take the current player being read by your procedure and determine his average for the league play) the results should be sorted numerically in team and player order. At the bottom of the report list : Which player bowled the highest average during the league, and which was the most improved from his starting average, be sure to label this output appropriately.
3. Produce a list that shows week by week which teams played which, and the points they earned that particular week. Sort the output in week order and then by team number 1 order (you are using the schedule table as your driver for this) make sure to skip a line in between each weeks output.
Make sure all stored procedures and functions use your Team Number as part of their name
Advanced SQL – Week 13
Triggers are SQL statements that are automatically executed when certain events occur. They can be tied to the following statements: DELETE, INSERT, and UPDATE.
For example you might save a copy of a row in an archive table before issuing a DELETE statement. Check that certain fields are formatted correctly before doing an INSERT, or perform calculations such as subtracting the order quantity from the amount on hand when doing an UPDATE
Each of the above examples have in common that something needs to be done automatically whenever a change occurs to a table.
Triggers can be single statements, or groups of statements that are enclosed within a BEGIN and END statement.
You create a trigger by supplying 3 pieces of information: a unique trigger name, the table it is to be associated with, and the event that should trigger it.
CREATE TRIGGER process_scores ON scores AFTER INSERT AS
SELECT ‘Another score has been added’;
Triggers are defined per event per table, and only one trigger per event per table is a ...
Final ProjectBe sure to follow the instructions for each step as.docx
1. Final Project
Be sure to follow the instructions for each step as you will be
graded accordingly. Each report should have a title and
appropriate field headings for each column of data. If you list a
team or player always list both the number and name. Be sure
that your results are sorted as requested, and that your numbers
make sense. Make all of your reports stored procedures, I will
want a copy of each stored procedure, and your results which
will be a query that runs all of them and the output they
produce.
1. Produce a list of Teams including how many points they
earned (determine this in a function that takes each team as its
being read in and brings back the number of points) sort the
output in highest to lowest order according to the points they
received and create an additional column that designates who
came in 1st place, 2nd place, and 3rd place. Include the name
of the manager in your list.
2. Produce a list of Players, by Team, with a blank line in
between each teams players. Include their starting average, and
the average of what they bowled during the league (do this by
building a function that you can call from your stored procedure
that will take the current player being read by your procedure
and determine his average for the league play) the results
should be sorted numerically in team and player order. At the
bottom of the report list : Which player bowled the highest
average during the league, and which was the most improved
from his starting average, be sure to label this output
appropriately.
3. Produce a list that shows week by week which teams played
which, and the points they earned that particular week. Sort the
2. output in week order and then by team number 1 order (you are
using the schedule table as your driver for this) make sure to
skip a line in between each weeks output.
Make sure all stored procedures and functions use your Team
Number as part of their name
Advanced SQL – Week 13
Triggers are SQL statements that are automatically executed
when certain events occur. They can be tied to the following
statements: DELETE, INSERT, and UPDATE.
For example you might save a copy of a row in an archive table
before issuing a DELETE statement. Check that certain fields
are formatted correctly before doing an INSERT, or perform
calculations such as subtracting the order quantity from the
amount on hand when doing an UPDATE
Each of the above examples have in common that something
needs to be done automatically whenever a change occurs to a
table.
Triggers can be single statements, or groups of statements that
are enclosed within a BEGIN and END statement.
You create a trigger by supplying 3 pieces of information: a
unique trigger name, the table it is to be associated with, and
3. the event that should trigger it.
CREATE TRIGGER process_scores ON scores AFTER INSERT
AS
SELECT ‘Another score has been added’;
Triggers are defined per event per table, and only one trigger
per event per table is allowed. So you cannot have more than 3
triggers per table (one for INSERT, UPDATE, and DELETE)
You can also define a trigger as AFTER INSERT, UPDATE so
that the same trigger will happen with two different events
You can DROP TRIGGER process_scores and you can ALTER
TRIGGER process_scores
There is also a INSTEAD OF trigger that replaces a given
command with another action. So instead of deleting records it
might mark them as inactive.
You can DISABLE TRIGGER process_scores ON scores and
you can ENABLE TRIGGER process_scores ON scores
Triggers are often used to create audit logs and can call Stored
Procedures.
Within INSERT trigger code, you can refer to a virtual table
named INSERTED to access the rows being inserted.
Within DELETE trigger code, you can refer to a virtual table
named DELETED to access the rows being deleted.
Within UPDATE trigger code, you can refer to a virtual table
named DELETED to access the previous (pre-UPDATE
statement) values and INSERTED to access the new updated
values.
4. The fact that triggers alter the apparent functioning of SQL as
they operate invisibly becomes an issue that must be identified
and dealt with.
ALWAYS check the tables that you are planning to access to
determine what is hiding such as keys and triggers. Knowing
what functions and stored procedures have been written can
assist you in your programming, as can being aware of views
and indexes.
Advanced SQL – Week 12
DECLARE Defines a new cursor.
OPEN Opens and populates the cursor by executing the
SELECT statement defined by the cursor.
FETCH Retrieves a row from the cursor.
CLOSE Closes the cursor.
DEALLOCATE Deletes the cursor definition and releases all
system resources associated with the cursor.
-- Step 1 Define any variable you intend to use
DECLARE @AccountID INT
DECLARE @getAccountID CURSOR
-- Step 2 Write your query and stick the results in the cursor
SET @getAccountID = CURSOR FOR
SELECT EMPNO
FROM EMP
-- Step 3 Open the cursor and read the first record
5. OPEN @getAccountID
FETCH NEXT
FROM @getAccountID INTO @AccountID
-- Step 4 Loop through the cursor reading each line until they
are no more
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @AccountID
FETCH NEXT
FROM @getAccountID INTO @AccountID
END
-- Step 5 Close the cursor and clear the memory it used
CLOSE @getAccountID
DEALLOCATE @getAccountID
http://www.kodyaz.com/articles/cursor.aspx
http://msdn.microsoft.com/en-us/library/ms180169.aspx
http://www.kodyaz.com/articles/t-sql-cursor-example-code.aspx
http://msdn.microsoft.com/en-
us/library/aa172595(SQL.80).aspx
These are important for the homework
http://www.eggheadcafe.com/software/aspnet/31208449/multipl
e-rows-into-multiple-columns.aspx
6. http://www.sqlteam.com/article/cursors-an-overview
HOMEWORK
1) Modify todays example to print the employee number and
name. I want you to print the employee number such that odd
numbers are to the left and even numbers are to the right.
12311 John Doe
12312 Jane Smith
2) Using the Bowling League Database. Produce a list of the
players on each team in team and player number order such that
you skip a blank line in between the list for each team. Show
the Team number and name and the player number and name.
Hints for Homework Week 11
--Step 1 Create a table of Agregated Scores by Date_Bowled,
Team_ID, Team_Name in a temporary file
--Step 2 Use the schedule and the table you created in Step 1 (2
times) to align the team information onto one row for both
teams in a temporary file (Hint: you should not be calculating
anything here just joining two vertical records from step 1 into
1 horizontal record)
--Step 3 Use the table you created in Step 2 to assign the points,
remember to code for ties and put the results into a temporary
file (Hint: your records are still horizontal here)
--Step 4 Build points table for all weeks and teams into a
temporary table that you will use for futher assignments (Hint:
7. you want your records to be vertical here now that the points
are calculated)
Drop
Table Schedule
create
table Schedule
(
Date_Bowled Date
,Team1 VARCHAR(2)
,Team2 VARCHAR(2)
);
GO
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/10/15','1','2');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/10/15','3','4');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/10/15','5','6');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/10/15','7','8');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/10/15','9','10');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/10/15','11','12');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/10/15','13','14');
8. INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/10/15','15','17');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/17/15','1','3');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/17/15','2','4');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/17/15','5','7');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/17/15','6','8');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/17/15','9','12');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/17/15','10','11');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/17/15','13','17');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/17/15','14','15');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/24/15','1','4');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/24/15','2','3');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/24/15','5','8');
9. INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/24/15','6','7');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/24/15','9','11');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/24/15','10','12');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/24/15','13','15');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/24/15','14','17');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/31/15','1','5');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/31/15','2','6');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/31/15','3','7');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/31/15','4','8');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/31/15','9','13');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/31/15','10','14');
INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/31/15','11','15');
10. INSERT
INTO Schedule (Date_Bowled, Team1, Team2)
VALUES('01/31/15','12','17');
INSERT
INTO
...
Sheet1Date_BowledTeam1Team201/10/151201/10/153401/10/1
55601/10/157801/10/1591001/10/15111201/10/15131401/10/151
51701/17/151301/17/152401/17/155701/17/156801/17/1591201/
17/15101101/17/15131701/17/15141501/24/151401/24/152301/2
4/155801/24/156701/24/1591101/24/15101201/24/15131501/24/
15141701/31/151501/31/152601/31/153701/31/154801/31/15913
01/31/15101401/31/15111501/31/15121702/07/151602/07/15270
2/07/153802/07/154502/07/1591402/07/15101502/07/15111702/
07/15121302/14/151702/14/152802/14/153502/14/154602/14/15
91502/14/15101702/14/15111302/14/15121402/21/151802/21/15
2502/21/153602/21/154702/21/1591702/21/15101302/21/151114
02/21/15121502/28/151902/28/1521002/28/1531102/28/1541202
/28/1551302/28/1561402/28/1571502/28/1581703/07/1511003/0
7/1521103/07/1531203/07/1541303/07/1551403/07/1561503/07/
1571703/07/158903/14/1511103/14/1521203/14/1531303/14/154
1403/14/1551503/14/1561703/14/157903/14/1581003/21/151120
3/21/1521303/21/1531403/21/1541503/21/1551703/21/156903/2
1/1571003/21/1581103/28/1511303/28/1521403/28/1531503/28/
1541703/28/155903/28/1561003/28/1571103/28/1581204/04/151
1404/04/1521504/04/1531704/04/154904/04/1551004/04/156110
4/04/1571204/04/1581304/11/1511504/11/1521704/11/153904/1
1/1541004/11/1551104/11/1561204/11/1571304/11/1581404/18/
1511704/18/152904/18/1531004/18/1541104/18/1551204/18/156
1304/18/1571404/18/15815
CASE WHEN TEAM1_GAME1>TEAM2_GAME1 THEN 1
ELSE 0 END GAME1,
11. if 12 % 2 =0
PRINT cast( 12 as char(4)) + ' '+'right'
if 13 % 2 <>0
PRINT cast( 13 as char(4))+ ' '+'left'
WHILE @@FETCH_STATUS=0
BEGIN
IF @STUDENT<>@TEMP_STUDENT
PRINT ''
PRINT @STUDENT
SELECT @[email protected]
FETCH NEXT
New Homework
Delete scores for Week16 and Week17
Create a schedule table in your bowling database to store the
attached list. Be careful that the fields you create have the
same datatype and length and the equivalent fields in the scores
table.
Now the fun begins. Create a script that uses the schedule to
12. read the scores table and produce a list that shows: HINT - You
have to use the scores table twice in your query, once to link
Team 1 for any given week and once to link Team 2 for any
given week.
Date, Team1 Number and Name, Total score for Game1, Game
2, Game 3 and Series and Team2 Number and Name, Total score
for Game1, Game 2, Game 3 and Series .
You also need to determine how many points each of the teams
earned, they get 1 point for having the highest team score for
Game 1, Game 2, Game 3, and Series. (So each match up
should produce 4 points divided among the two teams.)
Make the list in date and Team1 Number order.
At the end list a summary of each team and how many points
they earned using variables. There will be 480 points in total
for the 15 weeks we bowled.
Advanced SQL – Week 11
· The USE statement sets the current database
· You must DECLARE variables before you use them, you can
define one or more at the same time.
DECLARE @variablename variabletype = value
SET @TotalCost = 10
SET @TotalCost = @UnitCost * 1.1
DECLARE @Test money
SET @Test = (SELECT MAX(Sal) FROM Emp)
SELECT @Test
13. PRINT 'The maximum salary is ' +CONVERT(varchar,@Test,1)
go
· There are a number of System Variables such as: @@ERROR,
@@IDENTITY, @@ROWCOUNT
· To separate a script into multiple batches, we use the GO
statement. Using it causes all statements ince the beginning of
the script or the last GO statement (whichever is closer) to be
compiled into one execution plan and sent to the server
independently of any other batches.
· Variables declared in one batch are not available to other
batches
EXAMPLES
SELECT convert(varchar,table_catalog,20) as "Database
Name", table_name
FROM information_schema.tables
create table Billings (
BankerID INTEGER,
BillingNumber INTEGER,
BillingDate datetime,
BillingTotal INTEGER,
TermsID INTEGER,
BillingDueDate datetime ,
PaymentTotal INTEGER,
CreditTotal INTEGER);
GO
INSERT INTO Billings VALUES (5, 1, '2005-01-22', 110,
14. 1,'2005-04-22',123,321);
GO
INSERT INTO Billings VALUES (2, 2, '2001-02-21', 165,
1,'2002-02-22',123,321.);
GO
INSERT INTO Billings VALUES (5, 3, '2003-05-02', 190,
1,'2005-04-12',123,321);
GO
INSERT INTO Billings VALUES (4, 4, '1999-03-12', 165,
1,'2005-04-18',123,321);
GO
INSERT INTO Billings VALUES (5, 5, '2000-04-23', 165,
1,'2005-04-17',123,321);
GO
INSERT INTO Billings VALUES (6, 6, '2001-06-14', 165,
1,'2005-04-18',123,321);
GO
INSERT INTO Billings VALUES (5, 7, '2002-07-15', 120,
1,'2005-04-19',123,321);
GO
INSERT INTO Billings VALUES (8, 8, '2003-08-16', 165,
1,'2005-04-20',123,321);
GO
INSERT INTO Billings VALUES (9, 9, '2004-09-17', 165,
1,'2005-04-21',123,321);
GO
INSERT INTO Billings VALUES (5, 0, '2005-10-18', 145,
15. 1,'2005-04-22',123,321);
GO
DECLARE @MaxBilling money, @MinBilling money
DECLARE @PercentDifference decimal(8,2)
DECLARE @BillingCount int, @BankerIDVar int
SET @BankerIDVar = 5
SET @MaxBilling = (SELECT MAX(BillingTotal) FROM
Billings
WHERE BankerID = @BankerIDVar)
SELECT @MinBilling = MIN(BillingTotal), @BillingCount =
COUNT(*)
FROM Billings
WHERE BankerID = @BankerIDVar
SET @PercentDifference = (@MaxBilling - @MinBilling) /
@MinBilling * 100
PRINT 'Maximum Billing is $' +
CONVERT(varchar,@MaxBilling,1) + '.'
PRINT 'Minimum Billing is $' +
CONVERT(varchar,@MinBilling,1) + '.'
PRINT 'Maximum is ' +
CONVERT(varchar,@PercentDifference) +
'% more than minimum.'
PRINT 'Number of Billings: ' +
CONVERT(varchar,@BillingCount) + '.'
· Control of Flow Statements: IF … ELSE, CASE, WHILE
16. IF <Boolean Expression>
<SQL statement> | BEGIN <code series> END
[ELSE
<SQL statement> | BEGIN <code series> END]
Variable = CASE <input expression>
WHEN <when expression> THEN <result expression>
[…n]
[ELSE <result expression>]
END
WHILE <Boolean expression>
<sql statement> |
[BEGIN
<statement block>
[BREAK]
<sql statement> | <statement block>
[CONTINUE]
Transact-SQL Variableshttp://msdn.microsoft.com/en-
us/library/ms187953.aspx
T-SQL Loop Statement
http://www.databasejournal.com/features/mssql/article.php/3100
621/T-SQL-Programming-Part-2---Building-a-T-SQL-Loop.htm
Looping Statements
http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?t
opic=/com.ibm.db2.udb.apdv.sql.doc/doc/c0024350.htm