sql:PostgreSQL9.3 Using RETURNS TABLE vs. OUT parameters
http://www.postgresonline.com/journal/archives/201-Using-RETURNS-TABLE-vs.-OUT-parameters.html
http://www.postgresql.org/docs/9.4/interactive/index.html
--http://www.postgresonline.com/journal/archives/201-Using-RETURNS-TABLE-vs.-OUT-parameters.html
--ver:9.3 Geovin Du 涂聚文
--returning a single record using SQL function
CREATE OR REPLACE FUNCTION fn_sqltestout(param_subject text, pos integer)
RETURNS TABLE(subject_scramble text, subject_char text)
AS
$$
SELECT substring($1, 1,CAST(random()*length($1) As integer)) ,
substring($1, 1,1) As subject_char;
$$
LANGUAGE 'sql' VOLATILE;
-- example use
SELECT (fn_sqltestout('This is a test subject',1)).subject_scramble;
SELECT subject_scramble, subject_char FROM fn_sqltestout('This is a test subject',5); --Same function but written in plpgsql
--PLPGSQL example -- return one record
CREATE OR REPLACE FUNCTION fn_plpgsqltestout(param_subject varchar)
RETURNS TABLE(subject_scramble varchar, subject_char varchar)
AS
$$
BEGIN
subject_scramble := substring($1, 1,CAST(random()*length($1) As varchar));
subject_char := substring($1, 1,1);
RETURN NEXT;
END;
$$
LANGUAGE 'plpgsql' VOLATILE; -- example use
SELECT (fn_plpgsqltestout('This is a test subject')).subject_scramble;
SELECT subject_scramble, subject_char FROM fn_plpgsqltestout('This is a test subject'); -- test data to use --
CREATE TABLE testtable(id integer PRIMARY KEY, test text);
INSERT INTO testtable(id,test)
VALUES (1, 'Potato'), (2, 'Potato'), (3, 'Cheese'), (4, 'Cheese Dog'); --SQL function returning multiple records
CREATE OR REPLACE FUNCTION fn_sqltestmulti(param_subject varchar)
RETURNS TABLE(test_id integer, test_stuff text)
AS
$$
SELECT id, test
FROM testtable WHERE test LIKE $1;
$$
LANGUAGE 'sql' VOLATILE; -- example use
SELECT (fn_sqltestmulti('Cheese%')).test_stuff;
SELECT test_stuff FROM fn_sqltestmulti('Cheese%'); -- plpgsql function returning multiple records
-- note RETURN QUERY was introduced in 8.3
-- variant 1
CREATE OR REPLACE FUNCTION fn_plpgsqltestmulti(param_subject varchar)
RETURNS TABLE(test_id integer, test_stuff text)
AS
$$
BEGIN
RETURN QUERY SELECT id, test
FROM testtable WHERE test LIKE param_subject;
END;
$$
LANGUAGE 'plpgsql' VOLATILE; --测试
select * from fn_plpgsqltestmulti('Cheese%'); -- variant 2 use this if you need to do something additional
-- or conditionally return values or more dynamic stuff
-- RETURN QUERY is generally more succinct and faster
CREATE OR REPLACE FUNCTION fn_plpgsqltestmulti(param_subject varchar)
RETURNS TABLE(test_id integer, test_stuff text)
AS
$$
DECLARE
var_r record;
BEGIN
FOR var_r IN(SELECT id, test
FROM testtable WHERE test LIKE param_subject) LOOP
test_id := var_r.id ; test_stuff := var_r.test;
RETURN NEXT;
END LOOP;
END;
$$
LANGUAGE 'plpgsql' VOLATILE;
-- example use
-- This is legal in PostgreSQL 8.4+
-- (prior versions plpgsql could not be called this way)
SELECT (fn_plpgsqltestmulti('Cheese%')).test_stuff; SELECT * FROM fn_plpgsqltestmulti('Cheese%');
--函数 涂聚文 Geovin Du
CREATE FUNCTION check_password(uname TEXT, pass TEXT)
RETURNS BOOLEAN AS $$
DECLARE passed BOOLEAN;
BEGIN
SELECT (pwd = $2) INTO passed
FROM pwds
WHERE username = $1; RETURN passed;
END;
$$ LANGUAGE plpgsql
SECURITY DEFINER
-- Set a secure search_path: trusted schema(s), then 'pg_temp'.
SET search_path = admin, pg_temp; ---http://www.postgresql.org/docs/current/static/sql-createfunction.html
CREATE OR REPLACE function f_GetDepartmentName
(
did integer
)
returns varchar as $$
declare str varchar;
begin
select DepartmentName INTO str from DepartmentList where DepartmentID=did;
return str;
end;
$$language plpgsql; --测试
select f_GetDepartmentName(1) as name; --( (select DepartmentName from DepartmentList where DepartmentID = in_id) union (select name from test_result2 where id = in_id) )
CREATE OR REPLACE FUNCTION func_DepartmentMore ( in_id integer)
RETURNS SETOF varchar as
$$
DECLARE
v_name varchar;
BEGIN
for v_name in (select DepartmentName from DepartmentList where DepartmentID = in_id)loop
RETURN NEXT v_name;
end loop;
return;
END;
$$
LANGUAGE PLPGSQL;
---
select func_DepartmentMore(1); --- CREATE OR REPLACE FUNCTION func_DepartmentName_muti (in_id integer)
RETURNS SETOF RECORD as
$$
DECLARE
v_rec RECORD;
BEGIN for v_rec in (select DepartmentID,DepartmentName from DepartmentList where DepartmentID = in_id)loop
RETURN NEXT v_rec;
end loop;
return;
END;
$$
LANGUAGE PLPGSQL; --测试
select * from func_DepartmentName_muti(1) t(DepartmentID integer,DepartmentName varchar); CREATE OR REPLACE FUNCTION func_DepartmentName_query ( in_id integer)
RETURNS SETOF RECORD as
$$
DECLARE
v_rec RECORD;
BEGIN return query(select DepartmentID,DepartmentName from DepartmentList where DepartmentID = in_id);
return;
END;
$$
LANGUAGE PLPGSQL; --测试
select * from func_DepartmentName_query(1) t(DepartmentID integer,DepartmentName varchar); ---http://www.postgresonline.com/journal/archives/129-Use-of-OUT-and-INOUT-Parameters.html
CREATE OR REPLACE FUNCTION func_DepartmentName_out( in_id integer,out o_id integer,out o_name varchar)
RETURNS SETOF RECORD as
$$
DECLARE
v_rec RECORD;
BEGIN
for v_rec in ( select DepartmentID,DepartmentName from DepartmentList where DepartmentID = in_id)loop
o_id := v_rec.DepartmentID;
o_name := v_rec.DepartmentName;
RETURN NEXT ;
end loop;
return;
END;
$$
LANGUAGE PLPGSQL; --测试 select DepartmentID,DepartmentName from DepartmentList select * from func_DepartmentName_out(1); select * from func_DepartmentName_out(2); ---
CREATE OR REPLACE FUNCTION func_table(in_id int) RETURNS TABLE(f1 int, f2 varchar)
AS
$$
begin
SELECT f1=DepartmentID, f2=DepartmentName from DepartmentList where DepartmentID =in_id;
end;
$$
LANGUAGE SQL; SELECT * FROM dup(42); CREATE OR REPLACE FUNCTION fn_plpgsqltestout(param_subject text,
OUT subject_scramble text, OUT subject_char text)
AS
$$
BEGIN
subject_scramble := substring($1, 1,CAST(random()*length($1) As integer));
subject_char := substring($1, 1,1);
END;
$$
LANGUAGE 'plpgsql' VOLATILE; --测试 select fn_plpgsqltestout('geovindu'); CREATE OR REPLACE FUNCTION fn_sqltestmulti(param_subject varchar,
OUT test_id integer,
OUT test_stuff text)
RETURNS SETOF record
AS
$$
SELECT DepartmentID,DepartmentName
FROM DepartmentList where DepartmentName LIKE $1;
$$
LANGUAGE 'sql' VOLATILE; --测试
SELECT * FROM fn_sqltestmulti('%d%');
--OUT takes precendence which is why we prefix the table columns
CREATE OR REPLACE FUNCTION fn_plpgsqltestmulti(
param_subject varchar,
OUT test_id integer,
OUT test_stuff varchar)
RETURNS SETOF record
AS
$$
BEGIN
RETURN QUERY SELECT t.DepartmentID , t.DepartmentName
FROM DepartmentList As t
WHERE t.DepartmentName LIKE param_subject;
END;
$$
LANGUAGE 'plpgsql' VOLATILE; SELECT * FROM fn_plpgsqltestmulti('%d%',1, 'd');
sql:PostgreSQL9.3 Using RETURNS TABLE vs. OUT parameters的更多相关文章
- PL/pgSQL RETURNS TABLE 例子
实验如下: RETURNS TABLE 中的变量名和SQL文中的变量名同名时,执行时会出错: pgsql=# create table sales(itemno integer,quantity in ...
- SQL Azure (18) 使用External Table实现垮库查询
<Windows Azure Platform 系列文章目录> 问题 1.我们在进行SQL Server开发的时候,经常会使用垮库查询.但是在默认情况下,使用Azure SQL Datab ...
- 转载:SQL Server 2008-建立分区表(Table Partition) 转载
数据库结构和索引的是否合理在很大程度上影响了数据库的性能,但是随着数据库信息负载的增大,对数据库的性能也发生了很大的影响.可能我们的数据库在一开始有着很高的性能,但是随着数据存储量的急速增长—例如订单 ...
- 传入sql数组字符串,输出table
CREATE function [dbo].[split](@aString varchar(),@pattern varchar()) returns @temp table([Sid] [, ) ...
- linux之SQL语句简明教程---CREATE TABLE
表格是数据库中储存资料的基本架构.在绝大部份的情况下,数据库厂商不可能知道您需要如何储存您的资料,所以通常您会需要自己在数据库中建立表格.虽然许多数据库工具可以让您在不需用到 SQL 的情况下建立表格 ...
- oracle学习之路(四) ---------PL/SQL 表,二维数组(TABLE)
LOB类型 ORACLE提供了LOB (Large OBject)类型.用于存储大的数据对象的类型.ORACLE眼下主要支持BFILE, BLOB, CLOB 及 NCLOB 类型. NCLOB 存储 ...
- sql 将字符串转化为table
/* *参数说明: 第一个参数为要转化的字符串,第二个参数为字符串中间的分隔符 */ ),)) )) as begin ) set @SourceSql=@SourceSql+@StrSeprate ...
- sql sever 授予用户create table权限
sql server2008的用户权限管理及其细致.此处我也不知道是表扬还是批评.所以经常会遇到各种“对不起您没有**权限”的问题. 本次遇到的问题机器常见.在尝试修改数据库结构时提示"没有 ...
- SQL Server 2008 R2——TRUNCATE TABLE 无法截断表 该表正由 FOREIGN KEY 约束引用
=================================版权声明================================= 版权声明:原创文章 禁止转载 请通过右侧公告中的“联系邮 ...
随机推荐
- day 02 ---class - homework
# -*- coding: utf-8 -*-# @Time : 2018/12/20 14:34# @Author : Endless-cloud# @Site : # @File : day 02 ...
- Protobuf底层存储原理
参考官网, 序列化原理 底层二进制存储 message Test1 { optional int32 a = 1; } 并设置为a=150,序列化到一个文件中,查看文件,得到下面的二进制: 08 96 ...
- day3.python 学习之列表
python中列表用[ ]表示, list = [ ] #表示一个空列表 1.list = [ 'A','B','C',‘D’] print(list[0]) # 表示打印出列表中的第一个元素,列表 ...
- 6、TensorFlow基础(四)队列和线程
队列和线程 和 TensorFlow 中的其他组件一样,队列(queue)本身也是图中的一个节点,是一种有状态的节点,其他节点,如入队节点(enqueue)和出队节点(dequeue),可以修改它的内 ...
- 除法保留小数bit位 + 四舍五入
/*num1/num2 保留bit 位小数 , 四舍五入*/ #include<bits/stdc++.h> using namespace std; ]; int main() { in ...
- (一)Audio子系统之AudioRecord.getMinBufferSize
在文章<基于Allwinner的Audio子系统分析(Android-5.1)>中已经介绍了Audio的系统架构以及应用层调用的流程,接下来,继续分析AudioRecorder方法中的ge ...
- js 实现星级评分
最近的项目中有一个星级评分的需求, 自己就写了一下, 由于可能一个页面要用到多个,就采用了面向对象的写法. 用到的png图片也放到这里. js要用到jquery. css: .sr-star{ ...
- mysql数据库基本知识
一.库操作 创建数据库:creat database 'mydababase1';creat database if not exists 'mydababase1' //只有两个选项 查询数据库: ...
- 结合React使用Redux
前面的两篇文章我们认识了 Redux 的相关知识以及解决了如何使用异步的action,基础知识已经介绍完毕,接下来,我们就可以在React中使用Redux了. 由于Redux只是一个状态管理工具,不针 ...
- hibernate一对多多对一双向
注意事项:一对多,多对一双向关联,在一的一方的多的getSet集合上的oneToMany上加上mappedBy.告诉hibernate由多的方一来维护关系.这也符合逻辑 ,本来外键就是在加在多的一方. ...