Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 函数通过与逗号分隔的列表进行比较来选择列值_Sql_Arrays_Postgresql_Parameter Passing - Fatal编程技术网

Sql 函数通过与逗号分隔的列表进行比较来选择列值

Sql 函数通过与逗号分隔的列表进行比较来选择列值,sql,arrays,postgresql,parameter-passing,Sql,Arrays,Postgresql,Parameter Passing,我想将逗号分隔的字符串与列值进行比较 我做了以下操作,但没有返回预期结果ll=''Java,CPP'将动态出现 create or replace function testing(ll text) returns void as $body$ Declare foo text[]; ko text[]; BEGIN select unique_code into foo from codings where unique_code =

我想将逗号分隔的字符串与列值进行比较

我做了以下操作,但没有返回预期结果
ll=''Java,CPP'
将动态出现

create or replace function testing(ll text)
returns void as
$body$
Declare

       foo text[];
       ko text[];
BEGIN
       select unique_code into foo from codings
       where  unique_code = ANY (regexp_split_to_array(ll, '\,'));
       raise info '%',foo;
END;
$body$
Language plpgsql;
我犯了以下错误

ll
的值是动态的,如上所述,现在它也返回NULL,在我有'Java'和'Cpp'的行中,这两个值都在匹配的大小写中

select unique_code from codings;

unique_code
  Java
  Python
  CPP
  C
  Haskell
我也试过修剪,但没有效果。更新代码如下:

create or replace function testing(ll text)
returns void as
$body$
Declare

       foo text[];
       --ll text;
       ko text[];
       oo text;
BEGIN
      --oo := replace(ll,'"','');
      raise info '%',regexp_split_to_array(trim(both '"' from ll), '\,');
      ko := regexp_split_to_array(trim(both '"' from ll), '\,');

       ---ll := "CH-PREP,CH-PRMB";
       --select(regexp_split_to_array(ll, '\|')) into ko;
        --foo := array(select unique_key from membership_map);
       select  unique_code into foo from codings where  unique_code = ANY(ko);
       raise info '%',foo;
       --raise info '%', ko;
END;
$body$
Language plpgsql;
然后:

selecttesting('Java,CPP')

您需要使用从
ll
值中删除

select unique_code 
from codings 
where unique_code = ANY (regexp_split_to_array(trim(both '"' from '"Java,CPP"'), '\,'));
示例数据的输出:

unique_code
Java
CPP

还有一个问题是,在一条语句中分配多个值,因此需要使用
数组
运算符。更改

select unique_code into foo from codings where unique_code = ANY(ko);

您需要使用从
ll
值中删除

select unique_code 
from codings 
where unique_code = ANY (regexp_split_to_array(trim(both '"' from '"Java,CPP"'), '\,'));
示例数据的输出:

unique_code
Java
CPP

还有一个问题是,在一条语句中分配多个值,需要使用
数组
运算符。改变

select unique_code into foo from codings where unique_code = ANY(ko);


您得到了第一个错误:

。。。函数调用中字符串文本周围缺少单引号。像

SELECT testing("Java,CPP");  -- missing ''!
您可能希望从输入中删除这些双引号,因此调用应该是:

SELECT testing('Java,CPP');
相关的:

调用修复后,函数体中数据类型不匹配引起的潜在错误就会发生。您尝试将
unique\u code text
分配给
foo text[]

例如,这将起作用:

CREATE OR REPLACE FUNCTION testing(_ll text[])  -- taking array of text
  RETURNS text[] AS
$func$
DECLARE
   foo text[];
BEGIN
   foo := ARRAY(  -- array constructor
      SELECT unique_code 
      FROM   codings
      WHERE  unique_code = ANY(_ll)
      );

   RETURN foo;
END
$func$  LANGUAGE plpgsql;
电话:

见:

传递一个有效数组文本还可以避免昂贵且不必要的
regexp\u split\u to\u array()
调用。(Regexp函数功能强大,但相对昂贵。)

可变
功能相同:

