ORA-06553: PLS-801:Internal Error [55018] - sql

I want to make a function call like SELECT URUN_GETIR('test1') FROM DUAL; but i got ORA-06553: PLS-801: Internal Error [55018].
I tried ORA-06553: PLS-801: internal error [55018] when testing function returning ROWTYPE this like for ex URUN_GETIR('test1').KULUSERNAME but getting same error. It didn't work for me.Thanks in advance.
My db table :
My plsql function code :
create or replace FUNCTION URUN_GETIR(KULADI VARCHAR2)
RETURN URUN%ROWTYPE
AS
URUN_TABLO URUN%ROWTYPE;
BEGIN
SELECT * INTO URUN_TABLO FROM URUN ur WHERE ur.kulusername = KULADI;
RETURN URUN_TABLO;
END;

You can achieve your goal with using table functions. Other than that you can not call directly your function like you wanted. Here is another choice for you Use Stackoverflow Wisely
CREATE TABLE URUN
(
CREATED_BY VARCHAR2 (50 CHAR),
CREATED_DATE DATE,
UPDATED_BY VARCHAR2 (50 CHAR),
KULUSERNAME VARCHAR2 (50 CHAR),
ID NUMBER (10)
);
INSERT INTO URUN (CREATED_BY, CREATED_DATE, UPDATED_BY, KULUSERNAME, ID) VALUES('TEST1',TO_DATE('19000101','YYYYMMDD') ,'TTEST1','USER1',1);COMMIT;
INSERT INTO URUN (CREATED_BY, CREATED_DATE, UPDATED_BY, KULUSERNAME, ID) VALUES('TEST2',TO_DATE('19000102','YYYYMMDD') ,'TTEST2','USER2',2);COMMIT;
INSERT INTO URUN (CREATED_BY, CREATED_DATE, UPDATED_BY, KULUSERNAME, ID) VALUES('TEST3',TO_DATE('19000103','YYYYMMDD') ,'TTEST3','USER3',3);COMMIT;
INSERT INTO URUN (CREATED_BY, CREATED_DATE, UPDATED_BY, KULUSERNAME, ID) VALUES('TEST4',TO_DATE('19000104','YYYYMMDD') ,'TTEST4','USER4',4);COMMIT;
CREATE OR REPLACE TYPE URUN_OBJ AS OBJECT
(
CREATED_BY VARCHAR2 (50 CHAR),
CREATED_DATE DATE,
UPDATED_BY VARCHAR2 (50 CHAR),
KULUSERNAME VARCHAR2 (50 CHAR),
ID NUMBER (10)
);
CREATE OR REPLACE TYPE URUN_OBJ_TAB AS TABLE OF URUN_OBJ;
CREATE OR REPLACE FUNCTION URUN_GETIR (KULADI IN VARCHAR2)
RETURN URUN_OBJ_TAB
PIPELINED
AS
REC_OBJ URUN_OBJ;
CURSOR DATA
IS
SELECT *
FROM URUN UR
WHERE UR.KULUSERNAME = KULADI;
BEGIN
FOR REC IN DATA
LOOP
REC_OBJ :=
URUN_OBJ (REC.CREATED_BY,
REC.CREATED_DATE,
REC.UPDATED_BY,
REC.KULUSERNAME,
REC.ID);
PIPE ROW (REC_OBJ);
END LOOP;
RETURN;
END;
/
SELECT * FROM TABLE(URUN_GETIR('USER1'));
CREATED_BY CREATED_DATE UPDATED_BY KULUSERNAME ID
TEST1 1.01.1900 TTEST1 USER1 1

Related

Procedure to insert data if not exist pl sql

