在这种情况下,正则表达式非常简单; 源账户取第一个数字,DBID取第二个数字——它是REGEXP_SUBSTR函数的最后一个参数。
例如:
SQL> with test (col) as
2 (select '[222].[2] ([srcacct id].[database id])' from dual)
3 select regexp_substr(col, '\d+', 1, 1) source_account,
4 regexp_substr(col, '\d+', 1, 2) dbid
5 from test;
SOU D
--- -
222 2
SQL>
[编辑:变量]
这取决于您使用的客户端工具。在 SQL*Plus 中,您可以使用 '&&col'(或您想提供的任何名称)并运行
SQL> set ver off
SQL> select regexp_substr('&&col', '\d+', 1, 1) source_account,
2 regexp_substr('&&col', '\d+', 1, 2) dbid
3 from dual;
Enter value for col: [222].[2] ([srcacct id].[database id]
SOU D
--- -
222 2
SQL> undefine col
结束,取消定义它,因为“&&”将保持其值永远。
无论如何,如果您想“传递”某些内容并“返回”结果,请考虑改为编写 函数。它无处不在。例如:
SQL> create or replace function f_ret (par_input in varchar2)
2 return varchar2
3 is
4 begin
5 return 'Source account ID = ' || regexp_substr(par_input, '\d+', 1, 1) ||
6 ', DBID = ' || regexp_substr(par_input, '\d+', 1, 2);
7 end;
8 /
Function created.
SQL> select f_ret('[222].[2] ([srcacct id].[database id])') result from dual;
RESULT
--------------------------------------------------------------------------------
Source account ID = 222, DBID = 2
SQL>
[编辑#2:查找表]
您编写的代码(作为注释)无法编译;你不能那样做。这是它的外观示例。我现在无法测试它;自己做吧。
create or replace function f_ret (par_input in varchar2)
return varchar2
is
l_source_account_id number;
l_dbid number;
retval varchar2(200);
begin
l_source_account_id := regexp_substr(par_input, '\d+', 1, 1);
l_dbid := regexp_substr(par_input, '\d+', 1, 2);
select count(*)
into l_cnt
from accounts_table
where srcaccountid = l_source_account_id;
if l_cnt > 0 then
retval := 'Source account ID = ' || regexp_substr(par_input, '\d+', 1, 1) ||
', DBID = ' || regexp_substr(par_input, '\d+', 1, 2);
end if;
return retval;
end;