passing variable value in pl/sql query block - sql

I struck up with one issue. please need your expertise..
PL/SQL block contain a variable p_user_id varchar(50)
The value set to below value
p_user_id := '101,102,103';
I have query like below in PL/SQl block
select Count(*) into v_count
from users
where user_id not in (p_user_id);
Every time when I call this PL/SQL block v_count value is same i.e. total no of records in users table. Not In clause not working properly. Please help.

Using LIKE string comparison:
DECLARE
p_user_id VARCHAR2(200) := '101,102,103';
v_count INT;
BEGIN
SELECT Count(*)
INTO v_count
FROM users
WHERE ',' || p_user_id || ',' NOT LIKE '%,' || user_id || ',%';
END;
/
Using collections:
CREATE OR REPLACE TYPE intlist IS TABLE OF INT;
/
DECLARE
p_user_id INTLIST := INTLIST( 101, 102, 103 );
v_count INT;
BEGIN
SELECT Count(*)
INTO v_count
FROM users
WHERE user_id NOT MEMBER OF p_user_id;
END;
/

There are no rows in the users table where user_id has the value '101,102,103'.
If you want to split a string into a set of separate values, you have to do a bit more than just use it in a not in expression.
Some possible approaches:
declare
p_csvlist varchar2(100) := '2002, 7369, 7499, 7902, 7934';
v_count integer;
begin
select count(*) into v_count
from emp e
where e.empno in
( select extractvalue(xt.column_value,'e')
from table(xmlsequence
( extract
( xmltype('<coll><e>' || replace(p_csvlist,',','</e><e>') || '</e></coll>')
, '/coll/*') )) xt );
dbms_output.put_line(v_count || ' rows');
end;
or this
declare
p_csvlist varchar2(100) := '2002, 7369, 7499, 7902, 7934';
v_count integer;
begin
select count(*) into v_count
from emp e
where e.empno in
( select regexp_substr(p_csvlist, '[^,]+',1,rownum)
from dual
connect by rownum <= length(p_csvlist) - length(replace(p_csvlist,',')) );
dbms_output.put_line(v_count || ' rows');
end;
or this (only works with numeric values):
declare
p_csvlist varchar2(100) := '2002, 7369, 7499, 7902, 7934';
v_count integer;
begin
select count(*) into v_count
from emp e
where e.empno in
( select to_number(xt.column_value)
from xmltable(p_csvlist) xt );
dbms_output.put_line(v_count || ' rows');
end;
Examples are from my FAQ article:
www.williamrobertson.net/documents/comma-separated.html

You can use sys.DBMS_DEBUG_VC2COLL --collection datatype provided by Oracle itself
DECLARE
p_user_id sys.DBMS_DEBUG_VC2COLL := sys.DBMS_DEBUG_VC2COLL( 101, 102, 103 );
v_count INT;
BEGIN
SELECT Count(*)
INTO v_count
FROM users
WHERE user_id not in(select * from table(p_user_id));
END;
/

Related

PL/SQL - The function deosn't return multiple rows

