Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

SQL Programming


Published on

Introduction to stored procedures, user defined functions and views.

Published in: Education, Technology
  • Be the first to comment

SQL Programming

  1. 1. Progamming in SQL<br />CIS-182<br />
  2. 2. Overview<br />Storing SQL Code can be done in text files or objects<br />Scripts are text files independent of any particular database or server<br />.sql files are scripts<br />Objects are stored as part of a database<br />Tend to be part of application “back end” to manage and make use of data <br />
  3. 3. SQL Programming Objects<br />Views are predefined SELECT statements<br />Stored Procedures are instructions about completing a task<br />Triggers are special stored procedures executing automatically if data or an object changes<br />User-defined Functions are instructions used as part of another statements<br />DDL is used to create each object<br />
  4. 4. Views<br />A view is a predefined selected statement<br />Also referred to as a “virtual table”<br />Allows easier access to data<br />Don’t need to remember all of the joins required to put data together<br />Allows data hiding<br />Can limit the rows or columns returned<br />Allows structure hiding<br />Can hide how data is actually stored<br />
  5. 5. Creating a View<br />Define the SELECT statement<br />Add the Data Definition Language statement before the SELECT<br />CREATE VIEW v_TitleWithPublisher<br />AS<br />SELECT t.*, pub_name Publisher<br />FROM titles t JOIN publishers p<br />ON t.pub_id=p.pub_id<br />
  6. 6. Using a View<br />A view is used in place of a table<br />Can apply criteria, grouping, sorting<br />SELECT publisher, title, price<br />FROM v_TitleWithPublisher<br />WHERE price&gt;20<br />ORDER BY title<br />
  7. 7. View Code Sample<br />
  8. 8. Stored Procedures<br />Stored procedures are similar to procedures in other languages<br />Designed to complete a particular task<br />Add a row<br />Return a value or table<br />Also referred to as “sproc”<br />Use CREATE, ALTER to code <br />Use Execute (or EXEC) to run<br />
  9. 9. Simple Stored Procedure<br />To get a list of all authors, code a SELECT statement <br />Add a CREATE statement prior to the SELECT<br />CREATE PROC up_GetAuthors<br />AS<br />SELECT *<br />FROM authors<br />
  10. 10. Sample SPROC Code<br />
  11. 11. Parameters<br />Parameters are used to send data to a procedure <br />Referred to as arguments in many languages<br />Define a name and data type<br />Name starts with ‘@’<br />Parameters can be INPUT or OUTPUT<br />Input parameters are values going into the procedure<br />Output parameters are values being used outside of the procedure<br />
  12. 12. Input Parameters<br />Can use value coming into sproc<br />As part of WHERE clause<br />WHERE price&gt;@SearchPrice<br />For value to store in a table<br />UPDATE titles<br />SET price=@NewPrice<br />WHERE title_id= @title_id<br />
  13. 13. Input Parameters -1 <br />
  14. 14. Input Parameters - 2<br />Show data before run sproc<br />Show data after run sproc<br />
  15. 15. Output Parameters<br />Output parameters are used to return a value<br />Direction must be specified when define parameter<br />When use sproc, must also provide a variable to capture value<br />If want average price of all books can use an output parameter<br />
  16. 16. Output Parameter Example<br />
  17. 17. Functions<br />Also known as user-defined functions, or “udf”<br />Functions return a value for use as part of another statement<br />Also referred to as ‘In-Line’<br />Sprocs execute as single statements<br />Can use input parameters if need to pass in a value<br />Parameters enclosed in parentheses<br />UDF’s don’t have output parameters<br />
  18. 18. Function Values<br />Functions may return different types of results<br />Scalar functions return a single value (string, date, integer<br />Table functions return a table (rows and columns)<br />May be single or multiple statements to generate a table<br />When use, need to qualify with where function is (schema)<br />
  19. 19. Scalar Function<br />
  20. 20. Table Function<br />