PostgreSQL how to write a stored procedure to get the First and last name from a table - postgresql

I want to write a stored procedure in PostgreSQL that get input parameters and then select data based on conditions using that input parameter values. How I can achieve this easily?
I only have to use the PostgreSQL stored procedure and and not function for this

In PostgreSQL if we want to return result set in tabular format we have to use functions. they are best way designed to do this all functionality

Related

Pass table as argument to postgreSQL function?

is it possible to pass table/list/json as argument to postgreSQL function from .net core API? The requirement is like for an invoice, there are multiple line items. so we want to save invoice first and then it's respective multiple line items. for line items, we have to either call postgreSQL function one by one and insert line item to DB. so just checking if SQL bulk inset like functionality is available in PostgreSQL.
CREATE FUNCTION test(table sometable)
RETURNS TABLE(id bigint, vendor_id bigint)
BEGIN
-- bulk or single insert multiple records from argument table into postgreSQL table
END;
Thanks,
So, I came to know that json can be passed to postgresql function and there are plenty of functionality is available to extract value from json in postgresql function makes life easy.

How to use stored procedure inside the stored procedure in Postgresql

I have been working on my code for our activity in our major comp sci subject. The task asks to update a certain field in the table in postgresql using stored procedure
I have already create a gettopemp() to retrieved the data in the table, and I want to retrieve the information of gettopemp() to my new stored procedure updatetopemp(). How to use stored procedure inside the stored procedure ???
If you want to pass a function name as a parameter and call that in your code, you'll have to use dynamic SQL.

Accessing dynamic column value in PostgreSQL Trigger

I am writing trigger for record updation in Postgresql using plpgsql . In trigger, I am accessing one value column value like below
NEW.mobileno -- This is usual way.
Now my requirement is , I will be having 'mobileno' text in one variable called dyn_columnname
Using this NEW and dyn_columnname , I should be access like NEW.mobileno value.
How can I achieve this..?
You need to use dynamic sql:
execute _dyn_sql_string into _dyn_col_value using _dyn_col_name;
http://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
It's usually better to avoid ending up with this situation to begin with, that being said, because the resulting query plans cannot be cached as efficiently as the plans from non-dynamic triggers.
If you're simply iterating through columns, consider approaching this by using dynamic sql, not to write a dynamic trigger function, but to write a dynamic function that creates or replaces a non-dynamic trigger function instead.
If you're using this because of some kind of business logic, consider revisitng your schema to aboid the situation entirely.

Decide which procedure to call, depending on field value

I have about 20 (and there will be more) specific stored procedures in my PostgreSQL 9.2 DB. They are used to make some calculations, some kind of financial "reports" (unfortunately, I can't just store data in tables and implement algorithms in the programs's code).
Procedures are very different one from another, they're operating on different tables, columns, implementing different algorithms etc.
Every procedure returns the same data type (numeric value).
And now, my client wants to create the funcionality, where user can select specific procedure (or combination of them, e.g. 10% of procedure's 1 returning value + 90% of procedure's 2 returning value), and use it as a "base" for later modeling.
He wants also my user to be able to change his selection later, without calling programmers every time. ;-)
I thought about making some tables:
Table: base_models
id <PK>,
user_id, <FK from users table>
model_name (varchar, or sth.)
Table: base_models_algorithms
base_model_id <FK from base_models>
algorithm_id <FK from algorithms>
percent_value (percent value of specific algorithm in model, eg. 10)
...and then, I need to store also my algorithm names (= stored procedures) in some table:
Table: algorithms
name: (stored procedure name, varchar?)
... and that's where the problem is.
Of course I can later create some view, with a column calculated by another procedure (model_current_value :-)), and decide what procedure to call depending on name stored in my algorithm's table, but that look awful for me. :(
There would be no data control (you can write anything to algorithms' table, and there is no way to ensure that this string is a name of procedure, returning correct data type etc.).
Of course I can fill the table myself, and won't let anyone to change it's data :-)
But maybe there is more elegant way to do the whole thing?
It sounds like you want the PL/PgSQL EXECUTE statement. You can use this to invoke dynamic SQL, eg for a 2-argument procedure with dynamic name:
EXECUTE format('SELECT %I($1,$2)', func_name) USING arg1, arg2;
This is a PL/PgSQL statement. It is not available in regular SQL. You can of course create a simple PL/PgSQL procedure that does this statement and call that from SQL.

User defined function : insert into a table statement forbidden

I'd like to write a ud-function in my SQL database in order to write procedure logging in a specific table among dbo tables.
I'd like this specific function could be called by any stored procedures inside my database.
I have no idea what kind of solutions I could use. I've learned that only 3 kinds of UDF are avalaible.
Any suggestions ?
Thanks.
A UDF always has to be side-effect free. That means that you cannot change data in a table from within a function.
If you are looking to call your logger from stored procedures, why not implement it as a stored procedure too?