Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 2700 – Data Structures (SQL)

Similar presentations


Presentation on theme: "COP 2700 – Data Structures (SQL)"— Presentation transcript:

1 COP 2700 – Data Structures (SQL)
Lecture 8 – June 15

2 Some Comments on Exam and Assignment3
Exists returns only True and False. If the select in the Exists returns ANY rows, then it is true. The variable list on set operations must match. You can join a table with itself AND is not the same as OR A select of one variable can then be used in an IN

3 Announcements Assignment 4 is due Thursday Night, June 17 by 11:59 PM.
Last Day to Drop is today

4 What is missing from SQL to make it a true Programming Language?
Variables @Variable Names, SET Conditional Statements If, Else, Switch/Case Iterative Statements While Traverse a “file” (which in this case is a Query Result Set). Cursor, Open, Fetch, Close Communicate with Outside World Print

5 These items were added to a program language named Transact/SQL
SQL Server specific, although Oracle and MySQL have a similar set of programming commands. These commands are then used to code: Scripts – Programs stored as text files and then processed through either the GUI, or oSQL (DOS Shell Script Processor) Functions – Small programs that accept parameter input and output a result. Stored Procedures – Programs that can be called from other programs, Management Student or oSQL Triggers – Small programs “fired” when certain actions are taken. Usually involved in ensuring data integrity.

6 Quick Look at oSQL Go to a DOS prompt Enter oSQL -? then
oSQL –S localhost\PBSC U=sa

7 Transact SQL Basics BEGIN DECLARE @Variable1 Type2;
Do Some Work SET IF/ELSE While Loop SELECT PRINT Exception Handling END; GO

8 BEGIN…END Defines Blocks of code
Can be stand alone or can distinguish code to be executed for IF/ELSE or While Loops

9 Some Simple Examples Declare @sName Varchar(60); BEGIN
Print the First_Name and Last_Name of Student Z123: Declare @sName Varchar(60); BEGIN = (Last_Name + ', ' + First_name) FROM Student WHERE Student_ID = 'Z123'; END; GO Note: Each command or declare ends with a semi-colon. The block of commands ends with a GO Print is an internal function that writes stuff to an output area.

10 Another Example Print the instructor’s name for Instructor_ID “Bradley” and then print the Semesters, Courses and Sections that he taught: Varchar(60); varchar(10); BEGIN = (Last_Name + ', ' + First_name) FROM Instructor WHERE Instructor_ID = 'Bradley'; declare Courses cursor for (SELECT Semester, Course_ID, Section FROM Schedule WHERE Instructor_ID = 'Bradley'); OPEN Courses; FETCH @Section; WHILE = 0 + ' ' + ' ' + as varchar(5))); END; CLOSE Courses; DEALLOCATE Courses;

11 Stuff Not Database Related
Simple Counting Loop (SQLServer does NOT have a FOR loop!!) Integer; BEGIN = 1 < 10 + 1 END;

12 Selects in Procedures Scalar Selects Cursors Direct Select Statements
Returns One Row and can then directly have the values of the columns assigned to variables Cursors Returns a result set that one can then transverse through Direct Select Statements Like Regular Old Selects. Write the output out to the console or to a grid. Let’s add a select to our previous script.

13 Function Calls Must Return a value Basic Function Creation
CREATE (ALTER) FUNCTION Example Numeric) RETURNS Numeric AS BEGIN Numeric; = 2 END;

14 Get Age Function CREATE FUNCTION Date) RETURNS NUMERIC AS BEGIN integer; integer; = = Year(GETDATE()); = = Month(GETDATE()); Begin - 1; END; = = Day(GETDATE()); GO select dbo.GETAGE(birth_date), * from student

15 Function with Database Call
CREATE FUNCTION StudentName as Varchar(10)) RETURNS Varchar(50) AS BEGIN Varchar(60) = 'Not Found'; = (Last_Name + ', ' + First_Name) FROM Student WHERE Student_ID END;

16 Stored Procedure Like a Function, but is not required to return anything. There can be “OUTPUT” parameters, but normally it communicates back to the calling procedure with Error Codes and “Throws”. Can be called directly from Transact SQL or Management Studio

17 Simple Stored Procedure
CREATE PROCEDURE varchar, @Office varchar(100) output as integer; BEGIN FROM Instructor WHERE Instructor_ID IF > 0) ='Instructor ' +' already in table, insert aborted'; RETURN; END; INSERT INTO Instructor (Instructor_ID, Last_Name, First_Name, Office, City) @City); = 'Insert Complete for Instructor '

18 Calling a Stored Procedure
use registration go varchar(100); InsertInstructor output;

19 For Wednesday Triggers
Small Transact SQL programs that are “Triggered” when the RDMS processes a change to a table Used to maintain constraints, database consistency Used to automatically create log type entries into other tables for security.

20 For Tonight We Need to write a function that will return the GPA for a student. Let’s design the function What is the input? What is the output? What database involvement do we have? What is the processing?


Download ppt "COP 2700 – Data Structures (SQL)"

Similar presentations


Ads by Google