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.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‘);

  

时间: 2024-10-10 04:49:02

sql:PostgreSQL9.3 Using RETURNS TABLE vs. OUT parameters的相关文章

SQL 创建联合主键Table

CREATE TABLE [User_Instance]( [IntanceID] [int] NOT NULL, [StaffID] [char](10) NOT NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO ALTER TABLE [[User_Instance] WITH NOCHECK ADD CONSTRAINT [PK_User_Instance] PRIMARY KEY NONCLUSTERED ( IntanceID, StaffI

We need the sql script to query the table Ditronics.Kiosk.Journal to find journal with mismatch denom information versus amount.

CREATE TABLE #MoneyTable ( Id INT IDENTITY(1, 1) PRIMARY KEY , MoneyName VARCHAR(50) , Cents INT ) INSERT INTO #MoneyTable ( MoneyName, Cents ) VALUES ( 'UnSupported', 0 ) INSERT INTO #MoneyTable ( MoneyName, Cents ) VALUES ( 'DollarOne', 100 ) INSER

转载:SQL Server 2008-建立分区表(Table Partition) 转载

数据库结构和索引的是否合理在很大程度上影响了数据库的性能,但是随着数据库信息负载的增大,对数据库的性能也发生了很大的影响.可能我们的数据库在一开始有着很高的性能,但是随着数据存储量的急速增长—例如订单数据—数据的性能也受到了极大的影响,一个很明显的结果就是查询的反应会非常慢.在这个时候,除了你可以优化索引及查询外,你还可以做什么?建立分区表(Table Partition)可以在某些场合下提高数据库的性能,在SQL Server 2005中也可以通过SQL语句来创建表分区,但在SQL Serve

linux之SQL语句简明教程---CREATE TABLE

表格是数据库中储存资料的基本架构.在绝大部份的情况下,数据库厂商不可能知道您需要如何储存您的资料,所以通常您会需要自己在数据库中建立表格.虽然许多数据库工具可以让您在不需用到 SQL 的情况下建立表格,不过由于表格是一个最基本的架构,我们决定包括 CREATE TABLE的语法在这个网站中. 在我们跳入 CREATE TABLE 的语法之前,我们最好先对表格这个东西有些多一点的了解.表格被分为栏位 (column) 及列位 (row).每一列代表一笔资料,而每一栏代表一笔资料的一部份.举例来说,

传入sql数组字符串,输出table

CREATE function [dbo].[split](@aString varchar(8000),@pattern varchar(10)) returns @temp table([Sid] [int] IDENTITY (1, 1) NOT NULL ,a varchar(100)) --实现split功能 的函数 --说明:@aString,字符串,如a:b:c:@pattern,分隔标志,如 : as begin declare @i int set @aString=rtrim

linux之SQL语句简明教程---ALTER TABLE

在表格被建立在资料库中后,我们常常会发现,这个表格的结构需要有所改变.常见的改变如下: 加一个栏位 删去一个栏位 改变栏位名称 改变栏位的资料种类 以上列出的改变并不是所有可能的改变.ALTER TABLE 也可以被用来作其他的改变,例如改变主键定义. ALTER TABLE 的语法如下: ALTER TABLE "table_name" [改变方式]; [改变方式] 的详细写法会依我们想要达到的目标而有所不同.再以上列出的改变中,[改变方式] 如下: 加一个栏位: ADD "

doris: shell invoke .sql script for doris and passing values for parameters in sql script.

1. background in most cases, we want to execute sql script  in doris  routinely. using azkaban, to load data,etc.And we want to pass parameters to the sql script file. we can easily handle such situation in hive. 1.1 hive usage: using -hiveconf:  or

sqlserver自定义函数

标量函数 RETURNS 子句指定一种标量数据类型,则函数为标量值函数. 语法 Create function 函数名(参数) Returns 返回值数据类型 [with {Encryption | Schemabinding }] [as] begin SQL语句(必须有return 变量或值) End 释义: with 子句指出了创建函数的选项,如果指出了 encryption 参数,则创建的函数是被加密的,函数定义的文本将以不可读的形式存储在 syscomments 表中,任何人都不能查看

实现SQL Server中的切割字符串SplitString函数,返回Table

有时我们要用到批量操作时都会对字符串进行拆分,可是SQL Server中却没有自带Split函数,所以要自己来实现了. -- ============================================= -- Author: chenlong -- Create date: 2015-02-02 -- Description: 根据逗号分隔拆分字符串,返回table -- ============================================= ALTER FUN