CREATE OR REPLACE FUNCTION Test_Param_Insert_Data(p_schema_table text, p_dblinkcon text) RETURNS void AS $$
declare
rec p_schema_table;
BEGIN
....
How to use function parameter p_schema_table as composite_type_name e.g. rec public.customer.
I tested create function, but error return
ERROR: type "p_schema_table" does not exist
Why plpgsql language don't understand p_schema_table is passing function parameter, It's should treat to table name e.g public.customer
You can't declare variable, which type is passed as parameter. In such case you should declare it using RECORD type.
Related
CREATE OR ALTER FUNCTION sso.FINDSEQVALUE
(#sequence_text text)
RETURNS int
AS
BEGIN
DECLARE #value int;
DECLARE #sequence_value nvarchar(150);
SELECT #sequence_value = CAST(#sequence_text AS nvarchar(150));
SELECT #value = NEXT VALUE FOR #sequence_value;
RETURN #value;
END;
I have a problem. I have created a function on SQL Server and I defined the parameter as you can see. But I cannot add the this command #sequence_value after NEXT VALUE FOR command and I am getting an error.
Incorrect syntax near '#sequence_value'
Somebody can say that "You can use (SELECT NEXT VALUE FOR [SEQUENCE])". But I need this function because of there are two different database on my project. I need same function for databases. In addition function parameter need to be text.
What should I do?
Is there 'concat' function in GreenPlum? I can use concat function in postgresql and it works well, but when i use it in Greenplum, I got an error.
select concat('a', 'b');
ERROR: function concat(unknown, unknown) does not exist at character 8
HINT: No function matches the given name and argument types. You may need to add explicit type casts.
LINE 1: select concat('a', 'b');
^
Is there some other functions can instead of 'concat' function in GreenPlum? And I have tried to create a function to instead of it, but got some syntax errors also.
CREATE OR REPLACE FUNCTION my_concat(VARIADIC arr VARCHAR[] ) RETURNS VARCHAR AS $$ SELECT array_to_string(arr, ''); $$ LANGUAGE SQL;
ERROR: syntax error at or near "VARCHAR" at character 51
LINE 1: CREATE OR REPLACE FUNCTION my_concat(VARIADIC arr VARCHAR[] ...
^
Anyone can help? Thanks very much!
Like most databases, Greenplum uses "||" to concatenate two strings together.
SELECT 'Green' || 'plum';
Result:
Greenplum
its a versional issue , you have use || in place where ever u using contact function.
Greenplum doesn't have the concat function yet. May be you can modify your code to use "||" instead of concat.
Well,
First I agree that you should replace your code to use the correct SQL syntax '||' for concatenation.
If you really want to create a function to emulate the concat, you could do something like:
create or replace function myschema.concat(arg1 text, arg2 text)
returns text as
$body$
declare
v_arg1 text;
v_arg2 text;
begin
v_arg1 := arg1;
v_arg2 := arg2;
return v_arg1 || v_arg2;
end
$body$
language plpgsql volatile;
Then, the query will work:
select myschema.concat('test1', 'test2');
>>test1test2
Hope you are looking for the below query.
gpadmin=# CREATE OR REPLACE FUNCTION my_concat( character varying[] ) RETURNS VARCHAR AS $$ SELECT array_to_string($1, ''); $$ LANGUAGE SQL;
gpadmin=# select my_concat(ARRAY['Green','plum']);
my_concat
Greenplum
Is there any way in PostgreSQL to declare local type "TABLE OF ..%ROWTYPE INDEX BY BINARY_INTEGER" inside a function like in Oracle?
CREATE OR REPLACE FUNCTION FNC
RETURN NUMBER
AS
TYPE TYPE_TB IS TABLE OF ADM_APPLICATIONS%ROWTYPE
INDEX BY BINARY_INTEGER;
TB_VAR TYPE_TB;
BEGIN
return 1;
END;
For every table there is also a corresponding type (with the same name) available.
So you can do the following:
CREATE OR REPLACE FUNCTION fnc()
RETURNs integer
AS
$$
declare
tb_var adm_applications[];
begin
return 1;
end;
$$
language plpgsql;
I have the following pl/pgsql function. (Obviously, this is not the full function, it's just the minimal amount of code needed to reproduce the problem)
CREATE OR REPLACE FUNCTION test_func(infos TEXT[][])
RETURNS void AS
$$
DECLARE
info TEXT[];
type TEXT[];
name TEXT;
BEGIN
FOREACH info SLICE 1 IN ARRAY infos LOOP
RAISE NOTICE 'Stuff: %', info;
type := info[1];
name := info[2];
RAISE NOTICE 'Done with stuff';
END LOOP;
RETURN;
END;
$$ LANGUAGE plpgsql;
When I run the function using SELECT test_func('{{something, things},{other, data}}'::TEXT[][]);, I get the following output:
NOTICE: Stuff: {something,things}
ERROR: malformed array literal: "something"
DETAIL: Array value must start with "{" or dimension information.
CONTEXT: PL/pgSQL function test_func(text[]) line 10 at assignment
I don't understand how this error is happening. When the value of info is printed, it shows {something,things}, which seems to me to be a proper array literal.
I am using PostgreSQL version 9.4.7, in case it matters.
The variable type should be text (not text[]):
CREATE OR REPLACE FUNCTION test_func(infos TEXT[][])
RETURNS void AS
$$
DECLARE
info TEXT[];
type TEXT;
name TEXT;
...
I have a function in Postgres which return a setof composite type. When returning I am only able do it with return next; but not with return query command, why is that?
CREATE TYPE return_type AS
(paramname character varying,
value character varying);
CREATE OR REPLACE FUNCTION test(i_param1 character varying, i_param2 character varying)
RETURNS SETOF return_type AS
--this works just fine returning two rows
r.paramname:='row1';
r.value:='myvalue1';
return next r;
r.paramname:='row1';
r.value:='myvalue1';
return next r;
return;
-- with this command I do not get a single row attached in the resultset
return query
select 'row1' as paraName,'myvalue1' as value
UNION ALL
select 'row2' as paraName,'myvalue2' as value;
return;
This works for me, even in Postgres 8.4:
CREATE OR REPLACE FUNCTION test(i_param1 varchar, i_param2 varchar)
RETURNS SETOF return_type AS
$func$
BEGIN
RETURN QUERY
SELECT 'row1'::varchar, 'myvalue1'::varchar
UNION ALL
SELECT 'row2','myvalue2';
END
$func$ LANGUAGE plpgsql;
Your original should only return an exception, because of type mismatch. You need to cast the string literals to matching types. On the other hand, column aliases are irrelevant here: not visible outside the function body.
SQL Fiddle