First page Back Continue Last page Image

Example: Function that returns key field from table

DELIMITER //

CREATE FUNCTION get_vendor_id

(

vendor_name_param VARCHAR(50)

)

RETURNS INT

DETERMINISTIC READS SQL DATA

BEGIN

DECLARE vendor_id_var INT;

SELECT vendor_id

INTO vendor_id_var

FROM vendors

WHERE vendor_name = vendor_name_param;

RETURN(vendor_id_var);

END//

Start with the CREATE FUNCTION Command

Identify the type of the variable returned by the function

Identify the characteristics for the function

Note: You must code either

DETERMINISTIC

NO SQL

or

READS SQL DATA

on a function

In this function the calling code provides the Vendor Name and the Vendor ID field is returned.. “What is the vendor id for amazon?”