Update big text in a CLOB column in Oracle 9 - oracle

I would update this text
try1
try2
try3
try4
How can i set syntax for do a update in oracle 9?
update table set field = 'try1
try2
try3
try4'
where id = 1
without text wrap?
I have a text really very big from update.

You can try to build the string with anonymous PL/SQL block.
This is just an example that you need to adapt:
SQL> set serveroutput on
SQL>
SQL> desc lob_table;
Name Null? Type
----------------------------------------- -------- ----------------------------
KEY_VALUE NUMBER
C_COL CLOB
SQL>
SQL> declare
2 v varchar2(10000);
3 begin
4 v:='try1' || rpad (' ', 9000, ' ') || 'try2';
5 dbms_output.put_line('length(v) = ' || length(v));
6 update lob_table set c_col = v where key_value=12;
7 end;
8 /
length(v) = 9008
PL/SQL procedure successfully completed.
SQL> show errors
No errors.
If the above code does not work in Oracle 9, you can try to use DBMS_LOB.
Example:
SQL> --
SQL> set serveroutput on
SQL> --
SQL> DECLARE
2 value VARCHAR2(10000);
3 amount binary_integer;
4 lob_loc CLOB;
5 BEGIN
6 value :='try1' || rpad (' ', 9000, ' ') || 'try2';
7 dbms_output.put_line('length(v) = ' || length(value));
8 SELECT c_col INTO lob_loc
9 FROM lob_table
10 WHERE key_value = 12 FOR UPDATE;
11 amount := length(value);
12 dbms_lob.write (lob_loc, amount, 1, value);
13 COMMIT;
14 END;
15 /
length(v) = 9008
PL/SQL procedure successfully completed.
SQL> show errors
No errors.
SQL>

Related

How can ı create table with function?

STEP 1:
Creating a new table and adding a record to the database with the help of the procedure
• the function will take 3 parameters string ,string and number,
• The first parameter represents the name of the table, and the other two parameters represent the records to be added to the created table.
Step 2:
As seen in the screenshot below; As described above, when the function runs, a table named 'mytable' will be created in the database with the first parameter, and other parameters will be added to the created table as a record.
enter image description here
Why function? They are supposed to return some value, not to perform DDL. Therefore, a procedure it is, using dynamic SQL as otherwise it won't work.
SQL> CREATE OR REPLACE PROCEDURE p_test (par_table_name IN VARCHAR2,
2 par_str IN VARCHAR2,
3 par_num IN NUMBER)
4 IS
5 l_cnt NUMBER;
6 BEGIN
7 SELECT COUNT (*)
8 INTO l_cnt
9 FROM user_tables
10 WHERE UPPER (table_name) = UPPER (par_table_name);
11
12 IF l_cnt = 0
13 THEN
14 EXECUTE IMMEDIATE 'create table '
15 || par_table_name
16 || ' as select '
17 || DBMS_ASSERT.enquote_literal (par_str)
18 || ' col_string, '
19 || par_num
20 || ' col_number from dual';
21 ELSE
22 raise_application_error (-20000,
23 'Object with that name already exists');
24 END IF;
25 END;
26 /
Procedure created.
Testing:
SQL> exec p_test('little', 'foot', 20);
PL/SQL procedure successfully completed.
SQL> select * from little;
COL_ COL_NUMBER
---- ----------
foot 20
SQL>

plsql procedure doesn't do what i programmed it to do

my procedure is supposed to change 2 values but when i call it it shows the same values entered
code
CREATE OR REPLACE PROCEDURE commande_remise(pourcentage_rem IN Decimal,
c_client IN commande.code_client%type,
c_reg IN commande.reglement%type,
c_montantht IN OUT commande.montant_ht%type,
c_montantttc IN OUT commande.montant_ttc%type)
IS
c_ref commande.ref_commande%type;
BEGIN
SELECT COUNT(ref_commande) INTO c_ref FROM commande;
c_ref := c_ref + 1;
c_montantht := c_montantht-c_montantht*pourcentage_rem;
c_montantttc := c_montantttc-c_montantttc*pourcentage_rem;
INSERT INTO commande(ref_commande, code_client, reglement, montant_ht, montant_ttc)
VALUES(c_ref, c_client, c_reg, c_montantht, c_montantttc);
COMMIT;
END commande_remise;
/
procedure call
DECLARE
c_remise DECIMAL :=0.2;
c_code commande.code_client%type :=2;
c_reglement commande.reglement%type :='oui';
c_montantht commande.montant_ht%type :=3080.12;
c_montantttc commande.montant_ttc%type :=3530.56;
c_com commande%rowtype;
CURSOR c_cur IS SELECT ref_commande, code_client, reglement, montant_ht, montant_ttc FROM commande;
BEGIN
commande_remise(c_remise, c_code, c_reglement, c_montantht, c_montantttc);
OPEN c_cur;
LOOP
FETCH c_cur INTO c_com;
exit when c_cur%notfound;
dbms_output.put_line(c_com.ref_commande || ' ' || c_com.code_client || ' ' || c_com.reglement || ' ' || c_com.montant_ht || ' ' || c_com.montant_ttc);
END LOOP;
CLOSE c_cur;
END;
/
the values are c_montantht and c_montantttc
the result is the third:, please help.
Works for me (I'll tell you a secret later).
Table first:
SQL> create table commande
2 (ref_commande number,
3 code_client number,
4 reglement varchar2(10),
5 montant_ht number,
6 montant_ttc number);
Table created.
SQL>
Procedure:
SQL> CREATE OR REPLACE PROCEDURE commande_remise
2 (pourcentage_rem IN NUMBER,
3 c_client IN commande.code_client%type,
4 c_reg IN commande.reglement%type,
5 c_montantht IN OUT commande.montant_ht%type,
6 c_montantttc IN OUT commande.montant_ttc%type)
7 IS
8 c_ref commande.ref_commande%type;
9 BEGIN
10 SELECT COUNT(ref_commande) INTO c_ref FROM commande;
11 c_ref := c_ref + 1;
12 dbms_output.put_line('pourcentage_rem = ' || pourcentage_rem);
13 c_montantht := c_montantht - c_montantht * pourcentage_rem;
14 c_montantttc := c_montantttc - c_montantttc * pourcentage_rem;
15
16 INSERT INTO commande
17 (ref_commande, code_client, reglement, montant_ht, montant_ttc)
18 VALUES
19 (c_ref, c_client, c_reg, c_montantht, c_montantttc);
20 END commande_remise;
21 /
Procedure created.
SQL>
Anonymous PL/SQL block:
SQL> set serveroutput on;
SQL> DECLARE
2 c_remise NUMBER :=0.2;
3 c_code commande.code_client%type :=2;
4 c_reglement commande.reglement%type :='oui';
5 c_montantht commande.montant_ht%type :=3080.12;
6 c_montantttc commande.montant_ttc%type :=3530.56;
7 c_com commande%rowtype;
8 CURSOR c_cur IS SELECT ref_commande, code_client, reglement, montant_ht, montant_ttc FROM commande;
9 BEGIN
10 commande_remise(c_remise, c_code, c_reglement, c_montantht, c_montantttc);
11 OPEN c_cur;
12 LOOP
13 FETCH c_cur INTO c_com;
14 exit when c_cur%notfound;
15 dbms_output.put_line(c_com.ref_commande || ' ' || c_com.code_client
16 || ' ' || c_com.reglement || ' ' || c_com.montant_ht
17 || ' ' || c_com.montant_ttc);
18 END LOOP;
19 CLOSE c_cur;
20 END;
21 /
pourcentage_rem = ,2
1 2 oui 2464,096 2824,448
PL/SQL procedure successfully completed.
SQL>
Table contents:
SQL> select * From commande;
REF_COMMANDE CODE_CLIENT REGLEMENT MONTANT_HT MONTANT_TTC
------------ ----------- ---------- ---------- -----------
1 2 oui 2464,096 2824,448
SQL>
Looks OK, right?
The secret: don't use DECIMAL in
procedure's parameter declaration: pourcentage_rem IN Decimal
anonymous PL/SQL block's variable declaration: c_remise DECIMAL :=0.2;
Use NUMBER instead. Because, if you use DECIMAL, then procedure's line #12 displays
pourcentage_rem = 0
so - when you subtract something that is multiplied by zero, you subtract zero and get the input value itself.

How to Escape dot(.) In a like expression in Oracle PL/SQL Statement

In a stored procedure I am filtering out list of employees whose role is SUPER.ADMIN
In such a case I used like expression as below
Emp.Role_nm Like ''''||p_rolenm||''''
p_rolenm I have mentioned in stored procedure as VARCHAR2
When ever I pass a value to p_rolenm as SUPER.ADMIN it's throwing error as identifier SUPER.ADMIN is not declared.
How I can escape . (Dot) in PL/SQL statements?
Why do you use that many single quotes? You don't need any (at least, I think so):
Sample data:
SQL> create table test (id number, role varchar2(20));
Table created.
SQL> insert into test
2 select 1, 'CLERK' from dual union all
3 select 2, 'SUPER.ADMIN' from dual;
2 rows created.
SQL> select * from test;
ID ROLE
---------- --------------------
1 CLERK
2 SUPER.ADMIN
SQL> set serveroutput on;
Procedure (anonymous, though, but that doesn't matter):
SQL> declare
2 p_rolenm varchar2(20) := 'SUPER.ADMIN';
3 l_id number;
4 begin
5 select id into l_id
6 from test
7 where role = p_rolenm;
8
9 dbms_output.put_line('l_id = ' || l_id);
10 end;
11 /
l_id = 2
PL/SQL procedure successfully completed.
SQL>
If you need like, then
SQL> declare
2 p_rolenm varchar2(20) := 'PER.ADM'; --> I changed this ...
3 l_id number;
4 begin
5 select id into l_id
6 from test
7 where role like '%' || p_rolenm || '%'; --> ... and this
8
9 dbms_output.put_line('l_id = ' || l_id);
10 end;
11 /
l_id = 2
PL/SQL procedure successfully completed.
SQL>
If you used dynamic SQL, then
SQL> declare
2 p_rolenm varchar2(20) := 'PER.ADM';
3 l_id number;
4 l_str varchar2(200); --> new variable for execute immediate
5 begin
6 l_str := q'[select id from test where role like '%' || :a || '%']';
7 execute immediate l_str into l_id using p_rolenm;
8
9 dbms_output.put_line('l_id = ' || l_id);
10 end;
11 /
l_id = 2
PL/SQL procedure successfully completed.
SQL>
Shortly, I don't understand what you are doing. Try to follow my examples. If it still doesn't work, post your SQL*Plus session.

Run query dynamically in stored procedure in Oracle

Select all the tables of database where column match than pass table name to next query using loop. If column name and column values matches than return true and exist for loop using a stored procedure:
CREATE OR REPLACE PROCEDURE TEST
(
NAME IN VARCHAR2 ,
ID IN NUMBER,
RE OUT SYS_REFCURSOR
) AS
BEGIN
OPEN RE FOR SELECT A.TABLE_NAME FROM
user_tables A JOIN user_tab_columns C
ON C.TABLE_NAME = A.TABLE_NAME
WHERE C.COLUMN_NAME = NAME;
FOR RE IN LOOP
v_Sql := 'SELECT COUNT(*) FROM '|| LOOP.TABLE_NAME || 'WHERE COLUMN_NAME =
ID';
EXECUTE IMMEDIATE v_Sql
IF v_Sql%ROWCOUNT > 0 THEN
return true;
EXIT
END LOOP;
END TEST;
For more understanding the problem
//Get all the tables of database where campus_id is exist in any table of
database
Campus, Class, Section (3 tables found)
Apply forloop on the records
Select count(campus_id) as total from (table name using loop) where campus_id = 1(value
pass)
if(total > 0){
Exist for loop and return true
}
else{
Again iterate the loop to next value
}
What you described doesn't make much sense. If there are several tables that contain a column you're checking and you exit the loop as soon as you find the first one, what about the rest of them?
Here's what I'd do, see if it helps. I'll create a function (not a procedure) that returns a table. In order to do that, I'll create type(s) first:
SQL> create or replace type t_record as object (tn varchar2(30), cnt number);
2 /
Type created.
SQL> create or replace type t_table as table of t_record;
2 /
Type created.
SQL>
The function:
in a cursor FOR loop I'm selecting tables that contain that column
L_STR is used to compose the SELECT statement
DBMS_OUTPUT.PUT_LINE is used to display it first, so that I could visually check whether it is correctly set or not.
if it is, I'm running it with the EXECUTE IMMEDIATE
the result is stored into a table type and returned to the caller
SQL> create or replace function f_colname
2 (par_column_name in varchar2,
3 par_column_value in varchar2
4 )
5 return t_table
6 is
7 retval t_table := t_table();
8 l_str varchar2(200);
9 l_cnt number;
10 begin
11 for cur_r in (select table_name
12 from user_tab_columns
13 where column_name = par_column_name
14 )
15 loop
16 l_str := 'select count(*) from ' || cur_r.table_name ||
17 ' where ' || par_column_name || ' = ' ||
18 chr(39) || par_column_value || chr(39);
19 -- Display l_str first, to make sure that it is OK:
20 -- dbms_output.put_line(l_str);
21 execute immediate l_str into l_cnt;
22 retval.extend;
23 retval(retval.count) := t_record(cur_r.table_name, l_cnt);
24 end loop;
25 return retval;
26 end;
27 /
Function created.
Testing:
SQL> select * from table (f_colname('DEPTNO', '10'));
TN CNT
------------------------------ ----------
TEST_201812 1
DEPT 1
EMP 3
SQL> select * from table (f_colname('ENAME', 'KING'));
TN CNT
------------------------------ ----------
EMP 1
BONUS 1
SQL>
That won't work properly for some datatypes (such as DATE) and will have to be adjusted, if necessary.
[EDIT: after you edited the question]
OK then, that's even simpler. It should still be a function (that returns a Boolean, as you said that - in case that something's being found - you want to return TRUE). Code is pretty much similar to the previous function.
SQL> create or replace function f_colname
2 (par_column_name in varchar2,
3 par_column_value in varchar2
4 )
5 return boolean
6 is
7 l_str varchar2(200);
8 l_cnt number;
9 retval boolean := false;
10 begin
11 for cur_r in (select table_name
12 from user_tab_columns
13 where column_name = par_column_name
14 )
15 loop
16 l_str := 'select count(*) from ' || cur_r.table_name ||
17 ' where ' || par_column_name || ' = ' ||
18 chr(39) || par_column_value || chr(39);
19 -- Display l_str first, to make sure that it is OK:
20 -- dbms_output.put_line(l_str);
21 execute immediate l_str into l_cnt;
22 if l_cnt > 0 then
23 retval := true;
24 exit;
25 end if;
26 end loop;
27 return retval;
28 end;
29 /
Function created.
Testing: as you can't return Boolean at SQL layer, you have to use an anonymous PL/SQL block, as follows:
SQL> declare
2 l_ret boolean;
3 begin
4 if f_colname('DEPTNO', '15') then
5 dbms_output.put_line('It exists');
6 else
7 dbms_output.put_line('It does not exist');
8 end if;
9 end;
10 /
It does not exist
PL/SQL procedure successfully completed.
SQL>

Oracle DB : get the query that triggered a particular update

I would like to monitor some field in my database ; when an SQL query updates that field with a certain value, I would like to log the query that triggered the update.
How can I do that ?
Thank you in advance!
Good question. Made me curious. I found an answer here.
A LOGMINER utility is also mentioned there. Maybe worth looking into?
SQL> CREATE OR REPLACE FUNCTION cur_sql_txt
2 RETURN CLOB
3 AS
4 v_cnt BINARY_INTEGER;
5 v_sql ORA_NAME_LIST_T;
6 v_rtn CLOB;
7 BEGIN
8 v_cnt := ora_sql_txt (v_sql);
9 FOR l_bit IN 1..v_cnt LOOP
10 v_rtn := v_rtn || RTRIM (v_sql (l_bit), CHR (0));
11 END LOOP;
12 RETURN RTRIM (v_rtn, CHR (10)) || ';';
13 END;
14 /
Function created.
SQL> CREATE OR REPLACE TRIGGER trigger_name
2 BEFORE UPDATE ON emp
3 FOR EACH ROW
4 BEGIN
5 DBMS_OUTPUT.PUT_LINE (cur_sql_txt);
6 END;
7 /
Trigger created.
SQL> SET SERVEROUTPUT ON;
SQL> UPDATE emp
2 SET empno = empno,
3 ename = ename
4 WHERE ROWNUM = 1;
UPDATE emp
SET empno = empno,
ename = ename
WHERE ROWNUM =
:"SYS_B_0";
1 row updated.
SQL>
Why don't you use the audit statement? It allows you to monitor for instance updates against a table.

Resources