Domanda

I keep getting a error when i run this code, What wrong with the code?

create or replace function f_vars(line varchar2,delimit varchar2 default ',')
return line_type is type line_type is varray(1000) of varchar2(3000);
sline varchar2 (3000);
line_var line_type;

pos number;
begin
sline := line;
for i in 1 .. lenght(sline)
loop

pos := instr(sline,delimit,1,1);
if pos =0 then
line_var(i):=sline;
exit;

endif;
string:=substr(sline,1,pos-1);
line_var(i):=string;
sline := substr(sline,pos+1,length(sline));

end loop;
return line_var;
end;

LINE/COL ERROR


20/5 PLS-00103: Encountered the symbol "LOOP" when expecting one of the following: if

22/4 PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: end not pragma final instantiable order overriding static member constructor map

È stato utile?

Soluzione

Stack Overflow isn't really a de-bugging service.

However, I'm feeling generous.

You have spelt length incorrectly; correcting this should fix your first error. Your second is caused by endif;, no space, which means that the if statement has no terminator.

This will not correct all your errors. For instance, you're assigning something to the undefined (and unnecessary) variable string.


I do have more to say though...

I cannot over-emphasise the importance of code-style and whitespace. Your code is fairly unreadable. While this may not matter to you now it will matter to someone else coming to the code in 6 months time. It will probably matter to you in 6 months time when you're trying to work out what you wrote.

Secondly, I cannot over-emphasise the importance of comments. For exactly the same reasons as whitespace, comments are a very important part of understanding how something works.

Thirdly, always explicitly name your function when ending it. It makes things a lot clearer in packages so it's a good habit to have and in functions it'll help with matching up the end problem that caused your second error.

Lastly, if you want to return the user-defined type line_type you need to declare this _outside your function. Something like the following:

create or replace object t_line_type as object ( a varchar2(3000));
create or replace type line_type as varray(1000) of t_line_type;

Adding whitespace your function might look something like the following. This is my coding style and I'm definitely not suggesting that you should slavishly follow it but it helps to have some standardisation.

create or replace function f_vars ( PLine in varchar2
                                  , PDelimiter in varchar2 default ','
                                    ) return line_type is 

   /* This function takes in a line and a delimiter, splits
      it on the delimiter and returns it in a varray.
      */

   -- local variables are l_
   l_line varchar2 (3000) := PLine;
   l_pos number;

   -- user defined types are t_
   -- This is a varray.
   t_line line_type;

begin

   for i in 1 .. length(l_line) loop

      -- Get the position of the first delimiter.
      l_pos := instr(l_line, PDelimiter, 1, 1);

      -- Exit when we have run out of delimiters.
      if l_pos = 0 then
         t_line_var(i) := l_line;
         exit;
      end if;

      -- Fill in the varray and take the part of a string
      -- between our previous delimiter and the next.  
      t_line_var(i) := substr(l_line, 1, l_pos - 1);
      l_line := substr(l_line, l_pos + 1, length(l_line));

   end loop;

   return t_line;

end f_vars;
/
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top