PostgreSQL Function

创建Function

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

参数说明

  1. function_name:Functions名称

  2. [OR REPLACE]:更改当前Functions,可以使用OR REPLACE关键字。它是一个可选参数。

  3. arguments:参数列表。一个Functions可以包含零个或多个参数。

  4. return_datatype:Function的返回值类型,可以是表列类型的基本类型、复合类型或域类型或引用。 

  5. function_body:逻辑的可执行部分

示例

Create function get_car_Price(Price_from int, Price_to int)  
returns int  
language plpgsql  
as  
$$  
Declare  
 Car_count integer;  
Begin  
   select count(*)   
   into Car_count  
   from Car  
   where Car_price between Price_from and Price_to;  
   return Car_count;  
End;  
$$;

说明

SELECT INTO命令将输出提供给Car_count

调用Function

  1. Select get_car_Price(26000,70000);
  2. select get_car_Price(Price_from => 26000,   Price_to => 70000);
  3. select get_car_Price(Price_from := 26000,   Price_to := 70000); 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值