I wanted to select 10 highly paid employees from the "employees" table, but the function returnes only 1 row. How to get multiple rows in this case? My subquery for selecting employees works well, but when I call function it returns 1 row.
This is my code:
CREATE OR REPLACE FUNCTION f_sal
RETURN Varchar2
IS cursor c_emp is (select first_name, last_name from (select first_name, last_name, row_number()
over(order by salary desc) as ranking from employees) where ranking <= 10);
v_first employees.first_name%type;
v_last employees.last_name%type;
begin
open c_emp;
fetch c_emp into v_first, v_last;
close c_emp;
return v_first || ' ' || v_last;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
dbms_output.put_line('Error');
when others then dbms_output.put_line('Other Error');
END;
select f_sal from dual;
Option 1: Use a collection
CREATE FUNCTION f_sal
RETURN SYS.ODCIVARCHAR2LIST
IS
v_names SYS.ODCIVARCHAR2LIST;
BEGIN
SELECT first_name || ' ' || last_name
BULK COLLECT INTO v_names
FROM employees
ORDER BY salary DESC
FETCH FIRST 10 ROWS ONLY;
return v_names;
END;
/
Then:
SELECT * FROM TABLE(f_sal);
Option 2: Use a pipelined function and iterate over a cursor into a collection
CREATE OR REPLACE FUNCTION f_sal
RETURN SYS.ODCIVARCHAR2LIST PIPELINED
IS
BEGIN
FOR n IN (
SELECT first_name || ' ' || last_name AS name
FROM employees
ORDER BY salary DESC
FETCH FIRST 10 ROWS ONLY
)
LOOP
PIPE ROW (n.name);
END LOOP;
END;
/
Then:
SELECT * FROM TABLE(f_sal);
Option 3: Return a cursor
CREATE FUNCTION f_sal
RETURN SYS_REFCURSOR
IS
v_names SYS_REFCURSOR;
BEGIN
OPEN v_names FOR
SELECT first_name || ' ' || last_name AS name
FROM employees
ORDER BY salary DESC
FETCH FIRST 10 ROWS ONLY;
return v_names;
END;
/
Then:
DECLARE
v_names SYS_REFCURSOR := f_sal();
v_name VARCHAR2(100);
BEGIN
LOOP
FETCH v_names INTO v_name;
EXIT WHEN v_names%NOTFOUND;
DBMS_OUTPUT.PUT_LINE( v_name );
END LOOP;
END;
/
db<>fiddle here

How to use dynamic table of every month in the insert statement while one static value we are passing in one column

In one Stored proc, I used insert into statement i declare one variable as v_tbl_name which is dynamic. I am going to use this dynamic table in insert into block while we are passing one static value in one column but getting error. Here is the sample code-
declare
v_tbl_name varchar2(5) := NULL;
v_gen_tbl constant varchar2(50) := 'arch_tbl_mon'; --table name
v_sql varchar2(10000);
begin
v_tbl_name := v_gen_tbl || '_' || TO_CHAR(SYSDATE, 'MON');
v_sql := 'insert into tbl --- another table going to insert
(select ''abc'' as col1, ........
from '||v_tbl_name||');'
execute immediate v_sql;
commit;
end;
Here 'abc' static value as column. After executing the above code, It is generating an err like Encountered the symbol "Execute".
Do it this way:
declare
v_tbl_name varchar2(5) := NULL;
v_gen_tbl constant varchar2(50) := 'arch_tbl_mon'; --table name
v_sql varchar2(1000);
begin
v_tbl_name := v_gen_tbl || '_' || TO_CHAR(SYSDATE, 'MON');
v_sql:= 'insert into tbl --- another table going to insert
select 'abc' as col1, ........
from '||v_tbl_name;
execute immediate v_sql;
commit;
end;
Demo:
declare
v_tbl_name varchar2(5) := NULL;
v_gen_tbl constant varchar2(50) := 'arch_tbl_mon'; --table
v_sql varchar2(1000);
begin
v_tbl_name := 'DEPT';
v_sql :='insert into tbl select * from '||v_tbl_name;
execute immediate v_sql;
end;
Exec:
SQL> select * from tbl;
DEPTNO DNAME LOC
---------- -------------------- --------------------
1 XXX YYY
Edit:
Here 'abc' static value as column so that's why not able to execute
the stored proc. It is generating an err like Encountered the symbol '
abc '.
See below demo:
Table tbl is being populated with static value abc from select statement:
SQL> select * from tbl;
DEPTNO DNAME LOC B
---------- -------------------- -------------------- ---
After Execution:
declare
v_tbl_name varchar2(5) := NULL;
v_gen_tbl constant varchar2(50) := 'arch_tbl_mon'; --table
v_sql varchar2(1000);
begin
v_tbl_name := 'DEPT';
v_sql :='insert into tbl (deptno,dname,loc,b) select a.*, ''abc'' from '||v_tbl_name||' a';
execute immediate v_sql;
Commit;
end;
OUTPUT
SQL> SELECT * FROM TBL;
DEPTNO DNAME LOC B
---------- -------------------- -------------------- ---
100 Executive USA abc
Final Edit:
You didnot follow what I showed to you hence you landed into the problem. Follow my inline comments. Please replace the column name where mentioned and it would be resolved.
declare
v_tbl_name varchar2(5) := NULL;
v_gen_tbl constant varchar2(50) := 'arch_tbl_mon'; --table name
v_sql varchar2(10000);
begin
v_tbl_name := v_gen_tbl || '_' || TO_CHAR(SYSDATE, 'MON');
v_sql := 'insert into tbl(col1,.....<all columns>) --- another table going to insert
select ''abc'' as col1, a.col2........<other columns a.<columns>
from '||v_tbl_name||' a';
execute immediate v_sql;
commit;
end;

