语法:


CREATE [OR REPLACE] FUNCTION function_name (arguments)
RETURNS return_datatype AS $variable_name$
DECLARE
declaration;
[...]
BEGIN
< function_body >
[...]
RETURN { variable_name | value }
END; LANGUAGE plpgsql;

参数说明

  • function_name:指定函数的名称。
  • [OR REPLACE]:是可选的,它允许您修改/替换现有函数。
  • RETURN:它指定要从函数返回的数据类型。它可以是基础,复合或域类型,或者也可以引用表列的类型。
  • function_bodyfunction_body包含可执行部分。
  • plpgsql:它指定实现该函数的语言的名称
#时间函数
CREATE OR REPLACE FUNCTION "public"."addtime"(difftype varchar, incrementvalue int4, inputdate timestamptz)
RETURNS "pg_catalog"."timestamp" AS $BODY$
DECLARE
YEAR_CONST Char(15) := 'year';
MONTH_CONST Char(15) := 'month';
WEEK_CONST Char(15) := 'week';
DAY_CONST Char(15) := 'day';
HOUR_CONST Char(15) := 'hour';
MIN_CONST Char(15) :='mi';
SECOND_CONST Char(15) :='ss';
dateTemp timestamp;
intervals interval;
BEGIN
IF lower($1) = lower(YEAR_CONST) THEN
select cast(cast(incrementvalue as character varying) || ' year' as interval) into intervals;
ELSEIF lower($1) = lower(MONTH_CONST) THEN
select cast(cast(incrementvalue as character varying) || ' months' as interval) into intervals;
ELSEIF lower($1) = lower(WEEK_CONST) THEN
select cast(cast(incrementvalue as character varying) || ' week' as interval) into intervals;
ELSEIF lower($1) = lower(DAY_CONST) THEN
select cast(cast(incrementvalue as character varying) || ' day' as interval) into intervals;
ELSEIF lower($1) = lower(HOUR_CONST) THEN
select cast(cast(incrementvalue as character varying) || ' hour' as interval) into intervals;
ELSEIF lower($1) = lower(MIN_CONST) THEN
select cast(cast(incrementvalue as character varying) || ' minute' as interval) into intervals;
ELSEIF lower($1) = lower(SECOND_CONST) THEN
select cast(cast(incrementvalue as character varying) || ' second' as interval) into intervals;
END IF; dateTemp:= inputdate + intervals; RETURN dateTemp;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE COST 100
;
CREATE OR REPLACE FUNCTION "public"."end_purchase_price"(refcursor)
RETURNS "pg_catalog"."refcursor" AS $BODY$
begin
open $1 for
SELECT p1.name_template,p2.material,p2.cust_spec,p0.price_unit::double precision,p3.name
FROM purchase_origin_line p0
LEFT JOIN product_product p1 ON p0.product_id=p1.id
LEFT JOIN product_template p2 ON p1.product_tmpl_id=p2.id
LEFT JOIN product_uom p3 ON p2.uom_id=p3.id
WHERE p2.active ='t' AND p0.id IN ( SELECT MAX(id)
FROM purchase_origin_line
where state<>'cancel' AND product_id >0
GROUP BY product_id
);
return $1;
end;
$BODY$
LANGUAGE 'plpgsql' VOLATILE COST 100
;
CREATE OR REPLACE FUNCTION "public"."prod_line_zd"( refcursor, gsid int4, sd varchar, ed varchar, gxstateid int4, workname varchar, lotname varchar, ename varchar, pname varchar, pgg varchar, gxname varchar)
RETURNS "pg_catalog"."refcursor" AS $BODY$
BEGIN
if gxstateid=3 then
open $1 for EXECUTE'
select t2.sequence,to_char(timeadd(''hour'',8,t1.date),''yyyy-mm-dd''),t3.name_template,t4.material,t4.cust_spec,t0.name workname,t5.name lotname,t2.lqty_ok,t2.lkgweight,t2.lqty,t2.lqty_no,t7.login,t8.name,t6.sequence,t6.name,t6.produce_description,case t2.lstate when 1 then ''领'' else ''交'' end,t1.name,t9.name_template,t9.cust_spec,t2.lremark from mrp_production t0
left join mrp_production_report t1 on t0.id=t1.workname
left join mrp_production_report_line t2 on t1.id=t2.line_id and t0.id=t2.lworkname
left join product_product t3 on t0.product_id=t3.id
left join product_template t4 on t3.product_tmpl_id=t4.id
left join mrp_production_report_lot t5 on t5.id=t2.lot_id
left join mrp_routing_workcenter t6 on t6.id=t2.lprocedure
left join res_users t7 on t7.id=t2.lemployee
left join res_partner t8 on t8.id=t7.partner_id
left join (select t0.id,t2.name_template,t3.material,t3.cust_spec from mrp_bom t0 left join mrp_bom_line t1 on t0.id=t1.bom_id
left join product_product t2 on t1.product_id=t2.id
left join product_template t3 on t3.id=t2.product_tmpl_id)t9 on t9.id=t0.bom_id
where t0.company_id='|| gsid ||' and timeadd(''hour'',8,t1.date)>='''|| sd ||''' and timeadd(''hour'',8,t1.date)<='''|| ed ||''' and exists(select 1 from mrp_production_report where workname=t0.id) and t0.state<>''cancel''
and t0.name like ''%'|| workname ||'%'' and t5.name like ''%'|| lotname ||'%'' and t8.name like ''%'|| ename ||'%'' and t3.name_template like ''%'|| pname ||'%'' and t4.cust_spec like ''%'|| pgg ||'%''and t6.name like ''%'|| gxname ||'%'' order by t1.date,t1.name,t2.sequence;';
ELSE
open $1 for EXECUTE'
select t2.sequence,to_char(timeadd(''hour'',8,t1.date),''yyyy-mm-dd''),t3.name_template,t4.material,t4.cust_spec,t0.name workname,t5.name lotname,t2.lqty_ok,t2.lkgweight,t2.lqty,t2.lqty_no,t7.login,t8.name,t6.sequence,t6.name,t6.produce_description,case t2.lstate when 1 then ''领'' else ''交'' end,t1.name,t9.name_template,t9.cust_spec,t2.lremark from mrp_production t0
left join mrp_production_report t1 on t0.id=t1.workname
left join mrp_production_report_line t2 on t1.id=t2.line_id and t0.id=t2.lworkname
left join product_product t3 on t0.product_id=t3.id
left join product_template t4 on t3.product_tmpl_id=t4.id
left join mrp_production_report_lot t5 on t5.id=t2.lot_id
left join mrp_routing_workcenter t6 on t6.id=t2.lprocedure
left join res_users t7 on t7.id=t2.lemployee
left join res_partner t8 on t8.id=t7.partner_id
left join (select t0.id,t2.name_template,t3.material,t3.cust_spec from mrp_bom t0 left join mrp_bom_line t1 on t0.id=t1.bom_id
left join product_product t2 on t1.product_id=t2.id
left join product_template t3 on t3.id=t2.product_tmpl_id)t9 on t9.id=t0.bom_id
where t0.company_id='|| gsid ||' and timeadd(''hour'',8,t1.date)>='''|| sd ||''' and timeadd(''hour'',8,t1.date)<='''|| ed ||''' and t2.lstate='|| gxstateid ||' and exists(select 1 from mrp_production_report where workname=t0.id) and t0.state<>''cancel''
and t0.name like ''%'|| workname ||'%'' and t5.name like ''%'|| lotname ||'%'' and t8.name like ''%'|| ename ||'%'' and t3.name_template like ''%'|| pname ||'%'' and t4.cust_spec like ''%'|| pgg ||'%''and t6.name like ''%'|| gxname ||'%'' order by t1.date,t1.name,t2.sequence;';
end if;
return $1;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE COST 100
;

sql函数创建的更多相关文章

  1. SQL函数创建错误

    [Err] 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration a ...

  2. SQL server 创建 修改表格 及表格基本增删改查 及 高级查询 及 (数学、字符串、日期时间)函数[转]

    SQL server 创建 修改表格 及表格基本增删改查 及 高级查询 及 (数学.字符串.日期时间)函数   --创建表格 create table aa ( UserName varchar(50 ...

  3. SQL语句-创建索引

    语法:CREATE [索引类型] INDEX 索引名称ON 表名(列名)WITH FILLFACTOR = 填充因子值0~100 GO USE 库名GO IF EXISTS (SELECT * FRO ...

  4. SQL函数说明大全

    一旦成功地从表中检索出数据,就需要进一步操纵这些数据,以获得有用或有意义的结果.这些要求包括:执行计算与数学运算.转换数据.解析数值.组合值和聚合一个范围内的值等. 下表给出了T-SQL函数的类别和描 ...

  5. 使用sql语句创建修改SQL Server标识列(即自动增长列)

    一.标识列的定义以及特点SQL Server中的标识列又称标识符列,习惯上又叫自增列.该种列具有以下三种特点:1.列的数据类型为不带小数的数值类型2.在进行插入(Insert)操作时,该列的值是由系统 ...

  6. oracle的sql函数

    只读事务set transaction read only当一个用户添加了只读事务,则查询时只会查到设置只读事务之前的内容,在并发量大的系统中,通过设置只读事务 便于统计 oracle的sql函数的使 ...

  7. SQL函数说明大全 (转)

    一旦成功地从表中检索出数据,就需要进一步操纵这些数据,以获得有用或有意义的结果.这些要求包括:执行计算与数学运算.转换数据.解析数值.组合值和聚合一个范围内的值等. 下表给出了T-SQL函数的类别和描 ...

  8. 在SQL中使用PL/SQL函数存在的问题

    -----------------------------Cryking原创------------------------------ -----------------------转载请注明出处, ...

  9. sql函数(转)

    一旦成功地从表中检索出数据,就需要进一步操纵这些数据,以获得有用或有意义的结果.这些要求包括:执行计算与数学运算.转换数据.解析数值.组合值和聚合一个范围内的值等. 下表给出了T-SQL函数的类别和描 ...

随机推荐

  1. 洛谷P2572 [SCOI2010]序列操作(ODT)

    题解 题意 题目链接 Sol ODT板子题..... // luogu-judger-enable-o2 #include<bits/stdc++.h> #define LL long l ...

  2. 【读书笔记】iOS-“一心多用”利用多线程提升性能

    iPhone将具有支持不同类型多线程API的能力,这些API包括:POSIX线程,NSObject,NSThread和NSOperation. iPhone操作系统是一个真正的抢占式,多任务操作系统, ...

  3. loadrunner Vugen-Tools General-Options-Replay设置

    Vugen-Tools General-Options-Replay设置 by:授客 QQ:1033553122 可以以动画模式或非动画模式(animated mode or non-animated ...

  4. android recovery 升级UI显示之资源文件

    Recovery只有在升级的时候才会呈现给用户,所以界面一般都很简单,没有android上层那么绚丽,所以recovery下面对图片的支持很有限,仅支持png图片显示,所以我们可以看到,recover ...

  5. LeetCode题解之 Subtree of Another Tree

    1.题目描述 2.问题分析 判断一个节点,然后判断子树. 3.代码 bool isSubtree(TreeNode* s, TreeNode* t) { if (s == NULL) return f ...

  6. sql server 转置 和实现随机分配和一串代码的含义拼在一行

    1.sql server 转置很容易搜到方法,一般需要手动写转置的列项,如果多时会比较烦,下面试了省事的方法: --案例需求数据 ----方法一:if object_id('tempdb.dbo.#s ...

  7. [MapReduce_add_5] MapReduce 实现标签的生成与聚合

    0. 说明 MapReduce 实现标签的生成与聚合 介绍 && 流程图 && 程序编写 1. 介绍 [1.1 原始有效数据] 86913510 {"revi ...

  8. Head First Android --- Intent

    How to create the intentYou create an intent that specifies an action using the following syntax:whe ...

  9. Hadoop第一阶段总结

    来自为知笔记(Wiz)

  10. GsonUtil工具类

    import java.util.ArrayList; import java.util.List; import java.util.Map; import com.google.gson.Gson ...