I have a table with these columns (id,first_name,birth). I want to create a procedure that insert a new customer only if the id inserted doesn't exist in the table. If it already exist, then don't insert it. This is my code so far, but I got an error 'line 3 sql statement ignored'. Any idea? I need to use procedure and pl sql in oracle. Thanks!
CREATE OR REPLACE PROCEDURE add_emp(v_id IN int,
v_name IN varchar2,
v_bday IN date) IS
BEGIN
INSERT INTO Employees
(Id, First_name, Birth)
SELECT *
FROM (SELECT v_id, v_name, v_bday) AS tmp
WHERE NOT EXISTS (SELECT Id FROM Employees WHERE Id = v_id);
END;
/
DECLARE
m_id int := 3;
m_name varchar2 := 'John';
m_bday date := '16-Dec-1990';
BEGIN
add_cust(m_id, m_name, m_bday);
END;
/
Your procedure has some syntax issue which is fixed in following code:
CREATE OR REPLACE PROCEDURE ADD_EMP (
V_ID IN INT,
V_NAME IN VARCHAR2,
V_BDAY IN DATE
) IS
BEGIN
INSERT INTO EMPLOYEES (
ID,
FIRST_NAME,
BIRTH
)
SELECT V_ID,
V_NAME,
V_BDAY
FROM DUAL -- FROM clause was missing
WHERE NOT EXISTS (
SELECT ID
FROM EMPLOYEES
WHERE ID = V_ID
);
END;
/
Also, Your calling PL/SQL block has some issues which are corrected in the following code:
DECLARE
M_ID INT := 3;
M_NAME VARCHAR2(10) := 'John'; -- varchar2 must be declared with size
M_BDAY DATE := DATE '1990-12-16'; -- added date literal to convert string to date
BEGIN
ADD_CUST(M_ID, M_NAME, M_BDAY);
END;
/
In Oracle SELECT does not work without a FROM clause (other DBMS products are different). So you need to provide a table; you can use DUAL, which is a dummy table provided by Oracle which is guaranteed to return one row.
INSERT INTO Employees(Id,First_name,Birth)
SELECT v_id, v_name, v_bday
from dual
WHERE NOT EXISTS (
SELECT Id FROM Employees WHERE Id = v_id
);
Your INSERT statement would work with a slight change
CREATE OR REPLACE PROCEDURE add_emp(v_id Employees.Id%type,
v_name Employees.First_name%type,
v_bday Employees.Birth%type) IS
BEGIN
INSERT INTO Employees
(Id, First_name, Birth)
SELECT v_id, v_name, v_bday
FROM dual
WHERE NOT EXISTS (SELECT * FROM Employees WHERE Id = v_id);
END;
/
You can replace INSERT statement with MERGE as an alternative DML such as
MERGE INTO Employees e1
USING
(SELECT v_id AS id, v_name AS First_name, v_bday AS birth
FROM dual) e2
ON ( e1.id = e2.id )
WHEN MATCHED THEN UPDATE SET e1.First_name = e2.First_name,
e1.Birth = e2.Birth -- If you need to change for the matching case
WHEN NOT MATCHED THEN INSERT( e1.id, e1.First_name, e1.birth )
VALUES( e2.id, e2.First_name, e2.birth );

PL/SQL Statement ignored : missing expression

