postgresql函数OUT和INOUT使用方法

postgresql函数IN,INOUT,OUT使用方法

前言

最近在做plsql转plpgsql,发现plpgsql的设计不太一样,搜索后发现比较直观的中文资料较少,看到一篇国外的博客,写的比较透彻,借用并记录一下,原帖地址:PL/pgSQL OUT Mode

一个OUT或者INOUT参数

以下例子为postgresql官方文档中的例子。

CREATE OR REPLACE 
  FUNCTION sales_tax( IN  amount  REAL
                    , OUT tax     REAL )
  AS $$
BEGIN
  /* Calculate the tax at 6%. */
  tax := amount * 0.06;
END;
$$ LANGUAGE plpgsql;

RETURNS子句是可选的,上面代码和下面代码是一样的。

CREATE OR REPLACE 
  FUNCTION sales_tax(</
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PL/Java is an extension for PostgreSQL that allows you to write stored procedures, functions, and triggers in Java. It provides a way to integrate Java code into your database, making it possible to perform complex calculations and data manipulations inside the database itself. To handle input and output in PL/Java, you can use the Java Database Connectivity (JDBC) API. This allows you to connect to the database, execute SQL statements, and retrieve results. Here is an example of a PL/Java function that takes an input parameter and returns a result: ```java import java.sql.*; public class MyFunctions { public static int add(int a, int b) throws SQLException { Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost/mydatabase", "myuser", "mypassword"); PreparedStatement stmt = conn.prepareStatement("SELECT ? + ?"); stmt.setInt(1, a); stmt.setInt(2, b); ResultSet rs = stmt.executeQuery(); rs.next(); int result = rs.getInt(1); rs.close(); stmt.close(); conn.close(); return result; } } ``` In this example, the `add` function takes two integer parameters (`a` and `b`) and returns their sum. It uses JDBC to connect to the database, execute a SQL statement that adds the two parameters, and retrieve the result. Finally, it closes the database resources and returns the result. To use this function in PostgreSQL, you need to install the PL/Java extension and create a Java function that calls the `add` method: ```sql CREATE FUNCTION my_add(int, int) RETURNS int AS 'MyFunctions.add' LANGUAGE java; ``` Now you can use the `my_add` function in your SQL queries: ```sql SELECT my_add(1, 2); -- returns 3 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值