Oracle plsql switch case and sqlplus spool - oracle

The following query runs in an error at the THAN statement. Is it possible to use a select in the THAN statement? Any ideas on how to make it work?
SET SERVEROUTPUT ON
SET TERMOUT OFF
SET ECHO OFF
SET FEEDBACK OFF
SET LINESIZE 140
SET PAGESIZE 0
exec dbms_output.enable(NULL);
SPOOL C:\test\user.sql
BEGIN
SELECT DISTINCT version,
CASE
WHEN version = '12.1.0.2.0'
THEN
dbms_output.put_line(''select' || 'Alter user ' || username || ' identified by values ' || '''EE3FD1E715941451''' || ';''); from DBA_USERS_WITH_DEFPWD;
ELSE
dbms_output.put_line(''select' || 'Alter user ' || username || ' identified by values ' || '''Invalid Password''' || ';''); from DBA_USERS_WITH_DEFPWD;
FROM PRODUCT_COMPONENT_VERSION;
END;
SPOOL OFF
Edit:
Code which is working looks like this:
spool c:/test/user.sql
select 'Alter user ' || username || ' identified by values ' || '''Invalid Password''' || ';'
from DBA_USERS_WITH_DEFPWD;
spool off
Spool output example:
Alter user GSMUSER identified by values 'Invalid Password';
Alter user MDSYS identified by values 'Invalid Password';
Alter user OLAPSYS identified by values 'Invalid Password';
Alter user LBACSYS identified by values 'Invalid Password';
The script is not working with new Oracle DB Versions. Since Oracle 12.1.0.2.0 it’s not possible to set the password to an invalid password. I need to build in version identification for this issue. All Oracle Databases with a version below 12.1.0.2.0 should be treated with the old script and the passwords should be set to “invalid password”. All newer versions should get some kind of standard password for now.

Maybe you need something like this:
...
DECLARE
vVersion varchar2(100);
BEGIN
/* get the version */
SELECT DISTINCT version
into vVersion
from PRODUCT_COMPONENT_VERSION;
--
/* loop through users */
for i in ( select * from DBA_USERS_WITH_DEFPWD) loop
/* print a different statement, based on vVersion, for the current user */
if ( vVersion = '12.1.0.2.0' ) then
dbms_output.put_line('Alter user ' || i.username || ' identified by values ' || '''EE3FD1E715941451''' || ';');
else
dbms_output.put_line('Alter user ' || i.username || ' identified by values ' || '''Invalid Password''' || ';');
end if;
end loop;
END;
...

Another version which solves the problem without PL/SQL and correct spool:
SET TERMOUT OFF
SET ECHO OFF
SET LINESIZE 140
SET FEEDBACK OFF
SET PAGESIZE 0
SPOOL user.sql
SELECT 'alter user ' || username || ' identified by values '''
|| CASE
WHEN b.version = '12.1.0.2.0' THEN '462368EA9F7AD215'
ELSE 'Invalid Password'
END
|| ''';'
FROM DBA_USERS_WITH_DEFPWD a,
(SELECT VERSION
FROM PRODUCT_COMPONENT_VERSION
WHERE UPPER (product) LIKE '%DATABASE%') b;
SPOOL OFF
#user.sql

If it's not possible to use PL/SQL:
SET TERMOUT OFF
SET ECHO OFF
SET LINESIZE 140
SET FEEDBACK OFF
SET PAGESIZE 0
spool user.sql
SELECT 'Alter user '
|| A.USERNAME
|| ' identified by values '
|| '''EE3FD1E715941451'''
|| ';'
FROM DBA_USERS_WITH_DEFPWD a,
(SELECT DISTINCT version
FROM PRODUCT_COMPONENT_VERSION) b
WHERE version = '12.1.0.2.0'
UNION ALL
SELECT 'Alter user '
|| A.USERNAME
|| ' identified by values '
|| '''Invalid Password'''
|| ';'
FROM DBA_USERS_WITH_DEFPWD a,
(SELECT DISTINCT version
FROM PRODUCT_COMPONENT_VERSION) b
WHERE version != '12.1.0.2.0';
spool off
#user.sql

Related

Oracle: update table using dynamic column names

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?

Execute immediate error in Oracle

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;

oracle set public synonym all table or other object?

I have productionDB and testDB.ProductionDB have synonym for tables.And now i want to create synonym for all tables for testDB.How i can create synonym all of them. I can get list synonym this query
select *
from all_synonyms s
join all_objects o
on s.table_owner = o.owner
and s.table_name = o.object_name
where s.table_owner = 'XYZ'
Try the following query:
SELECT CAST(dbms_metadata.get_ddl(object_type => 'SYNONYM',
name => a.synonym_name,
SCHEMA => a.owner) AS VARCHAR2(4000))
FROM ALL_SYNONYMS a;
Share and enjoy.
This is a starter script - it writes sql as output. Run it, READ the output first. Uncomment the last line when you think it is correct - it has to be run with privilege.
set pages 0
set feed off
set linesize 180
set trimspool on
spool syn.sql
select 'CREATE PUBLIC SYNONYM ' || SYNONYM_NAME | ' FOR ' ||
TABLE_OWNER || '.' || TABLE_NAME || ';'
from ALL_SYNONYMS
where DB_LINK is NULL;
select 'CREATE PUBLIC SYNONYM ' || SYNONYM_NAME | ' FOR ' ||
TABLE_OWNER || '.' || TABLE_NAME || '#' || DB_LINK || ';'
from ALL_SYNONYMS
where DB_LINK is NOT NULL;
spool off
-- uncomment after it has been tested
--#syn.sql
If testDB was created using a PROD export then why are the synonyms all missing? Something is very wrong here. OR I am missing something.

How to debug this plsql code?

I am getting error while executing ps-sql procedure.
below is my script.
set serveroutput on size 100000
set echo off
set feedback off
set lines 300
declare
cursor sessinfo is
SELECT NVL(s.username, '(oracle)') AS username,
s.osuser,
s.sid,
s.serial#,
p.spid,
s.status,
s.module,
s.machine,
s.program,
TO_CHAR(s.logon_Time,'DD-MON-YYYY HH24:MI:SS') AS logon_time,
s.last_call_et/3600 last_call_et_Hrs,
lpad(t.sql_text,30) "Last SQL"
from gv$session s,
gv$sqlarea t,
gv$process p
where s.sql_address = t.address and
s.sql_hash_value =t.hash_value and
p.addr=s.paddr and
s.status='INACTIVE' and
s.last_call_et > (3600)
order by last_call_et;
sess sessinfo%rowtype;
sql_string1 Varchar2(2000);
sql_string2 Varchar2(2000);
begin
open sessinfo;
loop
fetch sessinfo into sess;
exit when sessinfo%notfound;
sql_string1 := 'sid=' || sess.sid ||
' serail#=' || sess.serial# ||
' machine=' || sess.machine ||
' program=' || sess.program ||
' username=' || sess.username ||
' Inactive_sec=' || sess.last_call_et ||
' OS_USER=' || sess.osuser;
sql_string2 := 'alter system kill session ' || chr(39) ||
sess.sid || ',' || chr(39) || sess.serial# || ';';
dbms_output.put_line(sql_string1);
dbms_output.put_line(sql_string2);
end loop;
close sessinfo;
end;
/
quit;
i am getting below error. please help me.
[oracle#localhost ~]$ sqlplus -s '/as sysdba' #inactive12.sql
sql_string1:='sid='||sess.sid||' serail#='||sess.serial#||' machine='||sess.machine||' program='||sess.program||' username='||sess.username||' Inactive_sec='||sess.last_call_et||' OS_USER='||sess.osuser;
*
ERROR at line 12:
ORA-06550: line 12, column 165:
PLS-00302: component 'LAST_CALL_ET' must be declared
ORA-06550: line 12, column 1:
PL/SQL: Statement ignored
You aliased last_call_et as last_call_et_hrs, so the cursor doesn't see last_call_et anymore, use the aliased column last_call_et_hrs.

UPDATE statements in oracle pl/sql loop with tablenames as parameters

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||''')';

Resources