簡體   English   中英

如何在 postgresql 中為包含 varchar(2) ARRAY[50] 類型的輸入變量的 function 傳遞參數

[英]How to pass parameter for a function containing input variable of type varchar(2) ARRAY[50] in postgresql

這是用戶定義的類型和 function

CREATE TYPE test AS (test varchar(2) ARRAY[50]);
create function my_function(p_ste INOUT test ) returns text language plpgsql as $$
begin
  return p_ste ;
end;
$$;

調用 function 時如何傳遞參數?

function 只能有輸入參數(關鍵字 IN 實際上是可選的):不能使用 INOUT。

對於這種情況,您可以使用剛剛創建的類型名稱聲明 function 參數。 例子:

使用以下輸入:

create type my_type as(field varchar(2) array[50]);
create table my_table(c1 serial, c2 my_type);
insert into my_table(c2) values (row(array ['aa', 'bb' ]));
insert into my_table(c2) values (row(array ['cc', 'dd' ]));
select * from my_table;
--
create function my_function(p_val my_type) returns my_type 
language plpgsql 
as $$
begin
 p_val.field = (array['ok','ok']);
 return p_val;
end;
$$;
--
create procedure my_proc ()
language plpgsql
as $$
declare
 l_var1 my_table;
 l_var2 my_table;
begin
 select c2 into l_var1.c2 from my_table limit 1;
 raise notice 'input value=%', l_var1.c2;
 select my_function(l_var1.c2) into l_var2.c2;
 raise notice 'returned value=%', l_var2.c2;
end;
$$;

我有以下 output:

create type my_type as(field varchar(2) array[50]);
CREATE TYPE

create table my_table(c1 serial, c2 my_type);
CREATE TABLE

insert into my_table(c2) values (row(array ['aa', 'bb' ]));
INSERT 0 1

insert into my_table(c2) values (row(array ['cc', 'dd' ]));
INSERT 0 1

select * from my_table;
 c1 |     c2      
----+-------------
  1 | ("{aa,bb}")
  2 | ("{cc,dd}")
(2 rows)

create function my_function(p_val my_type) returns my_type 
language plpgsql 
as $$
begin
 p_val.field = (array['ok','ok']);
 return p_val;
end;
$$;
CREATE FUNCTION

create procedure my_proc ()
language plpgsql
as $$
declare
 l_var1 my_table;
 l_var2 my_table;
begin
 select c2 into l_var1.c2 from my_table limit 1;
 raise notice 'input value=%', l_var1.c2;
 select my_function(l_var1.c2) into l_var2.c2;
 raise notice 'returned value=%', l_var2.c2;
end;
$$;
CREATE PROCEDURE

call my_proc();
psql:tt.sql:74: NOTICE:  input value=("{aa,bb}")
psql:tt.sql:74: NOTICE:  returned value=("{ok,ok}")
CALL

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM