Upload
dsunte-wilson
View
15
Download
7
Embed Size (px)
DESCRIPTION
SQL Adv Lab
Citation preview
1
Unit 3
1. Using the authors table, create a single column label output as follows making sure there is no possibility of a data modification taking place:
Johnson White
10932 Bigge Rd.
Menlo Park CA 94025
Marjorie Green
309 63rd St. #411
Oakland CA 94618
Cheryl Carson
589 Darwin Ln.
Berkeley CA 94705
Michael O'Leary
22 Cleveland Av. #14
San Jose CA 95128
Dean Straight
5420 College Av.
Oakland CA 94609
Meander Smith
10 Mississippi Dr.
Lawrence KS 66044
Abraham Bennet
6223 Bateman St.
Berkeley CA 94705
Ann Dull
3410 Blonde St.
Palo Alto CA 94301
Burt Gringlesby
PO Box 792
Covelo CA 95428
Charlene Locksley
18 Broadway Av.
San Francisco CA 94130
Morningstar Greene
22 Graybar House Rd.
Nashville TN 37215
Reginald Blotchet-Halls
55 Hillsdale Bl.
Corvallis OR 97330
Akiko Yokomoto
3 Silver Ct.
Walnut Creek CA 94595
2
Unit 4
1. Create a stored procedure that returns all of the title IDs, title, and type from the titles table. 2. Enhance this stored procedure to only return the titles for the type you specify. Make sure to return a usage
statement to the user if a type is not specified.
3. Enhance this procedure to provide a default value if no value is given. Remove the usage statement. 4. Enhance this procedure to return the total sales for a given type of book directly into a local variable and create
a batch to test this.
5. Modify this procedure to only return the total sales if a type of business is passed into the proc. 6. Enhance this procedure to return the total sales if the type passed in was business or return a message to the user
that says Not a type of business if the type passed was not business
7. Enhance the procedure by adding a documentation header 8. Enhance the procedure by adding appropriate error handling 9. Create a second procedure to run the labels created in exercise 1 of Unit 9. Include the ability to pass in a state
to get just those authors that live in that state. If no state is passed, return all authors.
10. Using a select into, return only those title types that have an average price > $15
3
Unit 5
1. Create a trigger that maintains referential integrity for the sales table a. If a stor_id is inserted, validate it against the sales table b. If its invalid send an appropriate message and rollback the transaction
2. Create an insert, delete, update trigger to derive duplicate data in the salesdetail table a. Maintain the data in the titles.total_sales whenever data in the salesdetail.qty column changes
3. Create a trigger for the publishers table that displays the contents of the inserted and deleted tables a. Show the contents of the inserted/deleted tables for each of the three different DML statements and rollback
the transaction
4
Unit 3 Solutions
1. set nocount on
declare @name char(80),
@address char(50),
@city char(60)
declare cur_mycursor cursor for
select rtrim(ltrim(au_fname)) + ' ' + rtrim(ltrim(au_lname)), address,
rtrim(ltrim(city)) + ' ' + rtrim(ltrim(state)) + ' ' + rtrim(ltrim(zip))
from authors
for read only
open cur_mycursor
fetch cur_mycursor into @name, @address, @city
while @@fetch_status = 0
begin
print @name
print @address
print @city
print ' '
fetch cur_mycursor into @name, @address, @city
end
close cur_mycursor
deallocate cur_mycursor
set nocount off
Unit 4 Solutions
1. create procedure myproc as
select title_id, title, type from titles
go
2. drop procedure myproc
go
create procedure myproc (@type char(10) = null) as
if @type is null
begin
print 'Usage: exec myproc type'
return
end
select title_id, title, type from titles
where type = @type
go
3. drop procedure myproc
go
create procedure myproc (@type char(10) = 'business') as
select title_id, title, type from titles
where type = @type
go
5
4. drop procedure myproc
go
create procedure myproc (@type char(10) = 'business', @total_sales money output) as
select @total_sales = (select sum(ytd_sales) from titles where type = @type)
go
declare @total_sales money
exec myproc 'business',@total_sales output
select @total_sales
go
5. drop procedure myproc
go
create procedure myproc (@type char(10) = 'business', @total_sales money output) as
if @type = 'business'
begin
select @total_sales = (select sum(ytd_sales) from titles where type = @type)
end
return
go
declare @total_sales money
exec myproc 'popular_comp',@total_sales output
select @total_sales
6. drop procedure myproc
go
create procedure myproc (@type char(10) = 'business', @total_sales money output) as
if @type = 'business'
begin
select @total_sales = (select sum(ytd_sales) from titles where type = @type)
end
else
print 'Not a type of business'
return
go
declare @total_sales money
exec myproc 'popular_comp',@total_sales output
select @total_sales
7. drop prcedure myproc
go
create procedure myproc (@type char(10) = 'business', @total_sales money output) as
/********************************************************************************/
/* Stored procedure myproc */
/* Creation Date: */
/* Author: */
/* */
/* Purpose: */
/* Receives an input of type of book and outputs the total year to date */
/* sales for that type */
/* */
/* Input Parameters: */
/* @type The type of book */
6
/* */
/* Output Parameters: */
/* @total_sales The total sales for that type */
/* */
/* Return Status */
/* */
/* Usage: */
/* declare @total money */
/* exec myproc type, @total output */
/* */
/* Local Variables: */
/* */
/* Called by: */
/* */
/* Calls: */
/* */
/* Data Modifications: */
/* */
/* Updates */
/* Date Author Changes */
/********************************************************************************/
if @type = 'business'
begin
select @total_sales = (select sum(ytd_sales) from titles where type = @type)
end
else
print 'Not a type of business'
return
go
8. declare @error int,
@rows int
if @type = 'business'
begin
select @total_sales = (select sum(ytd_sales) from titles where type = @type)
select @error = @@error, @rows = @@rowcount
if @error 0
begin
raiserror('An error has occured',16,-1)
return
end
if @rows = 0
begin
raiserror('No rows matched criteria',16,-1)
return
end
end
else
print 'Not a type of business'
return
go
9. create procedure myotherproc (@state char(2)=null)
as
7
set nocount on
declare @name char(80),
@address char(50),
@city char(60)
if @state is null
declare cur_mycursor cursor for
select rtrim(ltrim(au_fname)) + ' ' + rtrim(ltrim(au_lname)), address,
rtrim(ltrim(city)) + ' ' + rtrim(ltrim(state)) + ' ' + rtrim(ltrim(zip))
from authors
for read only
else
declare cur_mycursor cursor for
select rtrim(ltrim(au_fname)) + ' ' + rtrim(ltrim(au_lname)), address,
rtrim(ltrim(city)) + ' ' + rtrim(ltrim(state)) + ' ' + rtrim(ltrim(zip))
from authors where state = @state
for read only
open cur_mycursor
fetch cur_mycursor into @name, @address, @city
while @@fetch_status = 0
begin
print @name
print @address
print @city
print ' '
fetch cur_mycursor into @name, @address, @city
end
close cur_mycursor
deallocate cur_mycursor
set nocount off
return
go
10. create procedure myproc
as
select type, avg(price) as AvgPrice into #mytemptable from titles group by type
select * from #mytemptable where AvgPrice > 15
go
8
Unit 5 Solutions
1. create trigger ti_sales on sales
for insert
as
declare @num_rows int
select @num_rows = @@rowcount
if @num_rows = 0
return
if (select count(*) from stores s, inserted i where i.stor_id = s.stor_id) != @num_rows
begin
raiserror(30001,"Attempt to insert invalid stor_id")
rollback transaction
return
end
return
go
2. create trigger tiud_salesdetail on salesdetail
for insert, update, delete
as
declare @irows int,
@drows int
/* Return if no rows affected */
if @@rowcount = 0
return
select @irows = count(*) from inserted
select @drows = count(*) from deleted
begin transaction
/* Are there rows in inserted? */
if @irows != 0
begin
update titles set total_sales = isnull(total_sales,0) + (select sum(qty) from inserted where titles.title_id =
inserted.title_id)
if @@transtate = 2
begin
rollback transaction
return
end
if @@transtate = 3
return
end
/* Are there rows in deleted? */
if @drows != 0
begin
update titles set total_sales = isnull(total_sales,0) - (select sum(qty) from deleted where titles.title_id =
deleted.title_id)
if @@transtate = 2
9
begin
rollback transaction
return
end
if @@transtate = 3
return
end
commit transaction
return
go
3. create trigger tiud_publishers on publishers
for insert, update, delete
as
declare @ins_rows int,
@del_rows int
if @@rowscount = 0
return
select @ins_rows = count(*) from inserted
select @del_rows = count(*) from deleted
if @del_rows = @ins_rows
/* Trigger fired by update */
begin
print 'Trigger fired by update. Contents of inserted are:'
select * from inserted
print 'Contents of deleted are:'
select * from deleted
rollback transaction
return
end
if @del_rows = 0 /*Trigger fired by insert */
begin
print 'Trigger fired by insert. Contents of inserted are:'
select * from inserted
print 'Contents of deleted are:'
select * from deleted
rollback transaction
return
end
if @ins_rows = 0 /*Trigger fired by delete */
begin
print 'Trigger fired by delete. Contents of inserted are:'
select * from inserted
print 'Contents of deleted are:'
select * from deleted
rollback transaction
return
end
return
10
go