How to loop json attribute in PostgreSQL - json

CREATE OR REPLACE FUNCTION parse_json (arg_t text)
RETURNS setof json
AS $$
DECLARE json_object json;
DECLARE i json;
BEGIN
SELECT (arg_t)::json into json_object;
FOR item IN SELECT * FROM json_array_elements((json_object->>'arg_t')::json)
LOOP
RAISE NOTICE ' % ', json_object->>'id';
END LOOP;
END;
$$ LANGUAGE 'plpgsql';
Function call:
select parse_json('{"id": 1,"name": "Amit","Class": "12th"}');
I want to loop ID. And run the function for as many as ID we pass as argument

Related

Problem with the output of a function with postgresql

I have a table named 'reserves' and the exercise asks me to make a function that counts how many reservations are with an id lower than the id I give as the only parameter of the function. They also ask me that when I introduce 'NULL' as a parameter, the function must make a sum of all of the imports from the column 'import' and show the total. I have half of the function working. The 'NULL' part is the one not working.
Now I have this:
CREATE OR REPLACE FUNCTION compta_reserves(int) RETURNS int AS $$
DECLARE
num_reserves INT:=(SELECT COUNT(*) FROM mobilitat.reserves);
id ALIAS FOR $1;
comptador INT:=0;
i INT:=1;
import_total double precision:=((SELECT SUM(import) FROM mobilitat.reserves WHERE import<60)::INT);
BEGIN
IF (id=NULL) THEN
RETURN import_total AS int;
ELSE
WHILE i<num_reserves LOOP
IF (SELECT id_reserva FROM mobilitat.reserves WHERE id_reserva=i)<id THEN
IF (SELECT import FROM mobilitat.reserves WHERE id_reserva=i)<60 THEN
comptador=comptador+1;
END IF;
END IF;
i=i+1;
END LOOP;
RETURN comptador;
END IF;
END;
$$ LANGUAGE PLPGSQL;
When I run the function with the 'NULL' parameter, I just get an empty table with no sum.

How to call an update table that is inside in the procedure for a function in PLSQL?

This is my situation:
I have a procedure that do an update table like this:
PROCEDURE UPDATE_MODEL AS
STR VARCHAR2(10000);
BEGIN
STR:='UPDATE SAE_MODEL_TABLE.....'
EXECUTE IMMEDIATE STR;
I need to do a function that call this UPDATE SAE_MODEL_TABLE...
I tried with this code
FUNCTION GET_MODEL_TABLE RETURN CLOB AS
STR4 VARCHAR2(50);
count_table number;
BEGIN
str4:='select count(1) from SAE_MODEL_TABLE;
execute immediate str4 into count_table;
IF count_table <> 0 then
UPDATE_MODEL ;
I have this error:
ORA-14551: DML operation can not be performed within a query,
I know that refers for add an update...
I do not know why you use dynamic SQL, but if you really want to do it, it might look like this
PROCEDURE UPDATE_MODEL AS
STR VARCHAR2(10000);
BEGIN
STR:='UPDATE SAE_MODEL_TABLE.....'
EXECUTE IMMEDIATE STR;
end;
FUNCTION GET_MODEL_TABLE RETURN CLOB AS
STR4 VARCHAR2(50);
count_table number;
my_clob clob;
BEGIN
str4:='select count(1) from SAE_MODEL_TABLE';
execute immediate str4 into count_table;
IF count_table <> 0 then
UPDATE_MODEL() ;
end if;
return my_clob;
end;

Perform OR/AND operation on Bits argument

I am tring to perform a BITOR on a argument that i am passing into a function. so i wrote the below code. Statement 'SET result = arg1 | arg2 ' isnt working. I tried few ways, however i wasnt able to achieve the right result. What do i need to change ?
DELIMITER $$
CREATE FUNCTION BIT_OR(arg1 varchar(255),arg2 varchar(255)) RETURNS varchar(255)
BEGIN
DECLARE result varchar(255);
BEGIN
SET result = arg1 | arg2;
END;
RETURN result;
END $$
DELIMITER ;
select bitwise_OR(00011101,00001111); -- 12127 ( i am expecting 00011111 or 31 (decimal equivalent))

how to use postgresql function in another function which returns set of Refcursor

My Function as Follows
CREATE OR REPLACE FUNCTION get_history(refcursor, encounterid integer, patientid integer)
RETURNS SETOF refcursor
Begin
end
How to use above function in another function.
Why would you want to return SETOF refcursor?
Maybe you want
RETURNS TABLE( ...)
or
RETURNS SETOF some_composite_type
You call that like any other SELECT command ..
SELECT * FROM get_history(...)
And can use it in a plpgsql LOOP:
FOR my_row_var IN
SELECT * FROM get_history(...)
LOOP
-- do stuff
END LOOP;
Or just
RETURNS refcursor
There is a detailed example how to handle this in the manual here.
Even including an example for RETURNS SETOF refcursor.

Create a function with optional arguments in MySQL

I want to create a function with optional arguments in MySQL. For instance, I want to create function that calculates the average of its arguments. I create a function of five arguments, but when user passes just two arguments to the function then it should still run and return the average of the two arguments.
You cannot set optional parameters in MySQL stored procedures.
You can however set optional parameters in a MySQL UDF.
You do know that MySQL has an AVG aggregate function?
Workaround
If you can face the ugliness of this workaround here's samplecode that uses a comma separated string with values as input and returns the average.
DELIMITER $$
CREATE FUNCTION MyAvg(valuestr varchar) RETURNS float
BEGIN
DECLARE output float;
DECLARE arg_count integer;
DECLARE str_length integer;
DECLARE arg float;
DECLARE i integer;
SET output = NULL;
SET i = LENGTH(valuestr);
IF i > 0 THEN BEGIN
SET arg_count = 1;
WHILE i > 0 DO BEGIN
IF MID(valuestr, i, 1)
SET i = i - 1;
END; END WHILE;
/* calculate average */
SET output = 0;
SET i = arg_count;
WHILE i > 0 DO BEGIN
SET arg = SUBSTRING_INDEX(
SUBSTRING_INDEX(valuestr, ',' , i)
, ',', -1 );
SET output = output + arg;
SET i = i - 1;
END; END WHILE;
SET output = output / arg_count;
END; END IF;
RETURN output;
END $$
DELIMITER ;
Use concat_ws to feed the function.
SELECT MyAvg(CONCAT_WS(',',100,200,300,500)) AS test;
You can also write an UDF in C(++) or Delphi/Lazarus
While far from an ideal solution, here's how I solved optional parameters for a concat function I needed:
delimiter ||
create function safeConcat2(arg1 longtext, arg2 varchar(1023))
returns longtext
return safeConcat3(arg1, arg2, '');
||
create function safeConcat3(arg1 longtext, arg2 varchar(1023), arg3 varchar(1023))
returns longtext
return safeConcat4(arg1, arg2, arg3, '');
||
create function safeConcat4(arg1 longtext, arg2 varchar(1023), arg3 varchar(1023), arg4 varchar(1023))
returns longtext
begin
declare result longText;
set result = concat(arg1, arg2, arg3, arg4);
if( result is null) then
set result=arg1;
end if;
return result;
end
||
Note: This means you have to call the method that corresponds to the number of args.
Another approach is to pass only one 'super' parameter which is string with commas in it separating the real parameters. The mysql procedure can then parse the 'super' parameter into the separate real parameters.
Example:
create procedure procWithOneSuperParam(param1 varchar(500))
declare param2 varchar(100);
begin
if LOCATE(',',param1) > 0 then
.. param2=<extract the string after the ',' from param1> ..