gpt4 book ai didi

oracle - PL/SQL 函数参数

转载 作者:行者123 更新时间:2023-12-04 10:27:39 25 4
gpt4 key购买 nike

在 PL/SQL 中,下面的代码将失败。它不允许为 varchar2 参数定义大小。你知道为什么吗?我如何解决它?

create or replace function logMessage(msg varchar2(2000))
return number as
begin
null;
return 1;
end logMessage;
/

错误信息是

1/33 PLS-00103: Encountered the symbol "(" when expecting one of the following:

:= . ) , @ % default character The symbol ":=" was substituted for "(" to continue.

最佳答案

您可以通过删除大小约束来修复它。不需要:

create or replace function logMessage (msg in varchar2)
return number is
begin
null;
return 1;
end logMessage;
/

我假设您的功能比这稍微复杂一些?
create function 的完整语法声明 from the documentation是:
CREATE [OR REPLACE] FUNCTION [Owner.]FunctionName 
[(arguments [IN|OUT|IN OUT][NOCOPY] DataType [DEFAULT expr][,...])]
RETURN DataType [InvokerRightsClause] [DETERMINISTIC]
{IS|AS}

the specifics附近有很多资料如果你有兴趣,但你可能会发现 TECH on the Net更有用。

回答你的第一个问题,为什么我不知道也找不到答案。但是 to quote APC :

This is annoying but it's the way PL/SQL works so we have to live with it.



简而言之,您应该在运行时知道某事将持续多长时间并因此能够处理它。不过,您可以考虑以下几个选项:

如果你知道你想要的消息长度,你可以定义一个变量,它的默认值是 substr参数的:
create or replace function logmessage ( msg in varchar2 ) return number is

l_msg varchar2(2000) := substr(msg,1,2000);

begin
return 1;
end;

或者,您可以检查函数本身的长度:
create or replace function logmessage ( msg in varchar2 ) return number is

begin

if length(msg) > 2000 then
return 0;
end if;

return 1;
end;

关于oracle - PL/SQL 函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11282735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com