Skip to content

Commit 8dc6921

Browse files
John McGaugheyJohn McGaughey
John McGaughey
authored and
John McGaughey
committed
add 3 files showing procedures
1 parent f797afc commit 8dc6921

4 files changed

+111
-0
lines changed

maintaining-sql-eddb.sql

+23
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,26 @@ delete major
6262
where ID = (select id from Major where Description = 'General studies')
6363

6464
select * from major;
65+
66+
-- adding a student to a new class
67+
insert class (code, subject, section)
68+
values
69+
('ENG401', 'English', 401);
70+
71+
select * from Class;
72+
select * from Student;
73+
74+
select sc.StudentId, CONCAT(s.firstname, ' ', s.Lastname) as Name, c.code, c.subject, sc.ClassGradeValue as 'Grade', c.InstructorId as 'Instructor'
75+
from StudentClass SC
76+
JOIN Student S
77+
ON S.Id = sc.StudentId
78+
join Class c
79+
on c.Id = sc.ClassId;
80+
81+
insert StudentClass (StudentId, ClassId)
82+
values
83+
(
84+
(select id from Student where Lastname = 'McGaughey' and Firstname = 'John'),
85+
(SELECT id from class where code = 'ENG401')
86+
);
87+

procedures1-whileLoop-eddb.sql

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- procedures
2+
-- this creates the procedure counter
3+
create or alter procedure Counter
4+
@max int = 5 -- do not declare variable in procedure BEFORE as
5+
as
6+
-- BEGIN/END are statement blocks, code is run between statement blocks
7+
begin
8+
declare @nbr int = 0;
9+
declare @sum int = 0
10+
while @nbr <= @max
11+
begin
12+
set @sum = @sum + square(@nbr);
13+
print str(@nbr) + ' sum of the squares ' + trim(str(@sum));
14+
set @nbr += + 1;
15+
end
16+
print 'Done...'
17+
end
18+
19+
-- go will 'lock in the db', then can execute
20+
go
21+
exec Counter @max = 10; -- can pass values of variables during exec
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- procedures for
2+
create or alter procedure ShowCustomersBySalesRange
3+
@lowsales decimal(10,2) = 0.0,
4+
@highsales decimal(10,2) = 99999999.99
5+
as
6+
begin
7+
select *
8+
from Customers
9+
where Sales between @lowsales and @highsales
10+
order by sales desc;
11+
end
12+
go
13+
exec ShowCustomersBySalesRange @lowsales = 50000;

procedures3-AddStudent-eddb.sql

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
-- Procedure to add student to EdDb
2+
CREATE or ALTER procedure AddStudent
3+
@firstname varchar(30) = null,
4+
@lastname varchar(30) = null,
5+
@statecode char(2) = 'OH',
6+
@sat int = null,
7+
@gpa decimal(4,2) = 0,
8+
@majorcode varchar(4) = null
9+
as
10+
begin
11+
-- make sure fields aren't null
12+
if @firstname is null
13+
begin
14+
print 'First name is required';
15+
return;
16+
end
17+
if @lastname is null
18+
begin
19+
print 'Last name is required';
20+
return;
21+
end
22+
23+
-- find majorid using users code
24+
declare @majorID int = 0;
25+
select @majorID = id from Major where code = @majorcode;
26+
27+
-- check to see if major was found
28+
if @majorID = 0
29+
begin
30+
print 'Major not found';
31+
return;
32+
end
33+
34+
insert Student (Firstname, Lastname, StateCode, SAT, gpa, MajorId)
35+
values (@firstname, @lastname, @statecode, @sat, @gpa, @majorid);
36+
37+
-- check to make sure 1 row was added
38+
if @@ROWCOUNT != 1
39+
begin
40+
print 'Insert failed';
41+
return;
42+
end
43+
44+
print 'Insert successful!';
45+
end
46+
go
47+
exec AddStudent @firstname = 'Jacob', @lastname = 'McGaughey', @statecode = 'KY',
48+
@sat = 1200, @gpa = 3.7, @majorcode = 'FIN';
49+
go
50+
select *
51+
from Student s
52+
join major m
53+
on s.MajorId = m.Id
54+
order by s.id desc;

0 commit comments

Comments
 (0)