创建语句CREATE实例:
CREATE PROCEDURE <proc name>
[(<parameter clause>)].[LANGUAGE <lang>]
[SQL SECURITY <mode>] [READS SQL DATA]
[WITH RESULT VIEW <view name>] AS
<local scalar variables>
BEGIN
<Procedure cade>
END
我们将这个语句拆分开解释:
CREATE PROCEDURE [()]
上面例子已经介绍,给出存储过程名称和输入输出参数及其数据类型。
[LANGUAGE ]指定存储过程用什么语言,一般为SQL Script,也可以是其他语言,比如R语言。
[SQL SECURITY ]
指定存储过程执行的安全模式,可以是以存储过程DEFINER定义者的权限去执行,也可以设定为以调用者INVOKER权限去执行。
[READS SQL DATA]
指定该存储过程为只读存储过程,没有更新数据的相关的DML语句或者DDL"语句。
[WITH RESULT VIEW ]
用来将读取的数据结果写入一个新的视图中,后面会详细介绍。
AS
可以用来定义本地local变量
创建主表 publishers
CREATE COLUMN TABLE publishers (
pub_id INTEGER PRIMARY KEY,
name VARCHAR (50),
street VARCHAR (50),
post_code VARCHAR (10),
city VARCHAR (50),
country VARCHAR (50));
INSERT INTO publishers VALUES(1, 'SAP Press', 'Chenhui Road 1001', '201203', 'Shannghai', 'China');
INSERT INTO publishers VALUES(2, 'Tsinghua Uni Press', 'Tshinghua Road 1001', '100084', 'beijing', 'china');
INSERT INTO publishers VALUES(3, 'BeiJing Uni Press', 'peking Road 1001', '100084', 'beiiing','China');
INSERT INTO publishers VALUES(4, 'Fudan Uni Press', 'Fudan Road 1001','201203', 'Shannghai','China');
INSERT INTO publishers VALUES(5, 'rongji Uni Press', 'Tongji Road 1001',' 201203', 'Shannghai', 'China');
CREATE COLUMN TABLE books
( isbn VARCHAR (20) PRIMARY KEY,
title VARCHAR (50),
publisher INTEGER,
edition INTEGER,
year VARCHAR (4),
price DECIMAL (5, 2),
crcy VARCHAR(3)
);
INSERT INTO books VALUES ('978-3-486-57690-0', 'HANA APP DEV GUIDE', 1, 6, '2006', '39.80', 'RMB');
INSERT INTO books VALUES ('978-3-86894-012-1', 'IN MEMORY DATABASE', 2, 3, '2009', '29.95', 'RMB');
INSERT INTO books VALUES ('978-3-8266-1664-8', 'HANA ADMIN GUIDE', 3, 3, '2008', '39.95', 'RMB');
INSERT INTO boaks VALUES ('978-3-8266-1665-8', 'Mobility Dev', 1, 3, ,'2008', '49.95', 'RMB');
INSERT INTO books VALUES ('978-3-8266-1665-9', 'ios Dev', 4, 3,'2010', '42.95','RMB');
INSERT INTO books VALUES ('978-3-8266-1665-2', 'Andriod Dev', 5, 3,'2011', '22.95','RMB');
INSERT INTO books VALUES ('978-3-8266-1665-4', 'SAP SUP Intro', 1,3,'2012','24.55','RMB');
CREATE COLUMN TABLE audiobooks(
isbn VARCHAR (20) PRIMARY KEY,
title VARCHAR (50),
publisher INTEGER,
year VARCHAR (4),
price DECIMAL (5, 2),
Crcy VARCHAR (3)
);
INSERT INTO audiobooks VALUES ('978-39388781-37-1', 'Timemanagement', 4, '2006', '24.90', 'EUR');
创建输出表类型
CREATE TYPE tt_publishers AS TABLE
(publisher INTEGER,
name VARCHAR (50),
price DECIMAL,
cnt INTEGER) ;
CREATE TYPE tt_years AS TABLE
(year VARCHAR (4) ,
price DECIMAL,
cnt INTEGER);
CREATE TABLE message_box( messaqe VARCHAR (200), log_time TIMESTAMP);
-- 初始化消息存储过程
CREATE PROCEDURE init_proc LANGUAGE SQLSCRIPT AS
BEGIN
DELETE FROM message_box;
END;
-- 增加消息的存储过程
CREATE PROCEDURE ins_msg_proc (p_msg VARCHAR (200))
LANGUAGE SQLSCRIPT AS
BEGIN
INSERT INTO message_box VALUES (:p_msg,CURRENT_TIMESTAMP);
END;
call "REPOUSER"."INS_MSG_PROC"('我来了')
-- procedure
CREATE PROCEDURE getOutput(
IN cnt INTEGER,
IN currency VARCHAR(3),
out output_pubs TT_PUBLISHERS,
out output_year TT_YEARS)
--LANGUAGE SQLSCRIPT SQL SECURITY DEFINER READS
LANGUAGE SQLSCRIPT SQL SECURITY DEFINER READS SQL DATA
--LANGUAGE SQLSCRIPT READS SQL DATA
AS
--do
BEGIN
--DECLARE cnt INTEGER ;
--DECLARE currency VARCHAR(3) ;
--Query1 取得满足条件的出版商ID列表(以“-"开头为注释行)
big_pub_ids =
SELECT publisher AS pid FROM books GROUP BY publisher HAVING COUNT(isbn) > :cnt;
--Query2 取得根据ID列表和其他输入值读取书籍相关数换
big_pub_books =
SELECT title, name, publisher, year, price FROM :big_pub_ids, publishers, books WHERE pub_id = pid AND pub_id = publisher AND crcy = :currency;
--Query3 以出版商为基础对Query 2的数据做聚合-价格SUM
output_pubs =
SELECT publisher, name, SUM(price) As price, COUNT(title) AS cnt FROM :big_pub_books GROUP BY publisher,name;
--Query4 以时间年份为基础,对Query 2数据做聚合-价格SUM
output_year =
SELECT year, SUM(price) AS price, COUNT(title) AS cnt FROM :big_pub_books GROUP BY year;
END;
call getOutput(0,'RMB',null,null)
第一个查询我们从“BOOKS”表中取到了发行书数量达到一定要求
(根据输入值cnt)的出版商ID:
第二个查询我们依据出版商ID,从"PUBLISHERS"表和"BOOKS"
表中取到书和出版商细节信息;
第三个和第四个查询则对第二个查询取的数据价格和书的数量
做了些聚合,第三个的聚合依据出版商"PUBS" ,第四个的聚合
依据发行年份"YEAR"
这条语句的执行结果就会把输出结果集1写入到op_publishers这张表中,把结果集2写入到op_years这张表中。
创建表的代码:
CREATE TABLE op_publishers(
publisher INTEGER,
name VARCHAR (50),
price DECIMAL,
cnt INTEGER
) ;
CREATE TABLE op_years(
year VARCHAR (4),
price DECIMAL,
cnt INTEGER);
CALL getOutput (0, 'RMB', op_publishers, op_years) WITH OVERVIEW;
附加:
只会在当前用户下的 Schema下
以下两种常见创建方式:
1.SQL创建
Create procedure prc_name (in inp integer, out opt "HOTEL"."HOTEL")
as
begin
opt = select * from "HOTEL"."HOTEL" where HNO = :inp ;
end;
与开始的一样,都在当前用户的 Schema 下。
另一种方式创建:
PROCEDURE "HOTEL"."com.demo.xr::test01" (in inp integer, out opt "HOTEL"."HOTEL") LANGUAGE SQLSCRIPT SQL SECURITY INVOKER --DEFAULT SCHEMA
创建在指定的 SCHEMA 下