I have a little DB with 3 tables: account, account_statements and account operations. I need to know:
all credit operations of the period
all debet operations of the perion
a balance on any date
My script is:
CREATE TABLE account
(
id number(10) NOT NULL,
account number(20) NOT NULL,
CONSTRAINT account_id PRIMARY KEY (id)
);
CREATE TABLE account_statements
(
id number(10) NOT NULL,
account_id number(10) NOT NULL,
statement_date date,
inbalance number(20,2),
outbalance number (20,2),
CONSTRAINT statement_id PRIMARY KEY (id),
CONSTRAINT account_statements_foreign_id FOREIGN KEY (account_id) REFERENCES account(id)
);
CREATE TABLE account_operations
(
id number(10) NOT NULL,
account_id number(10) NOT NULL,
operdate date,
summ number(20,2),
opertype char(6),
CONSTRAINT operations_id PRIMARY KEY (id),
CONSTRAINT account_operations_foreign_id FOREIGN KEY (account_id) REFERENCES account(id)
);
CREATE OR REPLACE FUNCTION Get_debet_on_period (
v_startdate IN date,
v_enddate IN date,
v_account IN account.account%TYPE)
RETURN number
IS
v_debet_summ number(20,2);
BEGIN
SELECT SUM(summ) INTO v_debet_summ
FROM account_operations ao,
account a
WHERE ao.operdate between v_startdate AND v_enddate
AND ao.opertype='DEBET'
AND a.account=v_account
AND ao.account_id=a.id;
RETURN v_debet_summ;
END;
CREATE OR REPLACE FUNCTION Get_credit_on_period (
v_startdate IN date,
v_enddate IN date,
v_account IN account.account%TYPE)
RETURN number
IS
v_credit_summ number(20,2);
BEGIN
SELECT SUM(summ) INTO v_credit_summ
FROM account_operations ao,
account a
WHERE ao.operdate between v_startdate AND v_enddate
AND ao.opertype='CREDIT'
AND a.account=v_account
AND ao.account_id=a.id;
RETURN v_credit_summ;
END;
CREATE OR REPLACE FUNCTION Get_balance_on_date (
v_date IN date,
v_account IN account.account%TYPE)
RETURN number
IS
v_balance_summ number(20,2);
v_startdate date;
v_startsumm number;
BEGIN
SELECT MAX(as.statement_date) INTO v_startdate
FROM account_statements as, account a
WHERE a.id=as.account_id
AND a.account=v_account
AND as.statement_date<v_date;
IF v_startdate IS NOT NULL THEN
SELECT as.outbalance INTO v_startsumm
FROM account_statement as, account a
WHERE a.id=as.account_id
AND a.account=v_account
AND as.statement_date=v_statement_date;
v_balance_summ:=v_startsumm+Get_credit_on_period(v_startdate, v_date, v_account)-Get_debet_on_period(v_startdate, v_date, v_account);
ELSE
v_startsumm:=0;
v_balance_summ:=Get_credit_on_period(v_startdate, v_date, v_account)-Get_debet_on_period(v_startdate, v_date, v_account);
END IF;
RETURN v_balance_summ;
END;
I have an error in function Get_balance_on_date:
Errors: FUNCTION GET_BALANCE_ON_DATE Line/Col: 10/4 PL/SQL: SQL
Statement ignored Line/Col: 10/15 PL/SQL: ORA-00936: missing
expression Line/Col: 16/7 PL/SQL: SQL Statement ignored Line/Col:
16/14 PL/SQL: ORA-00936: missing expression
Your function get_balance_on_date does not compile.
You're using table account_statement, however you created table named in plural form account_statements.
You're using reserved keyword AS as an alias "account_statements as". You should change the alias to something different.
You're using undeclared variable "v_statement_date".
Fixed function for you:
CREATE OR REPLACE FUNCTION get_balance_on_date
(
v_date IN DATE
,v_account IN account.account%TYPE
) RETURN NUMBER IS
v_balance_summ NUMBER(20, 2);
v_startdate DATE;
v_startsumm NUMBER;
v_statement_date DATE; -- Remove this if you don't need, created this for function to compile
BEGIN
SELECT MAX(stm.statement_date)
INTO v_startdate
FROM account_statements stm
,account a
WHERE a.id = stm.account_id
AND a.account = v_account
AND stm.statement_date < v_date;
IF v_startdate IS NOT NULL
THEN
SELECT stm.outbalance
INTO v_startsumm
FROM account_statements stm
,account a
WHERE a.id = stm.account_id
AND a.account = v_account
AND stm.statement_date = v_statement_date;
v_balance_summ := v_startsumm +
get_credit_on_period(v_startdate, v_date, v_account) -
get_debet_on_period(v_startdate, v_date, v_account);
ELSE
v_balance_summ := get_credit_on_period(v_startdate, v_date, v_account) -
get_debet_on_period(v_startdate, v_date, v_account);
END IF;
RETURN v_balance_summ;
END;
You are using a reserved keyword as an alias
FROM account_statements as, account a
as has to be changed to something else since it is part of the SQL language.

