ISDATE() equivalent function in greenplum - greenplum

I am converting ms SQL server queries into Postgres SQL for Greenplum database, Is there any equivalent function of ISDATE() in Greenplum/postgres?

To answer your question, there is no such function in postgres or greenplum.
But you can create a customized function. See it below
create or replace function is_date(s varchar) returns boolean as $$
begin
perform s::date;
return true;
exception when others then
return false;
end;
$$ language plpgsql;
For more details, see
Check whether string is a date Postgresql

Related

RESULT_CACHE RELIES_ON (NLS_SESSION_PARAMETERS)

Why below function is not returning fresh param value every time I am altering session to set new NLS_DATE_FORMAT
FUNCTION get_param(p_parameter IN VARCHAR2)
RETURN VARCHAR2 RESULT_CACHE relies_on(nls_session_parameters) IS
l_value nls_session_parameters.value%TYPE;
BEGIN
dbg('Entered Fn_Get_nls_session_Parameter_frc to cache details for .. ' || p_parameter);
SELECT SYS_CONTEXT('USERENV', p_parameter) INTO l_value FROM dual;
RETURN l_value;
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbg('In NDF : Gng to return value as null.. ');
l_value := NULL;
RETURN l_value;
END get_param;
Well... I would say the answer stands in the question! If you carefully read Oracle documentation about Cross Session Functions, then you'd know.
The cross-session PL/SQL function result cache provides a simple way to boost the performance of PL/SQL functions by saving the results of function calls for specific combinations of input parameters in the SGA. These results can be reused by any session calling the same function with the same parameters.
This is exactly what you're using when creating your function:
FUNCTION get_param(p_parameter IN VARCHAR2)
RETURN VARCHAR2
RESULT_CACHE relies_on(nls_session_parameters)
IS
Indeed nls_session_parameters View doesn't change between your calls! it is a fixed system view. What changes it what your user sees from it.
So you have solutions:
simpler and inefficient(sorry): remove RESULT_CACHE statement from your function declaration or find a way to refresh the cache between the calls
add a parameter that will change between your calls:
FUNCTION get_param(p_parameter IN VARCHAR2, p_dummy_session_id IN NUMBER)
RETURN VARCHAR2 RESULT_CACHE relies_on(nls_session_parameters) IS
...
(you might need to actually do something with the "dummy" parameter for it to be taken into account)
1) With Oracle Database 11gR2, the RELIES ON clause is deprecated, which means that you don’t even have to list the dependencies: Oracle will figure everything out for you.
2) Moreover Oracle has V$RESULT_CACHE_OBJECTS. There are information about cached object.
3) You can also force the oracle to refresh 'result_cache'
declare
n number;
begin
n := DBMS_RESULT_CACHE.INVALIDATE (user,'GET_PARAM');
end;

Oracle equivalent of stored procedure that returns an inline table?

Example in T-SQL (SQL Server - taken from here):
CREATE PROC proc_authors
#au_lname VARCHAR(40)
AS
SELECT
au_id, au_fname, au_lname, city, state
FROM authors
WHERE au_lname = #au_lname
go
Is it possible in Oracle to create a stored procedure that returns an inline table (without declaring a type - like the above)? If not, what would be the closest alternative? i.e. declare inline type, then use it. The idea is to minimize number of DB permissions that are granted.
Please include sample code as part of your answer.
Reasoning behind using stored procedure vs function - we have legacy software that can only execute stored procedures, or raw queries. It appears that only stored procedures in there have support for parameterized execution, which is what we are after.
try this with ref cursor
PROCEDURE proc_get_tada(ip_user IN VARCHAR2,
op_error_code OUT NUMBER,
op_cursor OUT SYS_REFCURSOR,) AS
BEGIN
OPEN op_cursor FOR
SELECT * FROM your_table yt where yt.user = ip_user;
EXCEPTION
WHEN OTHERS THEN
op_error_code := -1;
END proc_get_tada;
you will get collection of all data from you table you can iterate in java or calling program.
Maybe you are searching for something like this:
create table author
(
au_id number,
au_name varchar2(100)
);
insert into author (au_id, au_name) values(1, 'ME');
create or replace function getAuthor(auName varchar2)
return author%rowtype
is
retval author%rowtype;
begin
select * into retval from author where au_name=auName;
return retval;
end;
declare
auth author%rowtype;
begin
auth := getAuthor('ME');
dbms_output.put_line(auth.au_id);
end;

How to execute an Oracle function that returns a sys_refcursor using NHibernate?

