CREATE PROCEDURE

CREATE PROCEDURE 语句用于在当前数据库中创建存储过程,与 MySQL 中 CREATE PROCEDURE 语句的行为类似。

语法图

CreateProcedureStmt
CREATEDEFINER=userPROCEDUREIfNotExistsProcedureNameprocParameterscharacteristicsroutineBody
procParameters
procParameterprocParameter,procParameters
procParameter
INparam_nametypeOUTparam_nametypeINOUTparam_nametype
type
AnyvalidMySQLdatatypeexceptionjson
ProcedureName
Identifier.Identifier
IfNotExists
IFNOTEXISTS
characteristics
characteristiccharacteristic,characteristics
characteristic
COMMENTstringSQLSECURITYSecurityInfo
SecurityInfo
DEFINERINVOKER
routineBody
statement
BlockStmt
begin_label:BEGINstatementListENDend_label
CaseStmt
CASEsimpleCaseListELSEstatementListENDCASECASEcase_valuesearchCaseListsELSEstatementListENDCASE
simpleCaseList
simpleCasesimpleCaseListssimpleCase
simpleCase
WHENsearch_conditionTHENstatementList
searchCaseList
searchCasesearchCaseListsearchCase
searchCase
WHENwhen_valueTHENstatementList
IfStmt
IFsearch_conditionTHENstatementListelseIfListELSEstatementListENDIF
elseIfList
elseIfelseIfListelseIf
elseIf
ELSEIFsearch_conditionTHENstatementList
LoopStmt
begin_label:LOOPstatementListENDLOOPend_label
RepeatStmt
begin_label:REPEATstatementListUNTILsearch_conditionENDREPEATend_label
WhileStmt
begin_label:WHILEsearch_conditionDOstatementListENDWHILEend_label
CloseCur
CLOSEcursor_name
DeclareVar
DECLAREvarNamestypeDEFAULTvalueDECLAREhandler_actionHANDLERFORconditionValuesstatementDECLAREcursor_nameCURSORFORselect_statement
varNames
varNamevarName,varNames
varName
string
conditionValues
conditionValueconditionValue,conditionValues
FetchInto
FETCHNEXTFROMcursor_nameINTOvarNames
OpenCur
OPENcursor_name
ProcedureSQL
SelectStmtUpdateStmtInsertStmtDeleteStmtShowStmtExplainStmtCreateTableStmtAlterTableStmtDropTableStmt
statementList
statement;statement;statementList
statement
DeclareVarFetchIntoOpenCurCloseCurWhileStmtRepeatStmtLoopStmtIfStmtCaseStmtBlockStmtProcedureSQL
handler_action
CONTINUEEXITUNDO
conditionValue
mysqlErrorCodeSQLSTATEVALUEsqlstateValueSQLWARNINGNOTFOUNDSQLEXCEPTION

平凯数据库不支持以下语法 characteristic:LANGUAGE SQL,[NOT] DETERMINISTIC,{ CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }

参数含义举例
tidb_enable_procedure开启或者关闭 tidb 存储过程tidb_enable_procedure = ON
tidb_enable_sp_param_substitute是否开启存储过程内部变量查询下推tidb_enable_sp_param_substitute=ON
max_sp_recursion_depth存储过程最多支持多少次递归max_sp_recursion_depth = 125
stored_program_cache最多缓存多少个存储过程执行计划stored_program_cache = 256

示例

创建一个存储过程:

use test delimiter $$ create procedure t1() begin select 1; end $$ delimiter ;
mysql> use test Database changed mysql> delimiter $$ mysql> create procedure t1() -> begin -> select 1; -> end $$ Query OK, 0 rows affected (0.01 sec) mysql> delimiter ;