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
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
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
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
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)