How do I enter the data in this table in the insert?

I am new in pl / sql , I want to enter this data in the insert someone help me please?
I do not know how to enter this data in the insert without making a join
I can not find the way to enter the data in the insert
these SELECT PORCENTAJE
FROM RANGO_SUBIDA_COMISION;
VARIABLE B_ANIO NUMBER;
EXECUTE :B_ANIO := 042018;
DECLARE
V_EMPLEADO_ID EMPLEADOS.EMPLEADO_ID%TYPE;
V_NOMBRE EMPLEADOS.NOMBRE%TYPE;
V_APELLIDO EMPLEADOS.APELLIDO%TYPE;
V_VALOR_COMISION COMISIONES.VALOR_COMISION%TYPE;
V_SUELDO EMPLEADOS.SUELDO%TYPE;
BEGIN
FOR R IN
(
SELECT DISTINCT E.EMPLEADO_ID , E.NOMBRE , E.APELLIDO , C.VALOR_COMISION , E.SUELDO
FROM EMPLEADOS E JOIN COMISIONES C
ON (C.EMPLEADO_ID = E.EMPLEADO_ID)
)
LOOP
V_EMPLEADO_ID := R.EMPLEADO_ID;
V_NOMBRE := R.NOMBRE;
V_APELLIDO := R.APELLIDO;
V_VALOR_COMISION := R.VALOR_COMISION;
V_SUELDO := R.SUELDO;
INSERT INTO INFORME_SUBIDA_COMISION
VALUES ( :B_ANIO ,V_EMPLEADO_ID , V_NOMBRE , V_APELLIDO , V_VALOR_COMISION , V_SUELDO );
END LOOP;
END;
I'm not quite sure what your question is. But I don't understand why you are using a cursor for this. Why not just execute a query like this?
INSERT INTO INFORME_SUBIDA_COMISION (ANIO, EMPLEADO_ID, NOMBRE, APELLIDO, VALOR_COMISION, SUELDO)
SELECT DISTINCT :B_ANIO, E.EMPLEADO_ID, E.NOMBRE, E.APELLIDO,
C.VALOR_COMISION, E.SUELDO
FROM EMPLEADOS E JOIN COMISIONES C
ON C.EMPLEADO_ID = E.EMPLEADO_ID;
Note: I'm guessing what the column names are in INFORME_SUBIDA_COMISION, but you should explicitly list them.
you can use this insert in Bulk Mode if you have huge data to insert
SET SERVEROUTPUT ON
/*use this script to insert data in bulk mode.*/
DECLARE
TYPE array IS
TABLE OF informe_subida_comision%rowtype;
v_data array;
dml_errors EXCEPTION;
PRAGMA exception_init ( dml_errors,-24381 );
CURSOR l_cur IS
SELECT DISTINCT
e.empleado_id,
e.nombre,
e.apellido,
c.valor_comision,
e.sueldo
FROM
empleados e
JOIN comisiones c ON (
c.empleado_id = e.empleado_id
);
v_error_count NUMBER;
v_err VARCHAR2(500);
v_err_indx NUMBER;
BEGIN
OPEN l_cur;
LOOP
BEGIN
FETCH l_cur BULK COLLECT INTO v_data LIMIT 1000;
FORALL i IN 1..v_data.count SAVE EXCEPTIONS
--insert data into the table
INSERT INTO informe_subida_comision VALUES v_data ( i );
-- log any dml errors
EXCEPTION
WHEN dml_errors THEN
v_error_count := SQL%bulk_exceptions.count;
FOR i IN 1..v_error_count LOOP
v_err_indx := SQL%bulk_exceptions(i).error_index;
v_err := sqlerrm(-SQL%bulk_exceptions(i).error_code);
dbms_output.put_line(v_data(v_err_indx).empleado_id
|| ''
|| v_err);
END LOOP;
END;
COMMIT;
EXIT WHEN l_cur%notfound;
END LOOP;
CLOSE l_cur;
END;

Cursor inside procedure body

I am trying to declare a cursor inside procedure body.
I know it is supposed to be done in the declare block but the table the cursor refers is created inside the procedure body.
--TABLE MAY OR MAY NOT BE PRESENT PRIOR TO PROCEDURE EXECUTION
SELECT COUNT(*)
INTO ln_cnt
FROM User_Tables
WHERE table_name = 'TMP$UOM_COMBO_GEN';
IF ln_cnt > 0 THEN
EXECUTE IMMEDIATE ' CREATE TABLE TMP$UOM_COMBO_GEN (UOM_ID VARCHAR2(20 BYTE), HIER_CODE VARCHAR2(20 BYTE),NODE_CODE VARCHAR2(200 BYTE))';
END IF;
CURSOR C_HIER
IS
SELECT DISTINCT HIER_CODE FROM TMP$UOM_COMBO_GEN WHERE UOM_ID=P_UOM_ID;
FOR HIER IN C_HIER
LOOP
IF C_HIER%ROWCOUNT = 1 THEN
LV_SQL2 := '(SELECT UOM_ID, NODE_CODE '||HIER.HIER_CODE||' FROM TMP$UOM_COMBO_GEN WHERE UOM_ID='''||P_UOM_ID||''' AND HIER_CODE='''||HIER.HIER_CODE||''')'||HIER.HIER_CODE;
LV_SORT := ' ORDER BY '||HIER.HIER_CODE||'';
LV_SQL := 'SELECT * FROM ' || LV_SQL2;
ELSE
LV_SQL3 := ' LEFT OUTER JOIN(SELECT NODE_CODE '||HIER.HIER_CODE||' FROM TMP$UOM_COMBO_GEN WHERE UOM_ID='''||P_UOM_ID||''' AND HIER_CODE='''||HIER.HIER_CODE||''')'||HIER.HIER_CODE ||' ON 1=1';
LV_SORT := LV_SORT||','||HIER.HIER_CODE||'';
LV_SQL := LV_SQL || LV_SQL3;
END IF;
END LOOP;
I am getting the following error.
Error(17,10): PLS-00103: Encountered the symbol "C_HIER" when expecting one of the following: := . ( # % ;
Well a table once created is stored in database and u can refer it from wherever u want to refer in the schema .
Also there a change may be required in your code in the following part
SELECT COUNT(*)
INTO ln_cnt
FROM User_Tables
WHERE table_name = 'TMP$UOM_COMBO_GEN';
IF ln_cnt > 0 THEN -- means only if table exists u want to create the table which will
--throw an exception if table is already there ,
--so better equate it to 0
EXECUTE IMMEDIATE ' CREATE TABLE TMP$UOM_COMBO_GEN (UOM_ID VARCHAR2(20 BYTE), HIER_CODE VARCHAR2(20 BYTE),NODE_CODE VARCHAR2(200 BYTE))';
END IF;
Now , if u really have a requirement to create a new table every time some condition is true/false and u then want to select the table in a cursor do something like following by using reference cursors
create or replace procedure abc(Table_name varchar2 , param_list varchar2 , where_clause varchar2) is
c_hier sys_refcursor ;
LV_SQL2 varchar2(2000) ;
LV_SORT varchar2(2000) ;
LV_SQL varchar2(2000) ;
LV_SQL3 varchar2(2000) ;
begin
SELECT COUNT(*)
INTO ln_cnt
FROM User_Tables
WHERE table_name = Table_name; -- Use any table here
IF ln_cnt = 0 THEN
EXECUTE IMMEDIATE ' CREATE TABLE '||Table_name||' '||param_list;
END IF;
open c_hier for 'SELECT DISTINCT '||param_list||' FROM '||table_name||' '||where_clause;
FOR HIER IN C_HIER
LOOP
IF C_HIER%ROWCOUNT = 1 THEN
LV_SQL2 := '(SELECT UOM_ID, NODE_CODE '||HIER.HIER_CODE||' FROM TMP$UOM_COMBO_GEN WHERE UOM_ID='''||P_UOM_ID||''' AND HIER_CODE='''||HIER.HIER_CODE||''')'||HIER.HIER_CODE;
LV_SORT := ' ORDER BY '||HIER.HIER_CODE||'';
LV_SQL := 'SELECT * FROM ' || LV_SQL2;
ELSE
LV_SQL3 := ' LEFT OUTER JOIN(SELECT NODE_CODE '||HIER.HIER_CODE||' FROM TMP$UOM_COMBO_GEN WHERE UOM_ID='''||P_UOM_ID||''' AND HIER_CODE='''||HIER.HIER_CODE||''')'||HIER.HIER_CODE ||' ON 1=1';
LV_SORT := LV_SORT||','||HIER.HIER_CODE||'';
LV_SQL := LV_SQL || LV_SQL3;
END IF;
END LOOP;

Any alternatives to using cursor in SQL procedure in Oracle 10g?

I give the SQL few inputs and I need to get all the ID's and their count that doesn't satisfy the required criteria.
I would like to know if there are there any alternatives to using cursor.
DECLARE
v_count INTEGER;
v_output VARCHAR2 (1000);
pc table1%ROWTYPE;
unmarked_ids EXCEPTION;
dynamic_sql VARCHAR (5000);
cur SYS_REFCURSOR;
id pp.id%TYPE;
pos INTEGER;
BEGIN
v_count := 0;
SELECT *
INTO pc
FROM table1
WHERE id = '&ID';
DBMS_OUTPUT.ENABLE;
dynamic_sql :=
'SELECT ID from pp
WHERE ( TO_CHAR(cdate, ''yyyy/mm/dd'') =
TO_CHAR (:a, ''yyyy/mm/dd''))
AND aid IN (SELECT aid FROM ppd WHERE TO_CHAR(cdate, ''yyyy/mm/dd'') =
TO_CHAR (:b, ''yyyy/mm/dd'')
AND cid = :c )
AND cid <> :d';
OPEN cur FOR dynamic_sql USING pc.cdate, pc.cdate, pc.id, pc.id;
LOOP
FETCH cur INTO id;
EXIT WHEN cur%NOTFOUND;
v_count := v_count + 1;
DBMS_OUTPUT.PUT_LINE (' Id:' || id);
END LOOP;
CLOSE cur;
IF (v_count > 0)
THEN
DBMS_OUTPUT.PUT_LINE ( 'Count: ' || v_count || ' SQL: ' || dynamic_sql);
RAISE unmarked_ids;
END IF;
DBMS_OUTPUT.PUT_LINE('SQL ended successfully');
EXCEPTION
WHEN unmarked_ids
THEN
DBMS_OUTPUT.put_line (
'Found ID's that not marked with the current id.');
WHEN NO_DATA_FOUND
THEN
DBMS_OUTPUT.put_line (
'No data found in table1 with the current id ' || '&ID');
END;
There are bind variables in the query. One of them is date, there are three more.
The count and ID's are required to be shown which will later be reported.
You could store the rowid in a temporary table along with an index value (0...n) and then use a while loop to go through the index values and join to the real table using the rowid.