Error in Initializing oracle collection

I have following self contained code which gives following error. I don't seem to find where do I have to initialize the collection.
create or replace type address_type is object
(
address_line varchar2(100),
city varchar2(100),
state varchar2(100)
)
/
create or replace type emp_rec_type is object
(
emp_name varchar2(100),
addr_rec address_type
)
/
create or replace type emp_arr is table of emp_rec_type
/
create table employees
(
emp_name varchar2(100),
addr_rec address_type
)
/
insert into employees values ( 'dave', address_type ( '30 br','br','nj'));
commit;
-- Create a function to return an array
create or replace function get_emp_rec ( p_name in varchar2 )
return
emp_arr
is
i integer;
l_arr emp_arr := emp_arr();
begin
i := 1;
l_arr.extend;
l_arr(l_arr.last).emp_name := 'a';
l_arr(l_arr.last).addr_rec := address_type ( 'a','b','c');
return l_arr;
end;
/
select emp_name from table ( get_emp_rec( 'dave' ))
* ERROR at line 1: ORA-06530: Reference to uninitialized composite ORA-06512: at "DBADMIN.GET_EMP_REC", line
22
You have to initialize emp_rec_type in the collection.
These two line
l_arr(l_arr.last).emp_name := 'a';
l_arr(l_arr.last).addr_rec := address_type ( 'a','b','c');
replace with this linie
l_arr(l_arr.last) := emp_rec_type('a', address_type ( 'a','b','c'));

Retrieve Oracle Metadata with Internal Data

I am trying to write a PL/SL Procedure which uses both meta data and internal data of a table.It is like:
table1 (ABC varchar2(50),wsx varchar2(50));
table2 (ABC number(50),dv varchar2(50));
table3 (ABC varchar2(10),wsds varchar2(50));
table4 (ABC varchar2(20),wfsdg varchar2(50));
table5 (ABC number(50),wsxsfd varchar2(50));
All five tables have one column with same name 'ABC'.
Suppose table1 has 3 rows like
('JOHN.TEDA','avdv'),('MARK.LEE','fesf'),('JOHN.DEA','fwfd') and other table also have any data like this.
Now using column name as an input('ABC') i should get output as
attached.
we can get column info from user_tab_columns.
Max length means max length of existing data in column ::
select max(length(ABC)) from table1
I am getting problem in joining both
Tables are not referential.
I have tried to replicate the scenario as mentioned by you with the use of PIPELINED function in Oracle. Hope this helps.
CREATE OR REPLACE TYPE fun_obj
IS
OBJECT
(
tab_name VARCHAR2(100),
colname VARCHAR2(100),
datatyp VARCHAR2(100),
datlen NUMBER,
nullable VARCHAR2(1),
LEN NUMBER );
/
CREATE OR REPLACE
FUNCTION test_max_count(
colname IN VARCHAR2)
RETURN fun_tab PIPELINED
AS
tab fun_obj:=fun_obj(NULL,NULL,NULL,NULL,NULL,NULL);
lvlen NUMBER;
BEGIN
FOR I IN
(SELECT DISTINCT table_name,
OWNER,
COLUMN_NAME,
DATA_TYPE,
DATA_LENGTH,
NULLABLE,
NULL AS MAX_LEN
FROM all_tab_columns
WHERE column_name = colname
)
LOOP
tab.tab_name:=i.table_name;
tab.colname :=i.COLUMN_NAME;
tab.datatyp :=i.DATA_TYPE;
tab.datlen :=i.DATA_LENGTH;
tab.nullable:=i.NULLABLE;
EXECUTE IMMEDIATE 'SELECT MAX(LENGTH('||i.column_name||')) FROM '||I.OWNER||'.'||I.TABLE_NAME INTO lvlen;
tab.len:=lvlen;
PIPE ROW(tab);
END LOOP;
END;
/
------------------------------------To Execute----------------------------------
SELECT * FROM TABLE(test_max_count('abc'));
CREATE OR REPLACE PROCEDURE test2 ( p_column_name IN varchar )
IS
CURSOR GET_DATA (COL VARCHAR )IS
SELECT TABLE_NAME ,COLUMN_NAME,DATA_TYPE,DATA_LENGTH,NULLABLE
FROM
user_tab_columns where COLUMN_NAME = COL;
a_table varchar2(50);
B_COL varchar2(50);
a_max varchar2(50);
BEGIN
FOR C IN GET_DATA(p_column_name) LOOP
a_table := c.table_name;
B_COL := C.COLUMN_NAME;
EXECUTE IMMEDIATE 'SELECT MAX(LENGTH('||B_COL||')) FROM '||a_table into a_max ;
insert into received_Data values (c.table_name,C.COLUMN_NAME,C.DATA_TYPE,C.DATA_LENGTH,C.NULLABLE,a_max);
END LOOP;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR (-20001,
p_column_name || ':$:' || SQLERRM, TRUE) ;
END test2;
/
CREATE TABLE RECEIVED_DATA
( TABLE_NAME VARCHAR2(50 BYTE),
COLUMN_NAME VARCHAR2(50 BYTE),
DATA_TYPE VARCHAR2(50 BYTE),
DATA_LENGTH VARCHAR2(50 BYTE),
IS_NULL VARCHAR2(50 BYTE),
MAX_LENGTH VARCHAR2(50 BYTE));
You cannot achieve this directly with SQL. You need PL/SQL + Execute Immediate to do the job.
create a table with your expected columns
then use for each loop to count the max length on each column.

