sql programming
DESCRIPTION
Introduction to stored procedures, user defined functions and views.TRANSCRIPT
![Page 1: SQL Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/1.jpg)
Progamming in SQL
CIS-182
![Page 2: SQL Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/7.jpg)
View Code Sample
![Page 8: SQL Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/10.jpg)
Sample SPROC Code
![Page 11: SQL Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/13.jpg)
Input Parameters -1
![Page 14: SQL Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/14.jpg)
Input Parameters - 2
Show data before run sproc
Show data after run sproc
![Page 15: SQL Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/16.jpg)
Output Parameter Example
![Page 17: SQL Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/19.jpg)
Scalar Function
![Page 20: SQL Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062319/55620ae2d8b42acb1e8b4767/html5/thumbnails/20.jpg)
Table Function