Your SlideShare is downloading. ×
SQL Programming
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

SQL Programming

780
views

Published on

Introduction to stored procedures, user defined functions and views.

Introduction to stored procedures, user defined functions and views.

Published in: Education, Technology

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
780
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
31
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Progamming in SQL
    CIS-182
  • 2. 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
  • 3. 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
  • 4. 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
  • 5. Creating a View
    Define the SELECT statement
    Add the Data Definition Language statement before the SELECT
    CREATE VIEW v_TitleWithPublisher
    AS
    SELECT t.*, pub_name Publisher
    FROM titles t JOIN publishers p
    ON t.pub_id=p.pub_id
  • 6. Using a View
    A view is used in place of a table
    Can apply criteria, grouping, sorting
    SELECT publisher, title, price
    FROM v_TitleWithPublisher
    WHERE price>20
    ORDER BY title
  • 7. View Code Sample
  • 8. 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
  • 9. 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_GetAuthors
    AS
    SELECT *
    FROM authors
  • 10. Sample SPROC Code
  • 11. 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
  • 12. Input Parameters
    Can use value coming into sproc
    As part of WHERE clause
    WHERE price>@SearchPrice
    For value to store in a table
    UPDATE titles
    SET price=@NewPrice
    WHERE title_id= @title_id
  • 13. Input Parameters -1
  • 14. Input Parameters - 2
    Show data before run sproc
    Show data after run sproc
  • 15. 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
  • 16. Output Parameter Example
  • 17. 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
  • 18. 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)
  • 19. Scalar Function
  • 20. Table Function