The Language of SQL- P37 pptx

5 237 0
The Language of SQL- P37 pptx

Đang tải... (xem toàn văn)

Thông tin tài liệu

DATABASE DIFFERENCES: MySQL In MySQL, the previous example would look like: DELIMITER $$ CREATE PROCEDURE ProcedureOne () BEGIN SELECT * FROM Customers; END$$ DELIMITER ; Remember that creating a stored procedure does not execute anything. It simply creates the procedure so that it can be executed later. Along with tables and views, the procedure will be visible in your database management tool so that you can view its contents. Parameters in Stored Procedures All of the SELECT statements you have seen up until now have had a certain static quality due to the fact that they were written to retrieve data in one specific way. The ability to add parameters to SELECT statements gives you the possi- bility of much greater flexibility. The term parameter in SQL statements is similar to the term variable, as it is used in other computer languages. A parameter is basically a value that is passed to a SQL statement by the calling progr am. It can have whatever value the user spe- cifies at the time the call is made. Let’s start with a simple example. We have a SELECT statement that retrieves data from a Customers table. Rather than select all customers, we would like the SELECT to retrieve data for only one specific CustomerID number. However, we don’t want to code the number directly in the SELECT statement. We want the SELECT statement to be general enough so it can accept any provided CustomerID number and then execute with that value. The SELECT statement without any parameters is simply: SELECT * FROM Customers Chapter 16 ■ Stored Procedures and Parameters166 Our goal is to add a WHERE clause that will allow us to select data for a particular customer. In a general form, we’d like the SELECT statement to be: SELECT * FROM Customers WHERE CustomerID ¼ ParameterValue In Microsoft SQL Server, the creation of such a stored procedure can be accomplished with: CREATE PROCEDURE CustomerProcedure (@CustID INT ) AS BEGIN SELECT * FROM Customers WHERE CustomerID ¼ @CustID END Notice the addition of the second line, which specifies the CustID parameter in the procedure. In Microsoft SQL Server, the @ symbol is used to denote a para- meter. The INT keyword is placed after the parameter to indicate that this parameter will have an integer value. The same parameter name is used in the WHERE clause. DATABASE DIFFERENCES: MySQL In MySQL, the command to create an equivalent stored procedure is: DELIMITER $$ CREATE PROCEDURE CustomerProcedure (CustID INT) BEGIN SELECT * FROM Customers WHERE CustomerID ¼ CustID; END$$ DELIMITER ; Notice that MySQL doesn’t require the @ symbol to denote a parameter. Parameters in Stored Procedures 167 When a stored proced ure is executed, the calling program passes a value for the parameter, and the SQL statement is executed as if that value were part of the statement. It should also be noted that the parameters discussed previously are input para- meters. As such, they contain values that passed into the stored procedure. Stored procedures can also include output parameters, whic h can contain values passed back to the calling program. It’s beyond the scope of this book to discuss the various nuances of how to specify both input and output parameters in stored procedures. Executing Stored Procedures After stored procedures are created, how are they executed? The syntax varies between databases. Microsoft SQL Server provides the EXEC keyword to run stored procedures. In Microsoft SQL Server, the following statement will execute the ProcedureOne procedure: EXEC ProcedureOne When this statement is executed, it brings back the results of the SELECT state- ment contained in the stored procedure. The Proc edureOne procedure didn’t have any parameters, so the syntax is simple. How do you execute procedures with input parameters? The following executes the CustomerProcedure procedure for a CustID value of 2: EXEC CustomerProcedure @CustID ¼ 2 DATABASE DIFFERENCES: MySQL Rather than using EXEC, MySQL uses a CALL keyword to execute stored procedures, and the syntax for stored procedures with parameters is slightly different. The equivalent of the previous two EXEC statements in MySQL is: CALL ProcedureOne; CALL CustomerProcedure (2); Chapter 16 ■ Stored Procedures and Parameters168 Modifying and Deleting Stored Procedures Once a stored procedure is created, it can be modified. Just as the ALTER VIEW keyword was used to modify views, the ALTER PROCEDURE keyword is used to modify stored procedures. The syntax is identical to the CREATE PROCEDURE, except that the word ALTER is used in place of CREATE. Just as the CREATE PROCEDURE has a slightly different syntax for each database, so does the ALTER PROCEDURE . You’ve already seen thi s example of creating a stored procedure with Microsoft SQL Server: CREATE PROCEDURE CustomerProcedure (@CustID INT ) AS BEGIN SELECT * FROM Customers WHERE CustomerID ¼ @CustID END After this procedure is created, if you wanted to alter the procedure to select only the top five rows from the Customers table, the command to accomplish that would be: ALTER PROCEDURE CustomerProcedure (@CustID INT ) AS BEGIN SELECT TOP 5 * FROM Customers WHERE CustomerID ¼ @CustID END DATABASE DIFFERENCES: MySQL MySQL provides an ALTER PROCEDURE command, but it has limited functionality. To alter the content of a stored procedure in MySQL, you need to issue a DROP PROCEDURE and then a CREATE PROCEDURE with the new content. Deleting a stored procedure is even simpler. Just as the DROP VIEW deletes a view, the DROP PROCEDURE statement deletes a procedure. Modifying and Deleting Stored Procedures 169 Here’s how the stored procedure named CustomerProcedure can be deleted: DROP PROCEDURE CustomerProcedure Functions Revisited In Chapter 4, we talked about the built-in scalar functions that are available in SQL. For example, we used character functions such as LEFT and mathematical functions such as ROUND. In Chapter 10, we discussed aggregate functions such as MAX. In addition to the built-in functions in SQL, developers can create their own functions and save them in a database. The procedure for creating functions is very similar to the procedure for creating stored procedures. SQL provides the keywords CREATE FUNCTION, ALTER FUNCTION, and DROP FUNCTION, which work very much like CREATE PROCEDURE, ALTER PROCEDURE, and DROP PROCEDURE. Due to the advanced nature of this topic, we’re not going to provide specific examples of this functionality. However, we’ll spend a few moments explaining the differences between using stored procedures and functions. Both stored procedures and functions can be saved in a database. These entities are saved as separate objects in a database, much like tables or views. The pro- cedures for saving and modifying stored procedures and functions are very similar. The same CREATE, ALTER, and DROP commands for stored procedures can be used for functions. The difference between the two lies in how they are used and in their capabilities. There are two main distinc tions between stored procedures and functions: ■ Stored procedures can have any number of output parameters. They can even have zero output parameters. In contrast, a function must always contain exactly one output parameter. In other words, when you call a function, you always get back a single value. ■ Stored procedures are executed by a calling program. The stored procedure cannot be directly referenced in a SELECT statement. In contrast, functions can be referenced within statements, as seen in Chapters 4 and 10. After you define your own function, you’ll reference that function by the name you specified when the functio n was created. Chapter 16 ■ Stored Procedures and Parameters170 . @CustID END Notice the addition of the second line, which specifies the CustID parameter in the procedure. In Microsoft SQL Server, the @ symbol is used to denote a para- meter. The INT keyword. are they executed? The syntax varies between databases. Microsoft SQL Server provides the EXEC keyword to run stored procedures. In Microsoft SQL Server, the following statement will execute the. statements gives you the possi- bility of much greater flexibility. The term parameter in SQL statements is similar to the term variable, as it is used in other computer languages. A parameter

Ngày đăng: 05/07/2014, 05:20

Mục lục

  • Chapter 1 Relational Databases and SQL

    • Language and Logic

    • Microsoft SQL Server, Oracle, and MySQL

    • Primary and Foreign Keys

    • The Significance of SQL

    • Chapter 2 Basic Data Retrieval

      • A Simple SELECT

      • Column Names with Embedded Spaces

      • Chapter 3 Calculations and Aliases

        • Calculated Fields

        • Chapter 4 Using Functions

          • The Function of Functions

          • Chapter 5 Sorting Data

            • Adding a Sort

            • Sorting in Ascending Order

            • Sorting in Descending Order

            • Sorting by Multiple Columns

            • Sorting by a Calculated Field

            • More on Sort Sequences

            • Chapter 6 Column-Based Logic

              • IF-THEN-ELSE Logic

              • Chapter 7 Row-Based Logic

                • Applying Selection Criteria

                • Limiting Rows with a Sort

                • Chapter 8 Boolean Logic

                  • Complex Logical Conditions

                  • Multiple Sets of Parentheses

                  • Boolean Logic and NULL Values

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan