语法:


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. 关于wsgi协议的理解

    基础概念 首先要了解 WSGI 规范的概念,WSGI(Web Server Gateway Interface)规范描述了web server(Gunicorn,uWSGI等)如何与web appli ...

  2. 从零开始学习html(八)CSS选择器——上

    一.什么是选择器? <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type&quo ...

  3. js 乘除法小数问题

    因为经常需要js来处理显示,就做下笔记 除法: function accDiv(arg1, arg2) { var t1 = 0, t2 = 0, r1, r2; try { t1 = arg1.to ...

  4. Salesforce Live Agent 简介

    Live Agent Salesforce 内置了即时聊天功能--Live Agent,可以用来作为客服工具. 本文简单介绍了使用 Live Agent 的步骤,主要包含: 启用 Live Agent ...

  5. android 保存图片,及将图片更新到图库

    **保存图片 public static File saveImage(Bitmap bmp) { File appDir = new File(Environment.getExternalStor ...

  6. Python tab键命令补全

    pip install pyreadline import rlcompleter, readline readline.parse_and_bind('tab: complete') root@pe ...

  7. macos 下安装brew

    1.终端执行 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master ...

  8. CENTOS7 SYSTEMD SERVICE 将自己的程序放入自动启动的系统服务

    1. 进入文件夹cd /usr/lib/systemd/system 2. 创建文件somed.service 3. 输入内容.如果是监控类程序,需要输入Type=forking[Unit]Descr ...

  9. C#检测U盘是否插入

    public partial class Form1 : Form { #region u盘属性 public const int WM_DEVICECHANGE = 0x219;//U盘插入后,OS ...

  10. PLSQL操作Oracle创建用户和表

    1.打开PLSQL,填写用户名和密码(初始有两个用户sys和system,密码是自己安装oracle数据库时定的),Database选择ORCL(默认数据库,oracle中创建的用户就像是mysql中 ...