Return an array in oracle stored procedure

I have a Stored procedure which takes 2 input parameter and 6 out. One of the out parameter returns more than one record. Is there any way to declare it as an array hence I don't have to declare a cursor. Here is the code,
CREATE OR REPLACE PROCEDURE SP_GET_USER_DETAILS
(
P_ID IN OUT VARCHAR2
, P_USER_NAME IN OUT VARCHAR2
, P_USER_TYPE OUT VARCHAR2
, P_EMPLOYEE_ID OUT VARCHAR2
, P_LICENSE_NO OUT VARCHAR2
, P_PHONE_NO OUT VARCHAR2
) IS
BEGIN
SELECT ACCT.ID, ACCT.USERNAME, ACCT.EMPLOYEE_ID, ACCT.LICENSE_NO,
ADDRESS.PHONE_NO INTO P_ID, P_USER_NAME, P_EMPLOYEE_ID, P_LICENSE_NO, P_PHONE_NO
FROM PROVIDER_ACCT ACCT
LEFT OUTER JOIN EMP_ADDRESS ADDRESS ON ACCT.ID=ACCT.ID
END SP_GET_PRISON_USER_DETAILS;
The problem is ADDRESS.PHONE_NO alone returns multiple rows. Is there any way to declare it as an array and get this working ? Thanks in advance.
If its not possible, could you please explain how to do it using a ref cursor ?
Use a collection:
CREATE OR REPLACE PROCEDURE SP_GET_USER_DETAILS
(
P_ID IN OUT VARCHAR2
, P_USER_NAME IN OUT VARCHAR2
, P_USER_TYPE OUT VARCHAR2
, P_EMPLOYEE_ID OUT VARCHAR2
, P_LICENSE_NO OUT VARCHAR2
, P_PHONE_NO OUT SYS.ODCIVARCHAR2LIST
)
IS
BEGIN
SELECT ID,
USERNAME,
EMPLOYEE_ID,
LICENSE_NO
INTO P_ID,
P_USER_NAME,
P_EMPLOYEE_ID,
P_LICENSE_NO
FROM PROVIDER_ACCT;
SELECT ADDRESS.PHONE_NO
BULK COLLECT INTO P_PHONE_NO
FROM PROVIDER_ACCT ACCT
LEFT OUTER JOIN EMP_ADDRESS ADDRESS
ON ACCT.ID = ADDRESS.ID;
END SP_GET_PRISON_USER_DETAILS;
/
Note: you haven't specified a where clause in your question - you probably want to add one in.