Calling Postgresql stored procedure using JDBC - postgresql

Very simple, just raw JDBC
CallableStatement call = con.prepareCall("{ call xyz() }");
call.execute();
Produces
ERROR: xyz() is a procedure
[java] Hint: To call a procedure, use CALL.
(xyz is indeed a parameterless stored procedure.)

con.createStatement().execute("xyz()");
seems to work. Just do not use CallableStatement.

Related

Cannot call methods on nvarchar(max) or varchar(max)

select
EmailAttachmentFilename,
IsNull(EmailAttachmentFilename,'').Replace('*','%') as EmailAttachmentFilename2
from dbo.MyTable1
gives the following error:
Cannot call methods on nvarchar(max).
I also added the Convert function, but that didn't solve the issue.
This is what happens when you have been doing C# or Python for a long time, then switch back to SQL. Replace is not a method in T-SQL, it is a function.
Here is the correct use of the Replace function.
select
EmailAttachmentFilename,
Replace(IsNull(EmailAttachmentFilename,''),'*','%') as EmailAttachmentFilename3
from dbo.Ch_BizTalk_IncomingEmail_Routing
from dbo.MyTable1
When I did this, I got a much more clear error:
select
EmailAttachmentFilename,
EmailAttachmentFilename.Replace('*','%') as EmailAttachmentFilename4
from dbo.MyTable1
Resulting error:
Incorrect syntax near 'Replace'.
However, when using the IsNull function, the error is rather obfuscated.

Problem calling npgsql stored function of a postgresql 10.10 database with postgis from EF Core 3.0

I have a function with a input json parameter in text format, I do the casting to json inside itself. This function returns another Json in text format. When I call this function from PGAdmin 4 using another function test, it works fine, but if I call that function from EF Core 3.0 I get this error:
Error Name: invalid input syntax for type json
Error State: 22P02
Error Context: JSON data, line 1: {"ErrorMsg":"Error Name: type "geometry...
PL/pgSQL function miguel.searchjson(text) line 87 at assignment
SQL statement "SELECT * from miguel.searchJSON(param)"
PL/pgSQL function miguel.test_searchjson() line 19 at SQL statement
With other functions without Json that returns a text, I don't have problems in the execution.
I am calling to my function that way:
var res = _context.JSResultSearch.FromSqlRaw("SELECT miguel.test_searchjson() as jsresult");
Frankly, I'm already desperate with this topic. I will be grateful with any help.
It seems like you're trying to call a function which returns a simple text, and expect EF Core to read it out for you (although this isn't clear, since you're calling FromSqlRaw on JSResultSearch, and we don't know what that is). EF Core doesn't support loading simple, non-entity values for you - see https://github.com/aspnet/EntityFrameworkCore/issues/11624.
However, EF Core wouldn't have much value here beyond simply dropping down to ADO.NET or using Dapper.

How to use PostgreSQL xmlexists() function in Hibernate HQL query?

I want to create HQL query with xmlexists() function but I get an error.
My code:
Query query = session.createQuery("From XMLTable AS tb WHERE xmlexists('//food[#id = \"1\"]' PASSING BY REF tb.xmlData)");
List list = query.list();
list.forEach(obj -> {
printSth((XMLTable) obj);
});
ERROR:
ERROR: line 1:77: unexpected token: PASSING
I also tried in pgAdmin 4 and there everything works fine. I guess that this is a syntax problem in HQL.
The problem seems to be related to the unorthodox syntax of the function you use.
An obvious solution for the problem would be to use session.createNativeQuery() instead of a JPQL query. You can then (and should) use plain SQL as the method argument.
Another solution might be to extend the dialect class Hibernate provides for PostgreSQL and register a new function implementation for xmlexists(). I am not sure if that would work though.

MS Access Call/Application.Run Procedure stored as variable not working

I have a MS Access form with a listbox with list of procedures. The procedure codes are stored in the form. There's also a button. When clicking a button, I want it to loop through the listbox and run the procedures from the selected items. I'm calling the procedures using Call and Application.Run but both methods are giving errors.
Call 'Compile Error: Expected Sub, Function, or Proeprty
Application.Run 'MS Access cannot find the procedure 'Name of Procedure'
Private Sub button_Click()
With Me.listbox1
For Each varItem In .ItemsSelected
currSub = .ItemData(varItem)
If Not IsNull(varItem) Then
Call currSub 'Compile Error: Expected Sub, Function, or Proeprty
Application.Run currSub 'MS Access cannot find the procedure 'Name of Procedure'
End If
Next
End With
Sub NameOfcurrSub1()
'some code
End Sub
Sub NameOfcurrSub2()
'some code
End Sub
etc.
If you want to call a sub using Application.Run, you need to store it in a separate, standard module (.bas, not .cls). Move your subs to a separate module, and it will work.
The Call keyword is deprecated, and doesn't call a function based on a string.
A form is a class module, its running instance is an object: Application.Run doesn't work off objects. If you want to invoke an object's methods, you need to use CallByName and specify the object instance, which would be Me... except you specifically can't pass Me directly to CallByName, so you could introduce a local variable to proxy the call:
Dim localMe As Object
Set localMe = Me
CallByName localMe, currSub, vbMethod ', arguments would go here
Or, move the procedures you want to invoke to a separate, standard module, as in Erik's answer.
Call DoSomething is 100% identical to DoSomething: the former is the deprecated explicit call syntax, the latter is the more commonly used implicit call syntax. Both are compile-time validated, which means doing Call NonExistingProcedure will throw a compile error just like invoking NonExistingProcedure would.

String passed into cursor.callproc becomes unknown (psycopg2, python 2.7, postgres 9.3)

For some reason, passing a string from Python into a Postgres function and calling it using a psycopg2 cursor causes the function to be unrecognized because the argument is unknown.
The function is very simple like so:
CREATE OR REPLACE FUNCTION text_test (some_text text)
RETURNS void AS $$
BEGIN
END;
$$ LANGUAGE plpgsql;
On the python side, I have:
cursor.callproc("text_test", ("test",))
And I get the following error:
psycopg2.ProgrammingError: function text_test(unknown) does not exist
LINE 1: SELECT * FROM text_test('test')
^
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Why does this only happen with strings and what do I need to do to have a function successfully accept a string? For some reason numeric data types are unaffected by this problem.
This happens because there is no way to cast the string to the "correct" text type. Is it a char(N)? A varchar(N)? A text?
Unfortunately .callproc() doesn't provide an easy way to specify the argument types but you can always use .execute() casting the arguments explicitly and everything works:
curs.execute("SELECT * FROM text_test(%s::text)", ("test",))
You could also make a list with the parameters you need to send:
param_list = ["test"]
curs.callproc(proc_name, param_list)
Here is a good answer about it:
python + psycopg2 = unknown types?