Variable without pl/sql value

Asked

Viewed 484 times

1

I tried to run this pl/sql but it didn’t 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 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 error with the null value and if it would be a good practice to always assign the value of 0 to all variables before anything

1 answer

1

The problem in this case is not related to the fact that v.ra be or not NULL, is only 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 Undefined ORA-06550: line 3, column 1: PL/SQL: Statement Ignored

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

I didn’t quite understand your purpose with that instruction, but the variable v has a type that depends on the table structure teste, wherefore v.ra should probably be a 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 instruction to v.ra := 0 passed or compiler an instruction he understands. In this case, it is an assignment. Hence your code has run without error.

To show that the problem had nothing to do with NULL, see the following anonymous block, with no assignment, working on SQL Fiddle.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.