Alan Mulligan
Alan Mulligan

Reputation: 1198

PLSQL call procedure from function

I have a function which is called from a select query, below is the function which works perfect. I want to call the procedure below if boolean = 1 that inserts values into the login table:

create or replace FUNCTION isLoggedIn(x IN VARCHAR2, y IN VARCHAR2)
RETURN number IS
boolean number(1) := 0;
BEGIN
SELECT count(*) into boolean
FROM VIEWLOGIN
WHERE username = x AND password = y;

IF boolean = 1 THEN
    PROCDURELOGIN
    RETURN boolean;     
ELSE
    RETURN 0;
END IF;
END;

This is my procedure:

create or replace PROCEDURE PROCDURELOGIN
IS 
BEGIN
   INSERT INTO "SW3"."LOGIN" (LOGINID, MEMBERID) 
   VALUES (seqLogin.NEXTVAL, '1');
   Commit;
END;

Create view VIEWLOGIN
SELECT firstname, surname, username, password
FROM member

But I get the error when I run the query:

Error starting at line : 1 in command -
SELECT firstname, surname, isLoggedIn(username, password)
FROM VIEWLOGIN
WHERE username = 'fionawalshe' AND password = 'qwertyu8'
Error report -
SQL Error: ORA-14551: cannot perform a DML operation inside a query 
ORA-06512: at "SW3.PROCDURELOGIN", line 4
ORA-06512: at "SW3.ISLOGGEDIN", line 10
14551. 00000 -  "cannot perform a DML operation inside a query "
*Cause:    DML operation like insert, update, delete or select-for-update
           cannot be performed inside a query or under a PDML slave.
*Action:   Ensure that the offending DML operation is not performed or
           use an autonomous transaction to perform the DML operation within
           the query or PDML slave.

Upvotes: 0

Views: 244

Answers (1)

Dmitriy
Dmitriy

Reputation: 5565

Oracle clearly says in the error message, what is a problem. Try this:

create or replace PROCEDURE PROCDURELOGIN
IS
pragma autonomous_transaction; 
BEGIN
   INSERT INTO "SW3"."LOGIN" (LOGINID, MEMBERID) 
   VALUES (seqLogin.NEXTVAL, '1');
   Commit;
END;

By the way, I don't like such procedures and recommend not to use them, if possible.

Upvotes: 2

Related Questions