This is the function:
FUNCTION GET_ALL(P_USER_ID IN VARCHAR2) RETURN SYS_REFCURSOR IS
C SYS_REFCURSOR;
BEGIN
OPEN C
FOR 'SELECT * FROM XYZ WHERE USER_ID = :P_USER_ID'
USING P_USER_ID;
RETURN C;
END;
I'm trying to call this function using NHibernate, like this:
Session
.CreateSQLQuery("BEGIN ? = PKG.GET_ALL(:P_USER_ID); END;")
.SetString("P_USER_ID", "SOMEONE")
.List<XYZ>();
Any code, tips or smoke signs are welcome.
PS: I'm using NHibernate 3.3.0.GA
From the official docs:
For Oracle the following rules apply:
A function must return a result set. The first parameter of a
procedure must be an OUT that returns a result set. This is done by
using a SYS_REFCURSOR type in Oracle 9 or 10. In Oracle you need to
define a REF CURSOR type, see Oracle literature.
There are working tests with full mapping and stored procedure code at https://github.com/nhibernate/nhibernate-core/tree/master/src/NHibernate.Test/SqlTest/Custom/Oracle

CallableStatement.getResultSet() always return null when calling an Oracle function

I am executing the following code:
CallableStatement cs;
cs = conn.prepareCall("{ ? = call mypackage.myfunc()}");
cs.registerOutParameter(1, OracleTypes.CURSOR);
System.out.println(cs.execute());
System.out.println(cs.getResultSet());
ResultSet rs = (ResultSet) cs.getObject(1);
System.out.println(rs);
The function is declared as follows:
CREATE OR REPLACE PACKAGE BODY myuser.mypackage AS
FUNCTION myfunc
return sys_refcursor
is
a_cursor sys_refcursor;
begin
open a_cursor for select * from mytable;
return a_cursor;
end myfunc;
end mypackage;
/
According to the documentation, if there is a result then cs.execute() will return true and cs.getResultSet() will have a value. However, this is the output I get:
false
null
oracle.jdbc.driver.OracleResultSetImpl#b92a848
I am using Oracle Express 11.2.0 and the latest driver.
Any hints/explanations/things to try will be very welcome.
Thanks!
getResultSet() is typically used with PreparedStatements that return data from a SQL SELECT query, not with calls to stored procedures or functions. So I completely expect the false and null values you are seeing.
If you have a stored procedure that returns one or more ref cursors, then you fetch the values using getObject and cast them to ResultSets. In fact, your code above does exactly this, so I don't understand why you need 'things to try'.
A SQL SELECT statement has to send the selected data somewhere, but because you can't put a bind parameter or suchlike into the SQL to act as the destination for the data, you require a separate mechanism for getting access to this data via JDBC. This is what getResultSet() is for. Your function returns a ref cursor via a bind parameter, so there isn't any need for an implicit 'result', as you can access the data via the bind parameter.

calling Oracle stored procedures in R - how to get the result set?

Looking for an example for calling Oracle stored proc using R, and returning a result set.
I'm using RJDBC library, dbGetQuery to call Sybase procs and point the results to a variable, and this works the same for Oracle select stmts. However, I don't see how to get this to return Oracle result sets from an Oracle stored proc (i.e., from the sys_refcursor out param). The only examples I find for retrieving data from Oracle involve "select columns from table".
Searching in google was led me to "dbCallProc – Call an SQL stored procedure" which sounds very promising, but every ref I found to it indicates that it is "Not yet implemented."
Any pointers or examples for using procs? Greatly appreciated. Don't know why Oracle always has to be such a challenge for retrieving result sets....
Thanks,
Mike
UPDATE: I'd take an example that simply called an Oracle stored proc. Are Oracle procs simply not supported currently in RJDBC?
I can't help you specifically with R, but you say you're having issues in calling Oracle procedures that use OUT params as sys_refcursors. You also indicate this ability may not be implemented yet. You do say, however, that you can "select columns from table" just fine.
So, I propose changing the procedures to pipelined function calls, and then doing a simple select to get your data from Oracle. A small example:
CREATE OR REPLACE package pkg1 as
type t_my_rec is record
(
num my_table.num%type,
val my_table.val%type
);
type t_my_tab is table of t_my_rec;
function get_recs(i_rownum in number)
return t_my_tab
pipelined;
END pkg1;
The package body:
create or replace package body pkg1 as
function get_recs(i_rownum in number)
return t_my_tab
pipelined
IS
my_rec t_my_rec;
begin
-- get some data
-- implement same business logic as in procedure
for my_rec in (select num, val from my_table where rownum <= i_rownum)
loop
pipe row(my_rec);
end loop;
return;
end get_recs;
end pkg1;
Usage:
select * from table(pkg1.get_recs(3));
Or:
select num, val from table(pkg1.get_recs(3));
This would return 3 rows of data, just as a procedure would return the same data. Only this way you can get it from a select statement (which you seem to be able to handle from R).
Hope that helps.

Resources