CREATEORREPLACEprocedure insertOne as BEGIN DELETEFROM APP_INFO WHERE APP_CODE='9999'; INSERTINTO APP_INFO(APP_CODE,APP_DESC,APP_NAME) VALUES('9999','通过','flsja'); END;
展示结果如图
有参存储过程
有参存储过程示例
1 2 3 4 5 6 7
CREATEORREPLACEPROCEDURE getAppInfo( code1 INvarchar, name1 OUTvarchar ) as BEGIN SELECT app_name INTO name1 FROM APP_INFO WHERE app_code=code1; END;
如图
结果集存储过程
返回过集存储过程示例,需要两步。
第一名,创建游标
1 2 3
CREATEORREPLACEPACKAGE pk_set AS TYPE cur ISREFCURSOR; END pk_set;
第二步,创建存储过程
1 2 3 4 5
CREATEORREPLACEPROCEDURE getApps( appcode INVARCHAR, rset OUT pk_set.cur ) AS BEGIN OPEN rset FORSELECT * FROM APP_INFO WHERE APP_CODE = appcode; END getApps;
createorreplacefunction totalCount() returnsintegeras $count$ declarecountinteger; begin selectcount(*) intocountfrom interface_info; return count; end; $count$ language plpgsql;
如图
有参存储过程
示例
1 2 3 4 5 6
createorreplacefunction finddescbyid( idcharactervarying, out desc1 charactervarying) returnscharactervaryinglanguagesqlas $function$ select interface_desc from interface_info where interface_code = id; $function$ ;
如图
结果集存储过程
示例
1 2 3 4 5 6 7
createorreplacefunction findCodeAndDescLimit(limit1 integer) returnstable("interfaceCode"charactervarying,"interfaceDesc"charactervarying) language plpgsql as $function$ beginreturnquery select interface_code,interface_desc from interface_info limit limit1; end; $function$ ;
如图
MySQL
无参存储过程
示例
1 2 3 4 5 6
DELIMITER ;; CREATEPROCEDURE getApps() BEGIN SELECT * FROM app_info; END;; DELIMITER ;
如图
有参存储过程
示例
1 2 3 4 5 6
DELIMITER ;; createprocedure getAppInfo(in appCode varchar(200),out appName varchar(200)) begin select app_name into appName from app_info where app_code=appCode; end;; DELIMITER ;
如图
结果集存储过程
示例
1 2 3 4 5 6
DELIMITER ;; createprocedure getAppLikeName(in name1 varchar(200)) begin select * from app_info where app_name like name1; end;; DELIMITER ;
如图
SQLServer
无参存储过程
示例
1 2 3 4 5
createprocedure getProject as begin select * fromproject; end;
如图
有参存储过程
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14
createprocedure GetProject_Out @Code nvarchar(64), @Namenvarchar(32) output as begin if(@Code isnotnulland @Code <> '') begin select @Name=project_name fromprojectwhere project_code=@Code end else begin set @Name='185' end end;
如图
结果集存储过程
示例
1 2 3 4 5
createprocedure getProjectLikeName(@name1 varchar(200)) as begin select * fromprojectwhere project_name like @name1; end
如图
DB2
无参存储过程
示例
1 2 3 4 5
createorreplaceprocedure simpleProcedure begin deletefrom data_info di where di.code='存储过程code'; insertinto data_info(code,name) values('存储过程code','存储过程name'); end
调用存储过程
1
call simpleProcedure
有参存储过程
示例
1 2 3 4 5 6 7
createorreplaceprocedure findByCode( in code11 varchar(50), out name11 varchar(50) ) begin select di.name into name11 from data_info di where di.code=code11; end
调用存储过程
1
call findByCode('存储过程1',?);
调用结果如图
结果集存储过程
示例
1 2 3 4 5 6 7 8 9 10 11 12
createorreplaceprocedure returnSet() /*定义返回结果集(游标)个数*/ dynamic resultsets1 begin begin /*定义一个游标,如果游标不只一个,上面的个数也要进行相应修改*/ declare rs cursorwith hold withreturntoclientfor select * from data_info; /*打开游标*/ open rs; end; end