I have a requirement where I need to run set of UPDATE statements in a for loop.
In the cursor there is a column called PROPERTY_ID which is a number and there are many tables
that have this number appended.
For ex: SELECT * FROM PC_ORG_EXT_111(where 111 is the property_id)
This is the code and it's throwing error.
Can anyone assist me if I'm missing something here.
SET SERVEROUTPUT ON SIZE 1000000
SET LINESIZE 1000
SET PAGESIZE 0
DECLARE
V_PROP_ID VARCHAR(200);
V_CNT NUMBER(25);
V_SQL_STRING VARCHAR2(500);
CURSOR CUR_CON
IS
SELECT * FROM PRE_CONVERSION_UNMERGE_LIST;
BEGIN
FOR REC_CON IN CUR_CON
LOOP
V_PROP_ID := 'PROPARCH.PC_ORG_EXT_' || REC_CON.PROPERTY_ID;
dbms_output.put_line('Property Table Name ' || V_PROP_ID);
EXECUTE IMMEDIATE 'select COUNT(1) from ' ||V_PROP_ID;
EXECUTE IMMEDIATE '
UPDATE SIEBEL.S_ACCNT_POSTN
SET OU_EXT_ID = ' || REC_CON.VALID_SURVIVIR_REC ||'
WHERE OU_EXT_ID = ' || REC_CON.INVALID_SURVIVOR_REC||'
AND OU_EXT_ID IN (SELECT ISAC_ROW_ID
FROM ' || V_PROP_ID || '
WHERE INTEGRATION_ID = '||REC_CON.DELPHI_ID||')
AND POSITION_ID NOT IN
(SELECT POSITION_ID
FROM SIEBEL.S_ACCNT_POSTN
WHERE OU_EXT_ID = '||REC_CON.VALID_SURVIVIR_REC||')';
END LOOP;
END;
Error says :
ORA-00933: SQL command not properly ended
ORA-06512: at line 20
Also let me know if there's a better way of doing it.
Thanks,
Please try this! - Single Quotes has to be Used with escape characters!
EXECUTE IMMEDIATE '
UPDATE SIEBEL.S_ACCNT_POSTN
SET OU_EXT_ID = ''' || REC_CON.VALID_SURVIVIR_REC ||'''
WHERE OU_EXT_ID = ''' || REC_CON.INVALID_SURVIVOR_REC||'''
AND OU_EXT_ID IN (SELECT ISAC_ROW_ID
FROM ' || V_PROP_ID || '
WHERE INTEGRATION_ID = '''||REC_CON.DELPHI_ID||''')
AND POSITION_ID NOT IN
(SELECT POSITION_ID
FROM SIEBEL.S_ACCNT_POSTN
WHERE OU_EXT_ID = '''||REC_CON.VALID_SURVIVIR_REC||''')';
Related
I am using Oracle 11g. My tables include columns like name and l_name (lowercase of name column). I am trying to iterate through all the columns in my table space to set the l_ columns to lowercase of their respective uppercase columns. Here is what I tried:
for i in (select table_name from user_tables) loop
SELECT SUBSTR(column_name,3) bulk collect into my_temp_storage FROM user_tab_columns WHERE table_name = i.table_name and column_name like 'L\_%' escape '\';
for j in (select column_name from user_tab_columns where table_name = i.table_name) loop
for k in 1..my_temp_storage.count
loop
if(j.column_name like 'L\_%' escape '\' and SUBSTR(j.column_name,3) = my_temp_storage(k)) then
DBMS_OUTPUT.PUT_LINE( 'update ' || i.table_name || ' set ' || j.column_name || ' = LOWER(' ||my_temp_storage(k)|| ') where ' || j.column_name || ' is not null');
execute immediate 'update ' || i.table_name || ' set ' || j.column_name || ' = LOWER(' ||my_temp_storage(k)|| ') where ' || j.column_name || ' is not null';
end if;
end loop;
end loop;
end loop;
I am storing all the names of columns in uppercase in my_temp_storage and updating the table with the LOWER value of the columns in my_temp_storage. This gave me an error saying:
Error report -
ORA-00900: invalid SQL statement
ORA-06512: at line 8
00900. 00000 - "invalid SQL statement"
*Cause:
*Action:
But the DBMS output seemed to be fine:
`update EMPLOYEE set L_NAME = LOWER(NAME) where L_NAME is not null`
Could you help me with the way I did or any other way it can be done?
The program could certainly be simplified:
begin
for i in (select table_name, column_name from user_tab_columns
where column_name like 'L\_%' escape '\')
loop
l_sql := 'update ' || i.table_name || ' set ' || i.column_name
|| ' = LOWER(' ||substr(i.columm_name,3)
|| ') where ' || i.column_name || ' is not null';
execute immediate l_sql;
end loop;
end;
It seems an odd database design though. Have you considered virtual columns, and/or function-based indexes, instead of manually maintained columns?
I want to execute the following command through a cursor where I have column name and column values in a different table.
However the script fails with following error message.
ORA-00933: SQL command not properly ended
execute immediate 'UPDATE EMP_DETAILS_T SET ' || EMP_REC.COLUMN_NAME || ' = ' || '''' || EMP_REC.VALUE ||''' ' || ' where employee_id = ' || ''''|| EMP_REC.EMPLOYEE_ID || ''' ' ;
entire command is as follow.
DECLARE
CURSOR CR_EMP_ATT IS
SELECT COLUMN_NAME,
VALUE,
EMPLOYEE_ID
FROM UPDATE_DATA_T
WHERE EMPLOYEE_ID = P_EMP_ID;
BEGIN
FOR CR_EMP IN CR_EMP_ATT LOOP
BEGIN
EXECUTE IMMEDIATE 'Update EMP_DETAILS_T set ' || CR_EMP.COLUMN_NAME || ' = ' ||''''||CR_EMP.VALUE||''''|| ' where employee_id = ' ||''''|| P_EMP_ID||'''';
DBMS_OUTPUT.PUT_LINE('temp table updated');
END;
END LOOP;
END;
I have placed this query in a procedure to run when needed by employee.
Your value column includes values which contain a single quote. (The ampersand isn't going to be a problem here). You can see how that breaks the statement by displaying it before execution:
dbms_output.put_line('Update EMP_DETAILS_T set ' || CR_EMP.COLUMN_NAME
|| ' = ' ||''''||CR_EMP.VALUE||''''|| ' where employee_id = '
||''''|| P_EMP_ID||'''');
... which reveals:
Update EMP_DETAILS_T set SOME_COLUMN = 'Standard & Poor's' where employee_id = 'ABC123'
... which has unbalanced single quotes; you can even see that in the syntax highlighting.
The simplest fix is to use bind variables for the two values - you can't for the column name - and pass the actual values with the USING clause:
EXECUTE IMMEDIATE 'Update EMP_DETAILS_T set ' || CR_EMP.COLUMN_NAME
|| ' = :VALUE where employee_id = :EMP_ID'
USING CR_EMP.VALUE, P_EMP_ID;
Does your value CR_EMP.VALUE contain any ' character?
That would be another reason to use bind variables, i.e.
EXECUTE IMMEDIATE 'Update EMP_DETAILS_T set '||CR_EMP.COLUMN_NAME||' = ' :val WHERE employee_id = :id'
USING CR_EMP.VALUE, P_EMP_ID;
I am using a stored procedure to insert data into a temp table using a cursor.
This procedure stores a dynamic query inside a variable to mount the insert/update command.
Here is the code(not the full query, I've cut some parts to make it easier to read):
FOR VC2 IN (SELECT C.OBJETIVO,
C.AUDITORIA ,
C.NOME,
C.PRODUTO
FROM CALCULO C)
LOOP
SELECT ' V_UPD NUMBER := 0;
SELECT (SELECT ID_TIPO_TERR
FROM ZREPORTYTD_TMP
WHERE AUDITORIA = ''' || VC2.AUDITORIA || '''
AND TERRITORIO = ''' || VC2.NOME || '''
AND PRODUTO = ''' || VC2.PRODUTO || ''')
INTO V_UPD FROM DUAL;
UPDATE ZReportYTD_TMP
SET TARGET = ' || VC2.OBJETIVO || '
WHERE AUDITORIA = ''' || VC2.AUDITORIA || '''
AND TERRITORIO = ''' || VC2.NOME || '''
AND PRODUTO = ''' || VC2.PRODUTO || ''';'
INTO V_SQL FROM DUAL;
EXECUTE IMMEDIATE (V_SQL);
END LOOP
Inside the dynamic query, in this part "SET TARGET = ' || VC2.OBJETIVO || '" the value VC2.OBJETIVO is a Number type, and it's replaced like "62481,76". In other words, this comma is making the command wrong and doesn't work.
Is there an easy way to replace the "," for "."?
Thank you very much! (:
Don't build your query by appending strings. You leave yourself open to lots of bugs and vulnerabilities, first of all SQL injection. The need to use dynamic queries doesn't justify not using bind variables. If you really need to use dynamic queries (it is not clear from your example why static update wouldn't work?!), do this instead:
FOR vc2 IN (...) LOOP
v_sql :=
'BEGIN
V_UPD NUMBER := 0;
SELECT (SELECT ID_TIPO_TERR
FROM ZREPORTYTD_TMP
WHERE AUDITORIA = :p1
AND TERRITORIO = :p2
AND PRODUTO = :p3)
INTO V_UPD FROM DUAL;
UPDATE ZReportYTD_TMP
SET TARGET = :p4
WHERE AUDITORIA = :p5
AND TERRITORIO = :p6
AND PRODUTO = :p7;
END';
EXECUTE IMMEDIATE v_sql USING VC2.AUDITORIA, VC2.NOME, VC2.PRODUTO,
VC2.OBJETIVO, VC2.AUDITORIA, VC2.NOME,
VC2.PRODUTO;
END LOOP;
Oracle will correctly bind with the appropriate type.
I don't see any need to use dynamic SQL at all.
Why not something like:
FOR VC2 IN (SELECT C.OBJETIVO,
C.AUDITORIA ,
C.NOME,
C.PRODUTO
FROM CALCULO C) LOOP
v_upd := 0;
SELECT
ID_TIPO_TERR
into
v_UPD
FROM
ZREPORTYTD_TMP
WHERE
AUDITORIA = VC2.AUDITORIA
AND TERRITORIO = VC2.NOME
AND PRODUTO = VC2.PRODUTO;
-- is v_upd used anywhere?
UPDATE
ZReportYTD_TMP
SET
TARGET = VC2.OBJETIVO
WHERE
AUDITORIA = VC2.AUDITORIA
AND TERRITORIO = VC2.NOME
AND PRODUTO = VC2.PRODUTO;
END LOOP;
I am using Oracle 11g , last couple of day I was facing problem of
execute dynamic query in oracle procedure. I did search lots off.
finally i have got solution .
-- In blow procedure we pass multiple argument at run time
-- We need reference cursor for dynamic query execution
create or replace PROCEDURE FETCH_REPORT1_NEW(IPID IN number ,CAID IN number,
ZOID IN number,CLID IN number,SDATE VARCHAR2 , EDATE
VARCHAR2,OUT_VALUE OUT VARCHAR2)
IS
l_sql varchar(200); TYPE cursor_ref IS REF CURSOR; c1
cursor_ref;
UZID transaction_data.zone_id%TYPE; OUTAGE_MINS
transaction_data.durationmin%TYPE;
BEGIN
l_sql := 'select Avg (durationmin) , zone_id ,
from transaction_data where alarm_id in (1,21,26,20) and zone_id not in(5)';
IF IPID>0 THEN
l_sql := l_sql||' and IP_ID = '||IPID;
END IF;
l_sql := l_sql||' group by (zone_id)';
open c1 for l_sql;
loop
fetch c1 into OUTAGE_MINS,UZID;
dbms_output.put_line(OUTAGE_MINS||UZID);
exit when c1%notfound;
end loop;
close c1;
END;
I want to the the number of records in all tables that match a specific name criteria. Here is the SQL I built
Declare SQLStatement VARCHAR (8000) :='';
BEGIN
SELECT 'SELECT COUNT (*) FROM ' || Table_Name || ';'
INTO SQLStatement
FROM All_Tables
WHERE 1=1
AND UPPER (Table_Name) LIKE UPPER ('MSRS%');
IF SQLStatement <> '' THEN
EXECUTE IMMEDIATE SQLStatement;
END IF;
END;
/
But I get the following error:
Error at line 1
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at line 3
Script Terminated on line 1.
How do I modify this so that it runs for all matching tables?
Update:
Based on an answer received, I tried the following but I do not get anything in the DBMS_OUTPUT
declare
cnt number;
begin
for r in (select table_name from all_tables) loop
dbms_output.put_line('select count(*) from CDR.' || r.table_name);
end loop;
end;
/
declare
cnt number;
begin
for r in (select owner, table_name from all_tables
where upper(table_name) like ('%MSRS%')) loop
execute immediate 'select count(*) from "'
|| r.owner || '"."'
|| r.table_name || '"'
into cnt;
dbms_output.put_line(r.owner || '.' || r.table_name || ': ' || cnt);
end loop;
end;
/
If you're selecting from all_tables you cannot count on having been given the grants necessary to select from the table name. You should therefore check for the ORA-00942: table or view does not exist error thrown.
As to the cause for your error: You get this error because the select statement returns a result set with more than one row (one for each table) and you cannot assign such a result set to a varchar2.
By the way, make sure you enable dbms_output with SET SERVEROUT ON before executing this block.
In Oracle 10g, is there a way to do the following in PL/SQL?
for each table in database
for each row in table
for each column in row
if column is of type 'varchar2'
column = trim(column)
Thanks!
Of course, doing large-scale dynamic updates is potentially dangerous and time-consuming. But here's how you can generate the commands you want. This is for a single schema, and will just build the commands and output them. You could copy them into a script and review them before running. Or, you could change dbms_output.put_line( ... ) to EXECUTE IMMEDIATE ... to have this script execute all the statements as they are generated.
SET SERVEROUTPUT ON
BEGIN
FOR c IN
(SELECT t.table_name, c.column_name
FROM user_tables t, user_tab_columns c
WHERE c.table_name = t.table_name
AND data_type='VARCHAR2')
LOOP
dbms_output.put_line(
'UPDATE '||c.table_name||
' SET '||c.column_name||' = TRIM('||c.column_name||') WHERE '||
c.column_name||' <> TRIM('||c.column_name||') OR ('||
c.column_name||' IS NOT NULL AND TRIM('||c.column_name||') IS NULL)'
);
END LOOP;
END;
Presumably you want to do this for every column in a schema, not in the database. Trying to do this to the dictionary tables would be a bad idea...
declare
v_schema varchar2(30) := 'YOUR_SCHEMA_NAME';
cursor cur_tables (p_schema_name varchar2) is
select owner, table_name, column_name
from all_tables at,
inner join all_tab_columns atc
on at.owner = atc.owner
and at.table_name = atc.table_name
where atc.data_type = 'VARCHAR2'
and at.owner = p_schema;
begin
for r_table in cur_tables loop
execute immediate 'update ' || r.owner || '.' || r.table_name
|| ' set ' || r.column_name || ' = trim(' || r.column_name ||');';
end loop;
end;
This will only work for fields that are VARCHAR2s in the first place. If your database contains CHAR fields, then you're out of luck, because CHAR fields are always padded to their maximum length.