CREATE OR REPLACE FUNCTION testing(VARIADIC _ll text[])
...  -- rest as above
您仍然可以在调用中使用关键字
VARIADIC
传递数组文字(或真数组值):

SELECT testing(VARIADIC '{Java,CPP}'); -- with array
或者可以传递元素值列表(最多100个):

相关的:


如果您必须以原始格式传递字符串
trim()
双引号,并使用更便宜的
string\u to\u array()
而不是
regexp\u split\u to\u array()

电话:


旁白:不要使用神秘的
ll
作为参数名。很容易被误读为
11
..

您遇到了第一个错误:

。。。函数调用中字符串文本周围缺少单引号。像

SELECT testing("Java,CPP");  -- missing ''!
您可能希望从输入中删除这些双引号,因此调用应该是:

SELECT testing('Java,CPP');
相关的:

调用修复后,函数体中数据类型不匹配引起的潜在错误就会发生。您尝试将
unique\u code text
分配给
foo text[]

例如,这将起作用:

CREATE OR REPLACE FUNCTION testing(_ll text[])  -- taking array of text
  RETURNS text[] AS
$func$
DECLARE
   foo text[];
BEGIN
   foo := ARRAY(  -- array constructor
      SELECT unique_code 
      FROM   codings
      WHERE  unique_code = ANY(_ll)
      );

   RETURN foo;
END
$func$  LANGUAGE plpgsql;
电话:

见:

传递一个有效数组文本还可以避免昂贵且不必要的
regexp\u split\u to\u array()
调用。(Regexp函数功能强大,但相对昂贵。)

可变
功能相同:

CREATE OR REPLACE FUNCTION testing(VARIADIC _ll text[])
...  -- rest as above
您仍然可以在调用中使用关键字
VARIADIC
传递数组文字(或真数组值):

SELECT testing(VARIADIC '{Java,CPP}'); -- with array
或者可以传递元素值列表(最多100个):

相关的:


如果您必须以原始格式传递字符串
trim()
双引号,并使用更便宜的
string\u to\u array()
而不是
regexp\u split\u to\u array()

电话:


旁白:不要使用神秘的
ll
作为参数名。很容易被误读为
11

现在我得到了一个错误:格式错误的数组文字:“Java”详细信息:数组值必须以“{”或维度信息开头。上下文:PL/pgSQL函数测试(文本)SQL语句第16行RAISE info“%”,regexp_split_to_数组(trim(从ll开始的“两者”),“\”;ko:=regexp_split_to_to_数组(trim(两者)”'从ll,'\,');--ll:=“CH-PREP,CH-PRMB”--select(regexp\u split\u to\u array(ll,\ \ \))到ko;--foo:=数组(从成员关系图中选择唯一键);从成员关系图中选择唯一键到foo,其中唯一键=任意(ko);请使用此更新的代码更新您的问题。在注释中阅读太困难。我认为您需要将
select unique\u code从unique\u code=ANY(ko)的编码更改为foo
select array(从unique\u code=ANY(ko)的编码中选择unique\u code))into foo;
现在它正在工作,谢谢你让它更新后接受它。现在我得到了错误:格式错误的数组文字:“Java”详细信息:数组值必须以“{”或维度信息开头。上下文:PL/pgSQL函数测试(文本)SQL语句第16行RAISE info“%”,regexp_split_to_数组(trim(从ll开始的两个“,”,“,”);ko:=regexp\u split\u to_array(trim(从ll开始的两个“,”\,”);--ll:=“CH-PREP,CH-PRMB”;--select(regexp\u split\u to_array(ll,\\\\”)为ko;--foo:=数组(从成员关系图中选择唯一键);从成员关系图中选择唯一键进入foo,其中唯一键=任意(ko);请使用此更新的代码更新您的问题。在注释中阅读太困难。我认为您需要将
select unique\u code从unique\u code=ANY(ko)的编码更改为foo
select array(从unique\u code=ANY(ko)的编码中选择unique\u code))into foo;
现在它正在工作,谢谢您,请将其更新后再接受
SELECT testing('"Java,CPP"'); -- even with enclosing double quotes