I am trying to create a function like this:
I tried changing the return type to int or text etc. to see if the code works outside of that, but it doesn't. I am a beginner in PostgreSQL so please don't be harsh if I missed something obvious.
create or replace function date_select(i INT) returns void as
$$
begin
select * from dwh_stg.stg_dce_gift where gift_id = i;
end
$$ language plpgsql
select date_select(16940)
SQL Error [42601]:
ERROR: query has no destination for result data
Hint: If you want to discard the results of a SELECT, use PERFORM instead.
Where: PL/pgSQL function date_select(integer) line 3 at SQL statement
If you want to return something, you need to define the function to return something (not void)
Apparently you want to return multiple rows from the table stg_dec_gift, for that you need to define the function as returns setof dwh_stg.stg_dce_gift. For a simple function encapsulating a query there is not need to use PL/pgSQL a plain SQL function will do just fine:
create or replace function date_select(i INT)
returns setof dwh_stg.stg_dce_gift --<< here
as
$$
select *
from dwh_stg.stg_dce_gift
where gift_id = i;
$$
stable
language sql;
Then use it in the FROM part:
select *
from date_select(16940);
Online example: https://rextester.com/WYDCE44062
Related
I have problem with return table using SQL Procedure.
My code:
CREATE PROCEDURE return_data(surname character varying)
LANGUAGE SQL
AS $$
SELECT * FROM peopple WHERE surname=surname
$$;
CALL return_data('Jobs');
Currently the procedure executes without error but it doesn't return a table.
Procedures aren't meant to return anything. Use a set returning function
create function return_data(p_surname varchar)
returns setof people
as
$$
select *
from people
where surname = p_surname;
$$
language sql;
You should also avoid parameter or variable names that have the same name as a column.
Then use it like this:
select *
from return_data('Jobs');
You can append one or more rows to the result set:
CREATE OR REPLACE FUNCTION return_data(surname character varying)
RETURNS TABLE(firstname character varying, surname character varying, age integer)
LANGUAGE plpgsql
AS $function$
BEGIN
RETURN QUERY
SELECT firstname, surname, age
FROM users;
END;
$function$
;
Invoke the function:
SELECT * FROM return_data("a surname");
A function can return but a procedure cannot. Thats true, I wanna add to that we generally use function to return values of one type (in general one value)
While,
Procedure never returns but can behave like a function without a return statement using OUT parameters. To add to this we use procedure when we dont wanna return in actual but executing sql statements but in case if we require a procedure to give multiple values we can make procedure behave like a function for multole values output vja OUT.
As a table can be accessed by record or array type depending upon homogenity. Therefore, use function with return type as record/array.
Hence, as function is solely used to return single output we have to specify via return.
I am trying to migrate the following MS SQL Server procedure to a PostgreSQL function.
CREATE PROCEDURE [dbo].[GMC]
AS
BEGIN
DECLARE #LID VARCHAR(3);
DECLARE #xml XML = '<XMLProf><CID>840</CID><MD>101113</MD></XMLProf>';
SELECT #LID = Pay.b.value('.','Varchar(3)')
FROM #xml.nodes('/XMLProf/CID') as Pay(b)
SELECT 'Return Value' = #LID
END
I have tried to convert to the following but it doesn't work.
CREATE OR REPLACE FUNCTION dbo.GMC()
RETURNS void
AS
$BODY$
DECLARE
LID VARCHAR(3);
bxml XML = '<XMLProf><CID>840</CID><MD>101113</MD></XMLProf>';
BEGIN
SELECT LID = Pay.b.value('.','Varchar(3)')
FROM XMLTABLE('/XMLProf/CID' PASSING bxml) as Pay(b)
SELECT 'Return Value' = LID
end;
$BODY$
LANGUAGE plpgsql;
Edit:
The result I am expecting is "840"
The error that I am getting is a syntax error:
ERROR: syntax error at or near ")"
LINE 12: FROM XMLTABLE('/XMLProf/CID' PASSING bxml) as Pay(b)
Can someone please tell me how can I accomplish this. Any help is really appreciated.
If you want to return something from a function, you can't use returns void. As XML is character data, returns text makes more sense.
As you only want to return a single value, xmltable() isn't really needed. And you don't need PL/pgSQL either:
CREATE OR REPLACE FUNCTION dbo.gmc()
RETURNS text
AS
$BODY$
select (xpath('/XMLProf/CID/text()',
'<XMLProf><CID>840</CID><MD>101113</MD></XMLProf>'::xml))[1]::text;
$BODY$
LANGUAGE sql;
xpath() returns an array of all matches, that's why the [1] is needed to pick out the first match.
Assuming you actually want to pass the XML to the function, you can use this:
CREATE OR REPLACE FUNCTION dbo.gmc(p_xml xml)
RETURNS text
AS
$BODY$
select (xpath('/XMLProf/CID/text()', p_xml))[1]::text;
$BODY$
LANGUAGE sql;
If you're basing your response element on the string size of the inner nodes of XMLProf, you might wanna take a look at XPATH and UNNEST.
CREATE OR REPLACE FUNCTION gmc() RETURNS text
AS $BODY$
WITH j AS (
SELECT
UNNEST(XPATH('//XMLProf/node()',
'<XMLProf><CID>840</CID><MD>101113</MD></XMLProf>'::XML)) AS rawxml
) SELECT (XPATH('//text()',j.rawxml))[1]::TEXT FROM j
WHERE CHAR_LENGTH((XPATH('//text()',j.rawxml))[1]::TEXT) = 3
$BODY$
LANGUAGE sql;
Testing ..
db=# SELECT * FROM gmc();
gmc
-----
840
(1 Zeile)
If you know exactly where to look and the string length is irrelevant, just get rid of the UNNEST and use the XPATH /XMLProf/CID/text() as pointed out by #a_horse_with_no_name.
This question already has an answer here:
SELECT raises exception in PL/pgSQL function
(1 answer)
Closed 6 years ago.
I have a simple user defined function as following:
create or replace function test()
returns table (a float, b float) as
$$
begin
drop table if exists test1;
create table test1(a float, b float);
insert into test1 values(1,1),(2,1),(3,3);
return query select * from test1;
end;
$$ language plpgsql;
I have another user defined function. In this one, I want to call the function above.
create or replace function test2()
returns table (a float) as
$$
begin
select test();
return query select a+b from test1;
end;
$$language plpgsql;
after this, when I do:
select test2();
It gives me an error:
query has no destination for result data.
If I make my second function as the following:
create or replace function test2()
returns table (a float) as
$$
select a+b from test();
$$language sql;
Then I do select test2();
It runs correctly. I think the reason why this doesn't work is when I return something from sql function, it returns a table, but when I return something from a plpgsql function, it returns something look like a tuple. For example (1,2). 1 will be the first attribute of the table, 2 will be the second one. This is just a simple example, I can use sql instead of plpgsql to solve this problem. However, in my real project, I really want to call a plpgsql function in another plpgsql function.
Then main structure of my program will look like the following:
plpgsql function 1
plpgsql function 2
plpgsql function 3
In my main plpgsql function, I want to call all these three functions. Does anyone have a idea of how to do that?
Like the error message says:
query has no destination for result data.
Meaning, you cannot execute a SELECT in a plpgsql function without destination for the result.
Use PERFORM instead of SELECT if you want to discard the result.
Related:
SELECT raises exception in PL/pgSQL function
I've got a function to return relevant position of an array matching given value like below:
CREATE OR REPLACE FUNCTION get_index(needle ANYELEMENT, haystack ANYARRAY)
RETURNS TABLE (i_position int)
AS $$
SELECT
i+i_step AS i_position
FROM (VALUES(1),(2)) steps(i_step),
generate_series(array_lower($2,1), array_upper($2,1)) AS i
WHERE $2[i] = $1
$$ LANGUAGE SQL STABLE;
Instead of passing a single value to the function, I want to pass a table name, as one column of the table would be used to do the value comparison (WHERE $2[i] = $1 ), instead of a single value passed to the function. However, it doesn't seem like the function support SQL using argument as table name.
I'm wondering if there's alternative. I'd like to use SQL function instead of PLPGSQL, for the sake of performance. As our table is huge.
I'd like to achieve something like below:
CREATE OR REPLACE FUNCTION get_index(tbl ANYELEMENT, haystack ANYARRAY)
RETURNS TABLE (i_position int)
AS $$
SELECT
i+i_step AS i_position
FROM (VALUES(1),(2)) steps(i_step),
generate_series(array_lower($2,1), array_upper($2,1)) AS i,
$1
WHERE $2[i] = $1.col1
$$ LANGUAGE SQL STABLE;
It is not possible in SQL function - It doesn't support dynamic sql. You have to use PLpgSQL - EXECUTE statement.
SQL function is faster than PLpgSQL only when inlining is successful. When it isn't successful, and SQL function should be evaluated, then PLpgSQL should not be slower. When body of SQL function contains SRF functions like generate_series, then inlining is not effective.
I have one function that returns all employee IDs
Function definition is like this:
CREATE OR REPLACE FUNCTION tmp()
RETURNS setof record AS
$func$
begin
select emp_id from employee_master;
end;
$func$
LANGUAGE plpgsql;
But when i call this function using
select * from tmp() as abc(emp_id text);
It gives error like
ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT: PL/pgSQL function "tmp" line 3 at SQL statement
Please give solution :)
If you want to return a rowset from a PL/PgSQL function you must use RETURN - in this case, probably RETURN QUERY:
RETURN QUERY SELECT emp_id FROM employee_master;
I don't see the point of having this in a PL/PgSQL function at all, though.
Make the function a plain SQL one as in:
...
LANGUAGE SQL;
It is much more practical to declare the actual type of the column instead of the unwieldy record type. Assuming emp_id to be integer, a simple SQL function could look like this:
CREATE OR REPLACE FUNCTION tmp()
RETURNS SETOF integer AS
$func$
SELECT emp_id FROM employee_master
$func$ LANGUAGE sql;
However, the error message in your comment does not match the given question. Depending on your actual requirements, you would adjust the RETURN type.