Calling PL/pgSQL function ignoring results - postgresql

I like to call a stored PL/pgSQL function the same way as with PERFORM ignoring the results, but from plain SQL. How can i achive this? I'm currently using SELECT to execute the function, but this prints data on the console what i don't need.
I thought about disabling client output for specific SELECT statements, but i can't find any client settings for this. Maybe there's a better way to do this kind of calls.

There is no such functionality in plain sql. What you can do though, is make the function not return anything.

Here is a dirty hack, i just came up with;
background is, i need to call a function, but specify an UPDATE, not a SELECT ...maybe your background is the same...
So I specified my UPDATE like this:
UPDATE sometable_doesnt_matter
SET some_comlumn=some_comlumn
WHERE (select my_function = 1);
And my function always returns the integer 1.
Ofc i'm going to change the code so that it will also work with a SELECT, but right now, as a hotfix, this works for me.

Related

Is there another way way to replace paginate() with take()->get() where query param is present? (Laravel 9)

Usually I use paginate when I want the user to view a list (or a narrowed down list based on filters). Simple example below:
Thing::query()
->orderByDesc('created_at')
->paginate(40);
If I wanted the user to view a short list, like get the five newest models, I would create a separate api with a query like below:
Thing::query()
->orderByDesc('created_at')
->take(5)
->get();
I want to combine the two eloquent queries in such a way that it gets the paginated list by default, but will take 5 if the query param 'take=5' is present. I can do this the following way:
Thing::query()
->orderByDesc('created_at')
->when(
$request->query('take'),
fn ($query, $count) => $query->take((int)$count)->get(),
fn ($query) => $query->paginate(50)
);
The above works but has been described by a colleague as a little confusing, since the 3rd argument to when() is if the first argument is false (documentation) but that isn't immediately apparent when viewing the code. The "confusing" part might be subjective here but I would like to make sure my code is quickly understood by other devs as best as possible.
Does anyone know of a simpler/clearer or just another way to achieve this? In an ideal world the take()->get() would only exist in the when() method and paginate() would exist outside of it, but be overridden by the when() condition if true.
Note: I anticipate some people might say that they should remain as separate api's, however in my opinion the extra logic here is so simple that the gain in reduced code outweighs the gain in "do one thing well".

Why use result sets rather than variables in Sybase?

In a Sybase database I am working with result sets are used (misused?) as variables.
For example, one often finds lines such as the following:
select SOMETHING = 'bla'
"SOMETHING" is technically a result set ... and the content of the result set is used by the application accessing the database. Since "SOMETHING" is not a variable, it does not get declared anywhere.
I have never seen this kind of hack before (and colleagues of mine couldn't explain to me the reason why it was done that way) and I have not found anything about it on google.
Is there some reference available that explains why one would want to use such a hack as opposed to "normal" variables?
I think you are not reading this correctly. This query simply means that there is a one-column result set with the column named 'SOMETHING'. This query is equivalent to: SELECT 'bla' AS SOMETHING

How to automatically store all ipython outputs into variable like `Out15` rather than the list component `Out[15]`?

ipython auto-completion is great, but a lot of times I need to use previous output and the form Out[15] doesn't get auto-completion. I always have to repeatly do an assignment:
out15=Out[15]
Then use out15 for autocompletion.
Can we make this mechanism automatic?
( I know I can use _, but it lacks readability and if I re-run sth, it becomes untraceable)
There is no option to do that in IPython, and I don't see any good reason you would like to store thing in Out15 more than in a list.
A better question would have been, 'how can I get completion on dictionary and list element without using an intermediary variable'.
Like for me, Out[15].<tab> does trigger completion. I just set the completer greedy option (IPCompleter.greedy) to True, either in my profile, or using the %config magic.
Still this has side effect like :
if a attribute is in fact a property, it might get computed, which you might not want (like Sql query...etc).
It also work other dicts and lists , like pandas, also on function like 'mystring'.upper().decdoe().<tab> will work ... etc
Anyway that not what you asked, so it will probably not help you.

Create a new function from an existing function in Postgres?

I have created a function function_1() in Postgres.
Now I want to create another similar function function_2() and copy the content of function_1().
Any Postgres command line can do that?
So far I only used copy&paste method in the command line to create function_2() (need to change the function name to "function_2" after paste)
If you are using the GUI pgAdmin, you can simply select the function in the object browser and open the reverse engineered SQL script in an SQL editor window.
There is a dedicated item in the options to automate it:
Options - Query tool - Query Editor - Copy SQL from main window to query tool
pg_dump the function definition, or use the pg_get_functiondef function to get the function definition. Modifying it is up to you.
If you're trying to programmatically create functions you might be better off with PL/PgSQL's EXECUTE command and the format function to create dynamic functions.
It's pretty unusual to need to do this, suggesting you're probably doing something that's much more easily done another way.

How to execute (Materialize ) a LINQ query call to a ServiceOperation returning just a string value

Up front, the guy on the keyboard here is way overdue on the sleep department. But also kinda desperate.
I have a Data-service [WebGet] method, setup like this.
[WebGet]
public string Finalize(string PayloadObject, string CltUUID, string Comment){..}
It return a simple string, depending on the outcome of the execution inside. This is working fine.
I call it like this
var res = base.ServiceRef.CreateQuery<DBcontext>("Finalize")
.AddQueryOption("PayloadObject", string.Format("'{0}'", builder.ToString()))
...
How do I get this query to materialize?
Usually one uses res.ToList(), ToArray() or .First(). If I do, I get an exception. Using .ToString() does not execute the query, just returns the url.
The query works, if I break in a line after this code and click 'View...' in the debugger, the query is executed, the method runs on the server.
It seems I just don't get it to run, what am I missing?? besides sleep
Thanks for any pointers
Regards, Andreas
I think you cannot use CreateQuery to call operation returning single value. You must use Execute instead. MSDN documentation also mentions that operations returning primitive types cannot use QueryOption which make sense because query option is used to define query for IQueryable. If you need to pass parameters try to use common WCF REST approach to specify template in WebGet and on client side use the URI with parameters correctly included.
You can also try to use SingleResultAttribute on your service operation.