简体   繁体   English

Oracle无效状态错误

[英]Oracle invalid state error

OK guys, this error is getting real old, I get nothing from Oracle, the function will compile but gives a warning, then if I try to use it, the invalid state message. 好的,这个错误真的很老了,我从Oracle那里什么都没有得到,该函数将编译但发出警告,然后,如果我尝试使用它,则返回无效状态消息。 Now here is the real rub. 现在这里是真正的摩擦。 If I 're-code' it as an anonymous program it runs fine!! 如果我将其“重新编码”为匿名程序,它将运行正常!! So somewhere in my declaration there has to be an error, as it contains the only real difference between the Create Function and the Anonymous Program. 因此,在我的声明中某处一定有一个错误,因为它包含“创建函数”和“匿名程序”之间的唯一真正区别。 Code for both is below, and yes I know there are easier ways to reverse a string, it is a class assignment and we cant use the built in string reverse function. 两者的代码都在下面,是的,我知道有更简单的方法来反转字符串,这是一个类分配,我们不能使用内置的字符串反转功能。 Any help is appreciated!! 任何帮助表示赞赏!

AS AN ANONYMOUS PROGRAM:(Note: this works fine) 作为一个匿名程序:(注意:这很好用)

 DECLARE
        strBinary             varchar2(32);
        intAnswer             number := 0;
        intExponent       number := 0;
        strReverse        varchar2(32); 
        strLength             number := 0;
        intBinChar            number := 0;
        BinChar               char;

     BEGIN
      dbms_output.enable;
        -- Reverse the Binary String first using similar function to Reversed_Name  
        strBinary := ('11111111');
        strLength := Length(StrBinary) + 1;

        FOR i IN 1..strLength   LOOP 
             BinChar := SUBSTR(strBinary,strLength-i,1);
            strReverse := strReverse || BinChar;
        END LOOP;

        --Extract the binary character from the reversed string and do the math
        FOR i IN REVERSE 1..LENGTH(strReverse)-1  LOOP
                BinChar := SUBSTR(strReverse, i, 1);
                intExponent := i - 1;
                intBinChar := TO_NUMBER(BinChar);
                intAnswer := intAnswer + (intBinChar * (2 ** intExponent));
        END LOOP;

     -- RETURN intAnswer;
        dbms_output.put_line(intAnswer);
    END;--RKC_BinToInt;

AND HERE IS THE FUNCTION THAT IS INVALID: 这是无效的功能:

 CREATE OR REPLACE FUNCTION RKC_BinToInt(strBinary IN varchar2(32) RETURN number IS
        intExponent       number := 0;
        strReverse        varchar2(32); 
        strLength             number := 0;
        intBinChar            number := 0;
        BinChar               char;

     BEGIN

        -- Initialize Reverse the Binary String first using similar function to Reversed_Name  
        intAnswer := 0;
        strLength := Length(StrBinary) + 1;

        FOR i IN 1..strLength   LOOP 
             BinChar := SUBSTR(strBinary,strLength-i,1);
            strReverse := strReverse || BinChar;
        END LOOP;

        --Extract the binary character from the reversed string and do the math
        FOR i IN REVERSE 1..LENGTH(strReverse)-1  LOOP
                BinChar := SUBSTR(strReverse, i, 1);
                intExponent := i - 1;
                intBinChar := TO_NUMBER(BinChar);
                intAnswer := intAnswer + (intBinChar * (2 ** intExponent));
        END LOOP;

      RETURN intAnswer;

    END RKC_BinToInt;
    /

Your function should be like this 你的功能应该是这样的

CREATE OR REPLACE FUNCTION RKC_BinToInt(strBinary IN varchar2) RETURN number IS
        intExponent       number := 0;
        strReverse        varchar2(32); 
        strLength             number := 0;
        intBinChar            number := 0;
        BinChar               char;
        intAnswer NUMBER;
     BEGIN

        -- Initialize Reverse the Binary String first using similar function to Reversed_Name  
        intAnswer := 0;
        strLength := Length(StrBinary) + 1;

        FOR i IN 1..strLength   LOOP 
             BinChar := SUBSTR(strBinary,strLength-i,1);
            strReverse := strReverse || BinChar;
        END LOOP;

        --Extract the binary character from the reversed string and do the math
        FOR i IN REVERSE 1..LENGTH(strReverse)-1  LOOP
                BinChar := SUBSTR(strReverse, i, 1);
                intExponent := i - 1;
                intBinChar := TO_NUMBER(BinChar);
                intAnswer := intAnswer + (intBinChar * (2 ** intExponent));
        END LOOP;

      RETURN intAnswer;

    END RKC_BinToInt;
    /

I FOUND THE ANSWER: 我发现答案:

The problem was not with the ridiculously complex code I created to do a basic Bin to Dec conversion. 问题不在于我创建的执行基本的Bin到Dec转换的可笑的复杂代码。

The problem was the RETURN variable intAnswer had to be declared in the Header (Create or Replace) Statement. 问题是必须在标头(创建或替换)语句中声明RETURN变量intAnswer。 Even though I initialized the variable in the BEGIN section of the code, the compiler did not have a handle for it. 即使我在代码的BEGIN部分中初始化了变量,编译器也没有针对它的句柄。 This was simply the result of some bad advice as I had asked a very experienced user if that variable had to be declared first. 这只是一些错误建议的结果,因为我曾问过一个非常有经验的用户是否必须先声明该变量。 SO NOTE: RETURN does NOT declare a variable used within the body of a program - It worked in the Anonymous program because it was of course, declared. 因此请注意:RETURN不会声明在程序主体中使用的变量-它在匿名程序中有效,因为它已被声明。

Thanks for taking the time out to help all us newbies. 感谢您抽出宝贵时间来帮助我们所有的新手。 To each and every poster on this site, you are all deserving of more than just a simple thanks for your tireless efforts. 对于本网站上的每位海报发布者,您所做的不懈努力都不仅仅是简单的感谢。

Thanks Again Rob Campbell 再次感谢Rob Campbell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM