Part 5: Stored Procedures, Date Functions & Real-World Reports ππ ️
After creating his first VIEW, Kai was buzzing with confidence. He said, “It’s like building a custom window into the data.” Exactly right. And that window is just the beginning — now, we’re ready to give him **buttons** to push. Enter: stored procedures.
What’s a Stored Procedure?
I explained it like this: “Imagine writing a script that does something useful — and then saving it with a name. Anytime you want to run it, you just call the name.”
Kai’s eyes lit up. “So… like a shortcut?” Yep. And shortcuts matter when you’re dealing with real-world tasks, like enrolling students or checking attendance.
CREATE PROCEDURE EnrollStudent @StudentID INT, @CourseName NVARCHAR(100) AS BEGIN INSERT INTO Enrollments (StudentID, CourseName, EnrollmentDate) VALUES (@StudentID, @CourseName, GETDATE()); END We ran it like this:
EXEC EnrollStudent @StudentID = 3, @CourseName = 'Math Fundamentals'; Kai smiled: “So I just tell it what I want, and it figures out the rest?” That’s the magic of reusable logic. π―
Using Date Functions to Track Real Things
With enrollment working, we looked at **dates**. Real-life databases live and breathe time — when someone signs up, when they attended, when they last logged in. So we explored date functions:
-- Students enrolled this week SELECT * FROM Enrollments WHERE EnrollmentDate >= DATEADD(DAY, -7, GETDATE()); He was curious about birthdays and anniversaries, so we tried:
-- Students with birthdays this month SELECT * FROM Students WHERE MONTH(BirthDate) = MONTH(GETDATE()); “So you can find people with the same birthday as me?” he asked. Yes — and that led to a *whole* side conversation about privacy, data ethics, and birthdays in the database. π
Real-World Reporting: Mini Attendance Tracker
Then came the real-world challenge. We designed a **mini attendance system** together — a table to track attendance, a stored procedure to record it, and a query to report on it.
CREATE TABLE Attendance ( AttendanceID INT IDENTITY(1,1), StudentID INT, CourseName NVARCHAR(100), DateAttended DATE DEFAULT GETDATE() ); CREATE PROCEDURE MarkAttendance @StudentID INT, @CourseName NVARCHAR(100) AS BEGIN INSERT INTO Attendance (StudentID, CourseName) VALUES (@StudentID, @CourseName); END Then we queried weekly attendance:
SELECT StudentID, CourseName, COUNT(*) AS DaysPresent FROM Attendance WHERE DateAttended >= DATEADD(DAY, -7, GETDATE()) GROUP BY StudentID, CourseName; “So this is like a report,” Kai said. “Like something you’d show a teacher or boss.” Exactly. We even mocked up a layout for using SQL Server Reporting Services (SSRS) to visualize it.
Beyond Code: Ownership and Identity ✨
At this point, it’s not just about SQL anymore. Kai isn’t just *learning* — he’s *building*. He’s starting to ask questions like, “Can we automate this?” or “What if we log absences too?” These are the questions of a **developer**.
And as a neurodivergent parent myself, these moments are beyond technical. They’re transformational. Watching Kai create tools with real-world applications — and take pride in his work — reminds me how capable and brilliant he is, in his own rhythm, in his own way.
What’s Next: Permissions, Security & Building a Full App π
Next time, we’re going to shift gears and look at **user permissions** — who gets access to what — and build in **basic security**. We’ll also start planning our first full **web-based student dashboard**, showing live enrollments and attendance, powered by Kai’s stored procedures and views.
He’s already designing the UI on paper. And I can’t wait to bring it to life with him.
Because this isn’t just a SQL project. This is Kai’s story of growth, resilience, and potential.
Keep Going, Keep Believing π
If you’re a parent, a carer, or someone who learns differently — know this: the path may look different, but the destination is just as rich. Give it time. Give it love. Give it curiosity. And most of all — give it belief.
We’ll see you in Part 6 — where we build something you can click. ππ»
Until then, keep writing code, keep asking “what if?”, and keep showing up. Because there’s no such thing as “too different” to succeed. π
Comments
Post a Comment