本文将介绍PostgreSQL函数返回多个结果集的详细情况,特别是关于plsql函数多个返回怎么写的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于m
本文将介绍PostgreSQL 函数返回多个结果集的详细情况,特别是关于plsql函数多个返回怎么写的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于mysql存储过程返回多个结果集吗、node.js – node-postgres是否支持多个结果集、postgresql – plpgsql函数返回另一个函数值、postgresql – plpgsql函数返回表(..)的知识。
本文目录一览:- PostgreSQL 函数返回多个结果集(plsql函数多个返回怎么写)
- mysql存储过程返回多个结果集吗
- node.js – node-postgres是否支持多个结果集
- postgresql – plpgsql函数返回另一个函数值
- postgresql – plpgsql函数返回表(..)
PostgreSQL 函数返回多个结果集(plsql函数多个返回怎么写)
是否可以从 Postgres 函数返回多个结果集,例如在 MSSQL 中:
CREATE PROCEDURE testASSELECT * FROM first_tableSELECT * FROM second_table
答案1
自 PostgreSQL 8.3 以来,出现了一种更简单的方法:
CREATE FUNCTION test() RETURNS SETOF first_table AS$func$BEGINRETURN QUERYSELECT * FROM first_table;RETURN QUERYSELECT * FROM second_table; -- has to return same rowtype as first_table!END$func$ LANGUAGE plpgsql;
称呼:
SELECT * FROM test();
mysql存储过程返回多个结果集吗
本篇文章将介绍存储过程返回多个结果集时是什么结果,希望给大家以参考作用。
咱们先来看一个orders表它的结构:
mysql> desc orders; +----------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------+-------------+------+-----+---------+-------+ | orderNumber | int(11) | NO | PRI | NULL | | | orderDate | date | NO | | NULL | | | requiredDate | date | NO | | NULL | | | shippedDate | date | YES | | NULL | | | status | varchar(15) | NO | | NULL | | | comments | text | YES | | NULL | | | customerNumber | int(11) | NO | MUL | NULL | | +----------------+-------------+------+-----+---------+-------+ 7 rows in set
然后嘞,咱们来看一个存储过程,它接受客户编号,并返回发货(shipped),取消(canceled),解决(resolved)和争议(disputed)的订单总数(多个结果集):
DELIMITER $$ CREATE PROCEDURE get_order_by_cust( IN cust_no INT, OUT shipped INT, OUT canceled INT, OUT resolved INT, OUT disputed INT) BEGIN -- shipped SELECT count(*) INTO shipped FROM orders WHERE customerNumber = cust_no AND status = ''Shipped''; -- canceled SELECT count(*) INTO canceled FROM orders WHERE customerNumber = cust_no AND status = ''Canceled''; -- resolved SELECT count(*) INTO resolved FROM orders WHERE customerNumber = cust_no AND status = ''Resolved''; -- disputed SELECT count(*) INTO disputed FROM orders WHERE customerNumber = cust_no AND status = ''Disputed''; END
其实,除IN参数之外,存储过程还需要4个额外的OUT参数:shipped, canceled, resolved 和 disputed。 在存储过程中,使用带有count函数的select语句根据订单状态获取相应的订单总数,并将其分配给相应的参数。按着上面的sql,我们如果要使用get_order_by_cust存储过程,可以传递客户编号和四个用户定义的变量来获取输出值。执行存储过程后,我们再使用SELECT语句输出变量值:
+----------+-----------+-----------+-----------+ | @shipped | @canceled | @resolved | @disputed | +----------+-----------+-----------+-----------+ | 22 | 0 | 1 | 1 | +----------+-----------+-----------+-----------+ 1 row in set
以上就是
node.js – node-postgres是否支持多个结果集
结果对象返回一个包含7个项目的数组,这些项目与返回的数据集数量相匹配.
在Node中,7行中的每一行只包含一个<未命名的门户1>字符串.
connection.query("BEGIN"); connection.query({text: "SELECT getoperationaldatasetmodel($1)",values : [clientid]},function(err,results) { if (err) { connection.query("COMMIT"); self.pool.release(connection); callback(err); } else { var opsDataset = null; var rows = results.rows; // this returns 7 rows but the rows do not contain data but rather the name of the dataset. }
那么:node-postgres是否支持多个结果集,如果是,那么有关如何提取的任何建议?
编辑:这是我用node-postgres的代码,如果其他人需要将来使用它.
// must wrap in a transaction otherwise won't be able to see the multiple sets. connection.query("BEGIN"); connection.query({text: "SELECT myfunction($1)",results) { if (err) { // handle error here connection.query("COMMIT;"); } else { connection.query('FETCH ALL FROM "<unnamed portal 1>"',r1) { // r1.rows will contain the data for the first refcursor }); connection.query('FETCH ALL FROM "<unnamed portal 2>"',r2) { // r2.rows will contain the data for the second refcursor }); // remember to handle the closure of the transaction });
解决方法
由于node-postgres无法识别您作为结果集句柄返回的refcursors,因此它似乎不支持Postgresql的多个结果集.这很公平,因为Postgresql也不支持多个结果集,它们只是用refcursors模拟.
您可以通过sql级别的游标命令SQL-level cursor commands从refcursor进行FETCH,尽管它的文档很糟糕.您不需要使用PL/PgSQL游标处理来执行此操作.只是:
FETCH ALL FROM "<unnamed portal 1>";
请注意双引号,这很重要.从< unnamed portal 1>的函数中返回从函数返回的refcursor名称.
另请注意,除非光标是使用WITH HOLD创建的,否则创建refcursor的事务仍必须处于打开状态.当事务提交或回滚时,非HOLD游标将关闭.
例如,给定虚拟refcursor-returns函数:
CREATE OR REPLACE FUNCTION dummy_cursor_returning_fn() RETURNS SetoF refcursor AS $$ DECLARE curs1 refcursor; curs2 refcursor; BEGIN OPEN curs1 FOR SELECT generate_series(1,4); OPEN curs2 FOR SELECT generate_series(5,8); RETURN NEXT curs1; RETURN NEXT curs2; RETURN; END; $$LANGUAGE 'plpgsql';
…返回一组游标,您可以通过将门户名称传递给FETCH来获得结果,例如:
regress=# BEGIN; BEGIN regress=# SELECT dummy_cursor_returning_fn(); dummy_cursor_returning_fn --------------------------- <unnamed portal 7> <unnamed portal 8> (2 rows) regress=# FETCH ALL FROM "<unnamed portal 7>"; generate_series ----------------- 1 2 3 4 (4 rows) regress=# FETCH ALL FROM "<unnamed portal 8>"; generate_series ----------------- 5 6 7 8 (4 rows) regress=#
postgresql – plpgsql函数返回另一个函数值
我想写另一个函数func2(模式整数),它可以返回func1()reults或做更多的东西. func1()的返回值是INTEGER类型.
这样的事情:
CREATE OR REPLACE FUNCTION func2(mode integer) RETURNS integer AS $$ begin if mode=1 then return func1(); -- NOT plpgsql Syntax end if; more stuff ..... return 2; end $$ LANGUAGE plpgsql VOLATILE
我的问题是如何做返回func1(); ?
我知道我能做到:
select func1() into temp; return temp;
但是如果有一种更加优雅的方式可以做到这一点,那我就是在想.
解决方法
选项1:
CREATE OR REPLACE FUNCTION func2(mode integer) RETURNS integer AS $BODY$ DECLARE _result integer; BEGIN _result = 2; IF mode=1 THEN _result = func1(); END IF; --more stuff ..... RETURN _result; END $BODY$ LANGUAGE plpgsql VOLATILE COST 100;
选项2:
CREATE OR REPLACE FUNCTION func2(mode integer) RETURNS integer AS $BODY$ BEGIN IF mode=1 THEN RETURN func1(); END IF; --more stuff ..... RETURN 2; END $BODY$ LANGUAGE plpgsql VOLATILE COST 100;
postgresql – plpgsql函数返回表(..)
CREATE OR REPLACE FUNCTION outofdate(actualdate varchar) RETURNS TABLE(designacion varchar(255),timebeingrotten varchar(255)) AS $BODY$ SELECT designacao,actualdate - prazo FROM alimento WHERE prazo < actualdate; $BODY$ LANGUAGE 'plpgsql' volatile; SELECT * From outofdate('12/12/2012');
它不断给我错误在第2行 – 表..
ERROR: Syntax error at or near
“TABLE” LINE 2: RETURNS
TABLE(designacion
varchar(255),timebeingrotten varch…
^*** Error ***
ERROR: Syntax error at or near “TABLE”
sql state: 42601 Character: 67
CREATE OR REPLACE FUNCTION foo(a int) RETURNS TABLE(b int,c int) AS $$ BEGIN RETURN QUERY SELECT i,i+1 FROM generate_series(1,a) g(i); END; $$LANGUAGE plpgsql;
呼叫:
SELECT * FROM foo(10);
今天关于PostgreSQL 函数返回多个结果集和plsql函数多个返回怎么写的介绍到此结束,谢谢您的阅读,有关mysql存储过程返回多个结果集吗、node.js – node-postgres是否支持多个结果集、postgresql – plpgsql函数返回另一个函数值、postgresql – plpgsql函数返回表(..)等更多相关知识的信息可以在本站进行查询。
本文标签: