Reputation: 31212
I am new to PL/SQL but have plenty of other SQL experience, including Oracle (just not so much scripting). I want to declare a numeric (integer) var, set it to a row count, and display it in a sentence-wrapped string. The end goal of this exercise is to have a SQL*Plus script that prints the string "There are 1 rows".
In SQL*Plus on Unix, I do:
SQL> variable v_dCnt number;
SQL> select count(*) into :v_dCnt from dual;
COUNT(*)
----------
1
SQL> select 'There are ' || :v_dCnt || ' rows' as MESSAGE from dual;
MESSAGE
-------------------------------------------------------
There are rows
Note how it displays blank for v_dCnt
rather than a value of 1
In Rapid SQL on Win7, I do
variable v_dCnt number;
select count(*) into :v_dCnt from dual;
select 'There are ' || :v_dCnt || ' rows' from dual;
and get ORA-01008: not all variables bound
What am I doing wrong?
Upvotes: 1
Views: 778
Reputation: 231661
In SQL*Plus, you most likely just need to put the SELECT INTO
in a PL/SQL block
SQL> variable v_dCnt number;
SQL> begin
2 select count(*)
3 into :v_dCnt
4 from dual;
5 end;
6 /
PL/SQL procedure successfully completed.
SQL> ed
Wrote file afiedt.buf
1* select 'There are ' || :v_dCnt || ' rows' from dual
SQL> /
'THEREARE'||:V_DCNT||'ROWS'
-------------------------------------------------------
There are 1 rows
Upvotes: 2