Oracle SQL Problems using Variables - sql

Having some simple issues here but I've been up for so many hours having trouble focusing so hoping someone else with a fresh head can answer this easy question which I can't seem to find a good answer that works for me in SQL Developer
DECLARE
TEST123 NUMBER;
BEGIN
SELECT COUNT(APP_ID) INTO TEST123 FROM applicant_credit;
SELECT * FROM APPLICANT_CREDIT;
END;
When I run this which seems easy enough to me I am getting error:
Error report:
ORA-06550: line 5, column 3:
PLS-00428: an INTO clause is expected in this SELECT statement
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
Also I have a stored procedure I made and works when I send it hard coded numbers, but I need to pass it the count of records from the applicant_credit mentioned in the earlier query which is what I'm actually after. Would the execute stored procedure work the same in the above block as it does outside a block. So should this work??
DECLARE
TEST123 NUMBER;
BEGIN
SELECT COUNT(APP_ID) INTO TEST123 FROM applicant_credit;
EXECUTE UPDATE_DECISION(102, 1, 1, 1, 1, TEST123);
SELECT * FROM APPLICANT_CREDIT;
END;
When I run this I get:
Error report:
ORA-06550: line 5, column 11:
PLS-00103: Encountered the symbol "UPDATE_DECISION" when expecting one of the following:
:= . ( # % ; immediate
The symbol ":=" was substituted for "UPDATE_DECISION" to continue.
Hitting the wall a bit here, any help is appreciated!!

for the first sql statement, you are getting that error because you cannot execute a select * statement inside a plsql block in that way. You can either use a cursor or a bulk collect statement or any other way to "redirect" the output of select * into a collection of records.
DECLARE
TEST123 NUMBER;
cursor c_cur
is select col_1, col_2 from application_credit;
BEGIN
SELECT COUNT(APP_ID) INTO TEST123 FROM applicant_credit;
-- loop through the cursor if required
END;
your second sql statement gave an error because you cannot use an execute statement inside the plsql block (unless it is execute immediate which is completely different). So, you can modify the code as follows :-
DECLARE
TEST123 NUMBER;
cursor c_cur is
select col1, col2 from applicant_credit;
BEGIN
SELECT COUNT(APP_ID) INTO TEST123 FROM applicant_credit;
UPDATE_DECISION(102, 1, 1, 1, 1, TEST123);
-- loop through the cursor if required
END;
some helpful links :-
http://www.techonthenet.com/oracle/cursors/declare.php
http://www.oracle.com/technetwork/issue-archive/2008/08-mar/o28plsql-095155.html

You do not need PL/SQL at all for this unless you are trying to learn it. All can be done with SQL only.
DECLARE
TEST123 NUMBER:= 0;
BEGIN
UPDATE_DECISION(102, 1, 1, 1, 1, TEST123);
-- The outer query is not required, this is only to show you the correct syntax.
SELECT * INTO TEST123 -- you are selecting from applicant_credit
FROM
(
SELECT COUNT(app_id) FROM applicant_credit
);
END;
/

Related

Trying to declare a cursor, but I'm getting hit with end of file! (Oracle SQL)

I'm currently learning Oracle SQL and I am on cursors right now. I know it's a small problem, and probably easy to solve, but my declaration statement is pulling an end of file error. (PLS-00103)
Here is the statement:
declare cursor CustCursor is select * from Customers where cust_email is null;
Any help would be appreciated, it might also be worth knowing that I followed the Sams Teach Yourself SQL Book and am still getting these problems.
Thank you!
DECLARE can't exist alone in the Universe - it is part of a PL/SQL block which also requires at least dummy BEGIN-END part of it.
This is what you have:
SQL> declare cursor CustCursor is select * from Customers where cust_email is null;
2 /
declare cursor CustCursor is select * from Customers where cust_email is null;
*
ERROR at line 1:
ORA-06550: line 1, column 78:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the
following:
begin function pragma procedure subtype type <an identifier>
<a double-quoted delimited-identifier> current cursor delete
exists prior
This is what you should have:
SQL> declare cursor CustCursor is select * from Customers where cust_email is null;
2 begin
3 null;
4 end;
5 /
PL/SQL procedure successfully completed.
SQL>
Or, as Ed Stevens says, to put everything where it belongs (although, the result will be just the same):
SQL> declare
2 cursor CustCursor is select * from Customers where cust_email is null;
3 begin
4 null;
5 end;
6 /
PL/SQL procedure successfully completed.
SQL>

PL/SQL procedure not compiling

I have a PL/SQL procedure that is not compiling. The errors are:
Error(3,7): PLS-00103: Encountered the symbol "INTO" when expecting one of the following: ( begin case declare exit for goto if loop mod null pragma raise return select update while with <an identifier> <a double-quoted delimited-identifier> <a bind variable> << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge The symbol "INTO" was ignored.
Error(8,1): PLS-00428: an INTO clause is expected in this SELECT statement.
My code:
CREATE OR REPLACE PROCEDURE findvisitsandlaborcosts
as
begin
select * from SI.customer;
end;
/
I have googled an online syntax checker and it says on the first line there is an error. But WHERE?!? It seems to be correct. I have googled the syntax of declaring a procedure and I have cross-checked many times. It must be something simple that I am overlooking...
in a PLSQL code, you need a placeholder to keep results of a SELECT query. Since PLSQL engine is expecting INTO clause within SELECT statement.
To begin with, you can select a set of columns and assign their values to local variables.
Your code should be like this -
CREATE OR REPLACE PROCEDURE findvisitsandlaborcosts
as
v_column1 SI.customer.column1%type;
v_column2 SI.customer.column2%type;
begin
select column1, column2 into v_column1, v_column2 from SI.customer;
end;
/
Note - you need to replace column1 and column2 with actual column names before running this code at your end.
If you want the results to get displayed on the caller of the procedure, then you would define an out parameter and print the records outside of the procedure
CREATE OR REPLACE PROCEDURE findvisitsandlaborcosts(x out sys_refcursor)
as
begin
open x for
select * from dual;
end;
/
--Note this block of code needs to be run in sqlci or sqldeveloper
define m ref cursor;
exec findvisitsandlaborcosts(:x);
print x;
Oracle 12c has support for implict return results
Have a look at this link
https://oracle-base.com/articles/12c/implicit-statement-results-12cr1

Common table expression defining a function in a cursor

Does somebody know if it is possible to use the a common table expression that defines a local function inside a PL/SQL cursor?
For example this code produce an error for me:
DECLARE
CURSOR LV_CUR IS
WITH function fct2(x number) return number is
begin
return 2*x;
end;
TEMP_AV AS
(
SELECT
fct2(LEVEL)
FROM
DUAL
CONNECT BY LEVEL < 10
)
SELECT
*
FROM
TEMP_AV;
BEGIN
FOR I IN LV_CUR
LOOP
NULL;
dbms_output.put_line(i.level);
END LOOP;
END;
The error is :
ORA-06550: Ligne 3, colonne 18 :
PL/SQL: ORA-00905: mot-clé absent
ORA-06550: Ligne 3, colonne 3 :
PL/SQL: SQL Statement ignored
ORA-06550: Ligne 6, colonne 5 :
PLS-00103: Encountered the symbol "END" when expecting one of the following:
begin function pragma procedure subtype type
current cursor delete
exists prior
I am able to execute the query (with the common table expression including a function definition) outside PL/SQL.
Yes and no. The 12.1 docs for the CURSOR statement explicitly say:
Restriction on select_statement
This select_statement cannot have a WITH clause.
This docs are wrong in this case, since you can have a WITH clause, it apparently just can't have the new 12c PL/SQL declarations in it. This block works fine, for example.
DECLARE
CURSOR LV_CUR IS
WITH
TEMP_AV AS
(
SELECT
level
FROM
DUAL
CONNECT BY LEVEL < 10
)
SELECT
*
FROM
TEMP_AV;
BEGIN
FOR I IN LV_CUR
LOOP
NULL;
dbms_output.put_line(i.level);
END LOOP;
END;
/

cant compile any cursor example

I'm trying to make any cursor example working, but just cant make it happen.
IDE: Oracle SQL developer
database: 10g sql oracle
I've tried this three examples, but it didn't compile well. Can someone provide me with working examples?
First one:
EDIT: works! just had 'where where'
DECLARE
CURSOR person_data_cur
as
SELECT *
FROM personal_data
WHERE where name='Karol';
BEGIN
FOR person_data
IN person_data_cur
LOOP
DBMS_OUTPUT.put_line (
person_data.surname);
END LOOP;
END;
and this:
DECLARE my_cursor1 CURSOR FOR
SELECT name, surname
FROM personal_data
WHERE name='Karol';
^error:
Error report -
ORA-06550: row 1, column 27:
PLS-00103: found symbol "FOR" when expected one of following:
:= . ( # % ; not null range default character
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
or even this:
EDIT: this one is OK, just my IDE ignored first two lines, and executed others... but i guess it was OK since it wasnt inside any block or procedure
CURSOR c1
IS
SELECT course_number
FROM courses_tbl
WHERE course_name = name_in;
For Second Example : Your syntax is incorrect.
The pseudo code for the cursor is :
Declare
Cursor Cursor_name
is
select * from table where <Conditions>;
Begin
For cur_Rec in cursor_name
Loop
<what you want to do>
End Loop;
End;
For the third example, I believe name_in is an input parameter, please correct the syntax to :
CURSOR c1 (name_in VARCHAR2)
IS
SELECT course_number
FROM courses_tbl
WHERE course_name = name_in;
Please refer Cursor Declaration for more details

How to create a Trigger in oracle?

I have oracle database 11gR2 and i want to create some triggers automatically with a block .
when i want to run query in below , i have an error .
My block is :
declare
tablesid number ;
tablenames varchar2(4000);
cursor c1 is
select id , title from hr.tables;
begin
open c1;
loop
fetch c1 into tablesid , tablenames;
CREATE OR REPLACE TRIGGER tablenames||'_before_insert'
before insert
on hr.tablenames
DECLARE
columnid number ;
columnval number ;
columntitle varchar(4000);
cursor c2 is
select id from hr.columns where tableid = tablesid ;
BEGIN
-- open c2 ;
for rec in c2 loop
select title into columntitle from hr.columns where id = rec.id
insert into hr.rowaction(columnid,newvalue,oldvalue,actiondate)
(
select id ,:new.columntitle,:old.columntitle,sysdate
from hr.tabletiltes
where id = rec.id
)
select title into columntitle from hr.columns where id = rec.id;
dbms_output.put_line(columntitle);
end loop;
end;
end loop ;close c1; end;
ORA-06550: line 11, column 6:
PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
( begin case declare end exit for goto if loop mod null
pragma raise return select update while with
<<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action :
Bind Variable "new" is NOT DECLARED
anonymous block completed
Bind Variable "new" is NOT DECLARED
anonymous block completed
Thanks
A trigger is a standalone separate piece of code that is usually considered DDL, the language that defines metadata for objects.
You cannot have a trigger declaration embedded inside a PL/SQL block. IF you need to CREATE some PL/SQL code on the fly - not a great idea - consider the DMBS_DDL.CREATE_WRAPPED procedure. You seem to have EXECUTE IMMEDIATE in mind as well. If so, have a read on this: (http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_ddl.htm)
You should define your trigger BEFORE running your PL/SQL in other words. Make two scripts.