View
803
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Introduction to stored procedures, user defined functions and views.
Citation preview
Progamming in SQL
CIS-182
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
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
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
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
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
View Code Sample
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
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
Sample SPROC Code
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
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
Input Parameters -1
Input Parameters - 2
Show data before run sproc
Show data after run sproc
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
Output Parameter Example
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
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)
Scalar Function
Table Function