mysql write functions and stored procedures

New Function

grammar:

$$ DELIMITER 
the CREATE 
    / * [User DEFINER = {| CURRENT_USER}] * / 
    the FUNCTION datacenterv2`.`functionName` `() - parentheses fill parameter format: name of the parameter type, such as int dRe 
    the RETURNS the TYPE - Return value type 
    BEGIN 
    - statements 
    END $$ 
DELIMITER;

 if functions

IF(sIsByArea = '0') THEN
       SELECT 
            IFNULL(DATE_FORMAT(`AddTime`,'%Y-%m-%d'),'')
       FROM 
       tb_avgprice
       WHERE ProvinceID = sIsByArea
       ORDER BY `AddTime` DESC LIMIT 1 INTO dRe;
    ELSE
    SELECT  
        IFNULL(DATE_FORMAT(`AddTime`,'%Y-%m-%d'),'')
    FROM tb_avgprice  
    WHERE ProvinceID != '0'
    ORDER BY `AddTime` DESC  LIMIT 1 INTO dRe;
    IF (dRe='' ) THEN
      SELECT DATE_FORMAT(FN_GetFisrtDateOfValidatedPrice(),'%Y-%m-%d') INTO dRe;
    END IF;
    END IF;

The code contains

If the function usage

if expression then statement end if

if expression then statement else statement end if

if expression then statements elseif statement else statement end if

Note: After endif, remember to add;

 

ifnull usage

Determining whether null, and if so, returns the second parameter

IFNULL(expr1,expr2)

 

select expr1 into dRe

dRe is variable

select expr1  limit 1

The first query

Date Format Conversion

date_format function

DATE_FORMAT (date, '% Y-% m-% d')

Calling Custom Functions

select function names

Call custom functions in the custom function's statement

 

 

 

 

Guess you like

Origin www.cnblogs.com/zyc19910109/p/11818829.html