20
Progamming in SQL CIS-182

SQL Programming

Embed Size (px)

DESCRIPTION

Introduction to stored procedures, user defined functions and views.

Citation preview

Page 1: SQL Programming

Progamming in SQL

CIS-182

Page 2: SQL Programming

Overview

• Storing SQL Code can be done in text files or objects

• Scripts are text files independent of any particular database or server– .sql files are scripts

• Objects are stored as part of a database– Tend to be part of application “back

end” to manage and make use of data

Page 3: SQL Programming

SQL Programming Objects

• Views are predefined SELECT statements

• Stored Procedures are instructions about completing a task– Triggers are special stored procedures

executing automatically if data or an object changes

• User-defined Functions are instructions used as part of another statements

• DDL is used to create each object

Page 4: SQL Programming

Views

• A view is a predefined selected statement– Also referred to as a “virtual table”

• Allows easier access to data– Don’t need to remember all of the joins

required to put data together

• Allows data hiding– Can limit the rows or columns returned

• Allows structure hiding– Can hide how data is actually stored

Page 5: SQL Programming

Creating a View

• Define the SELECT statement• Add the Data Definition Language

statement before the SELECT

CREATE VIEW v_TitleWithPublisherASSELECT t.*, pub_name PublisherFROM titles t JOIN publishers pON t.pub_id=p.pub_id

Page 6: SQL Programming

Using a View

• A view is used in place of a table• Can apply criteria, grouping, sorting

SELECT publisher, title, priceFROM v_TitleWithPublisherWHERE price>20ORDER BY title

Page 7: SQL Programming

View Code Sample

Page 8: SQL Programming

Stored Procedures

• Stored procedures are similar to procedures in other languages– Designed to complete a particular task

• Add a row• Return a value or table

– Also referred to as “sproc”

• Use CREATE, ALTER to code • Use Execute (or EXEC) to run

Page 9: SQL Programming

Simple Stored Procedure

• To get a list of all authors, code a SELECT statement

• Add a CREATE statement prior to the SELECT

CREATE PROC up_GetAuthorsASSELECT *FROM authors

Page 10: SQL Programming

Sample SPROC Code

Page 11: SQL Programming

Parameters

• Parameters are used to send data to a procedure – Referred to as arguments in many

languages– Define a name and data type

• Name starts with ‘@’

• Parameters can be INPUT or OUTPUT– Input parameters are values going into

the procedure– Output parameters are values being

used outside of the procedure

Page 12: SQL Programming

Input Parameters

• Can use value coming into sproc– As part of WHERE clauseWHERE price>@SearchPrice

– For value to store in a tableUPDATE titlesSET price=@NewPriceWHERE title_id= @title_id

Page 13: SQL Programming

Input Parameters -1

Page 14: SQL Programming

Input Parameters - 2

Show data before run sproc

Show data after run sproc

Page 15: SQL Programming

Output Parameters

• Output parameters are used to return a value– Direction must be specified when define

parameter– When use sproc, must also provide a

variable to capture value

• If want average price of all books can use an output parameter

Page 16: SQL Programming

Output Parameter Example

Page 17: SQL Programming

Functions

• Also known as user-defined functions, or “udf”

• Functions return a value for use as part of another statement– Also referred to as ‘In-Line’– Sprocs execute as single statements

• Can use input parameters if need to pass in a value– Parameters enclosed in parentheses– UDF’s don’t have output parameters

Page 18: SQL Programming

Function Values

• Functions may return different types of results– Scalar functions return a single value

(string, date, integer– Table functions return a table (rows and

columns)• May be single or multiple statements to

generate a table

• When use, need to qualify with where function is (schema)

Page 19: SQL Programming

Scalar Function

Page 20: SQL Programming

Table Function