Variable without value pl / sql

1

I tried to run this pl / sql but it did not work

declare
v teste%rowtype;
begin
v.ra;
select ra into v.ra from teste where nome = 'Danilo';
dbms_output.put_line(v.ra);
end;

After a long time I decided to assign a value to it and then change the value with the select and it worked

declare
v teste%rowtype;
begin
v.ra:= 0;
select ra into v.ra from teste where nome = 'Danilo';
dbms_output.put_line(v.ra);
end;

I wanted to know why it gives error with null value and if it would be a good practice always assign the value of 0 to all variables before anything

    
asked by anonymous 06.05.2018 / 00:00

1 answer

1

The problem in this case is not related to the fact that v.ra is NULL , it's just a syntax error.

Most likely the error message you received is as follows:

  

ORA-06550: line 3, column 1: PLS-00221: 'RA' is not a procedure or is   ORA-06550: line 3, column 1: PL / SQL: Statement ignored

Often the error messages are not very clear on the reason for the error, but in this case the reason seems to be clear: v.ra; is not a procedure or block of code that can be executed.

I did not quite understand its purpose with this statement, but the variable v has a type that depends on the structure of the teste table, so v.ra should probably be NUMBER, CHAR, BLOB, DATE, or other type commonly used to define a column of a table. It will therefore not be possible to "execute" the expression v.ra;

When you changed the statement to v.ra := 0 passed or compiler a statement that it understands. In this case, this is an assignment. Hence your code has run without error.

To show that the problem did not even have to do with NULL, see the following anonymous block, without the assignment, running at SQL Fiddle.

declare v teste%rowtype;
begin
select ra into v.ra from teste where nome = 'Danilo';
dbms_output.put_line(v.ra);
end
;
/
    
06.05.2018 / 09:45