SQL Dialects Reference/Procedural language/Stored procedures

From Wikibooks, open books for an open world
Jump to: navigation, search

Stored procedures[edit]

Database Create syntax Calling
DB2
CREATE PROCEDURE procedure_name(...)
   BEGIN
   /* SQL code */
   END
CALL procedure_name(...)
Firebird
SET TERM $$ ;

CREATE PROCEDURE nameprocedure
  (input_parameter_name datatype, ... ) 
RETURNS 
  (output_parameter_name datatype, ... )
AS 
DECLARE VARIABLE variable_name datatype;
BEGIN
  /* SQL code */
END$$

SET TERM ; $$
SELECT ... FROM function_name(...)
EXECUTE function_name(...)
MonetDB
CREATE PROCEDURE procedure_name(...)
   BEGIN
   /* SQL code */
   END
CALL procedure_name(...)
MySQL
DELIMITER $$

CREATE PROCEDURE nameprocedure
  (input_parameter_name datatype, ... )
BEGIN
  /* SQL code */
END$$

DELIMITER ;
CALL nameprocedure(...)
Linter
CREATE [OR REPLACE] PROCEDURE procedure_name([IN/OUT/INOUT] parameter_name datatype, ...) [RESULT datatype] [FOR DEBUG]
   DECLARE 
   /* variables declaration */
   CODE
   /* stored procedure code 
      (including SQL code)*/
   EXCEPTIONS
   /* exceptions declarations */
   END
CALL procedure_name(...)

EXECUTE procedure_name(...)

EXECUTE procedure_name(...) AS OWNER

SELECT procedure_name(...)
FROM ...
WHERE
procedure_name(...) = ...
OpenLink Virtuoso
Oracle
CREATE [OR REPLACE] PROCEDURE procedure_name [ (parameter [,parameter]) ]
IS
    [declaration_section]
BEGIN
    executable_section
    [EXCEPTION
        exception_section]
END [procedure_name];
BEGIN
  PACKAGE_NAME.procedure_name(...);
END;
PostgreSQL
CREATE FUNCTION function_name
  (input_parameter_name datatype, ...)
RETURNS return_type
AS $$
DECLARE
  variable_name datatype;
BEGIN
  /* SQL code */
END;
$$ LANGUAGE plpgsql;
SELECT function_name(...)
SQL Server
CREATE PROCEDURE nameprocedure
  (input_parameter_name datatype, ... )
AS
  /* SQL code */
GO
EXEC nameprocedure(...)
SQLite

N/A

N/A