Can Postgres 13 stored procedures return multiple result sets similar to SQL Server? - postgresql

I tried to search for it in the official docs but no luck.
Can Postgres 13 stored procedures return multiple result sets similar to SQL Server?

If you mean to return a result set by executing a plain query - no, such syntax is not supported in PL/pgSQL.
If you mean to return a result set by using a cursor - yes, PostgreSQL stored procedures can return cursors too.
You can define multiple cursor output parameters: https://www.postgresql.org/docs/13/plpgsql-cursors.html

Related

How to return a dynamic data type from a procedure using PostgreSQL 11?

I am looking for the dynamic result set to be return from procedure as we have in SQL Server.
In the earlier version of Postgres there was not procedure till version 10, but now in 11 we do have procedures.
I tried with functions in the postgresql but we need to provide the return type for the functions. But I am looking for the result which is dynamic, I mean to say sometimes the results comes from different table with different columns list based on the conditions check.
As I saw some of the examples of procedures from EDB, Unable to find an example for retrieving data.

Convert Oracle to T-SQL

Сan you help me to convert this Oracle rule to T-SQL.
SELECT CAST(SYS_CONTEXT('CLIENTCONTEXT', 'AccessSubject') AS NVARCHAR2(255)) AS AccessSubjectCode FROM DUAL
I would like to know how this will be in T-SQL : SYS_CONTEXT()
The SYS_CONTEXT('CLIENTCONTEXT', 'AccessSubject') call returns a value configured by the client application. See https://docs.oracle.com/cd/B28359_01/network.111/b28531/app_context.htm#DBSEG98209 for details.
The most similar feature in SQL Server is the CONTEXT_INFO() function. See https://msdn.microsoft.com/en-us/library/ms180125.aspx. However, in SQL Server the context can store just a single value, of maximum 128 bytes (as opposed to Oracle, where there are multiple contexts and you can store multiple named values in each context).

How can I read PostgreSQL arrays with SOCI?

I need to execute some query that returns (SELECTs) a PostgreSQL array, but the documentation of SOCI's PostgreSQL backend doesn't mention anything about arrays.
If I just try to put it into a soci::rowset, it thinks that it is a string column and returns a string like "{1, 2, 3}" which I would hate to parse. Is there any way to get SOCI to handle that data type automatically, either with soci::into or via a soci::rowset? Or do I have to resort to JOINing with the array in order to get separate rows in the resultset?
I'm using SOCI 3.2 and PostgreSQL 9.3.

JPA: How to call a stored procedure

I have a stored procedure in my project under sql/my_prod.sql
there I have my function delete_entity
In my entity
#NamedNativeQuery(name = "delete_entity_prod",
query = "{call /sql/delete_entity(:lineId)}",
and I call it
Query query = entityManager.createNamedQuery("delete_entity_prod")
setParameter("lineId",lineId);
I followed this example: http://objectopia.com/2009/06/26/calling-stored-procedures-in-jpa/
but it does not execute the delete and it does not send any error.
I haven't found clear information about this, am I missing something? Maybe I need to load the my_prod.sql first? But how?
JPA 2.1 standardized stored procedure support if you are able to use it, with examples here http://en.wikibooks.org/wiki/Java_Persistence/Advanced_Topics#Stored_Procedures
This is actually they way you create a query.
Query query = entityManager.createNamedQuery("delete_entity_prod")
setParameter("lineId",lineId);
To call it you must execute:
query.executeUpdate();
Of course, the DB must already contain the procedure. So if you have it defined in your SQL file, have a look at Executing SQL Statements from a Text File(this is for MySQL but other database systems use a similar approach to execute scripts)
There is no error shown because query is not executed at any point - just instance of Query is created. Query can be executed by calling executeUpdate:
query.executeUpdate();
Then next problem will arise: Writing some stored procedures to file is not enough - procedures live in database, not in files. So next thing to do is to check that there is correct script to create stored procedure in hands (maybe that is currently content of sql/my_prod.sql) and then use that to create procedure via database client.
All JPA implementations do not support calling stored procedures, but I assume Hibernate is used under the hood, because that is also used in linked tutorial.
It can be the case that current
{call /sql/delete_entity(:lineId)}
is right syntax for calling stored procedure in your database. It looks rather suspicious because of /sql/. If it turns out that this is incorrect syntax, then:
Consult manual for correct syntax
Test via client
Use that as a value of query attribute in NamedNativeQuery annotation.
All that with combination MySQL+Hibernate is explained for example here.

using WITH clause for PostgreSQL?

I was planning to use the WITH clause with PostgreSQL, but it doesn't seem to support the command. Is there a substitute command?
What I want to do is with one query select several sub-resultsets and use parts of the sub-resultsets to create my final SELECT.
That would have been easy using the WITH clause.
UPDATE:
Opps! I discovered that I misunderstood the error message I got; and pgSQL does support WITH.
PostgreSQL supports common-table expressions (WITH queries) in version 8.4 and above. See common table expressions in the manual.
You should really include your PostgreSQL version, the exact text of the error message, and the exact text of any query you ran in your question. Where practical/relevant also include table definitions, sample data, and expected results.