I want to test user passwords so I have created this simple select by using concat :
select 'conn '||oracle_username||'/test11#dev;'
from FND_ORACLE_USERID
where oracle_username not in ('APPS','APPLSYS')
and READ_ONLY_FLAG='A'
;
Query results output is like this:
conn ALR/test11#dev;
conn AX/test11#dev;
conn AK/test11#dev;
And I would like to add one more line under each, to look like this:
conn ALR/test11#dev;
disc;
conn AX/test11#dev;
disc;
conn AK/test11#dev;
disc;
How to manage this?
Thank you
try to say
select 'conn '||oracle_username||'/test11#dev;' || chr(10) || 'disc;'
from FND_ORACLE_USERID
where oracle_username not in ('APPS','APPLSYS')
and READ_ONLY_FLAG='A';
This will print output for each record in 2 lines
PS. Sometimes it might be needed not only "|| chr(10) ||" but "|| chr(10) || chr(13) ||". Try both if first one does not work.
For me as far as I can remember "chr(10)" worked in both windows and unix
Related
I need a query for db2 and oracle which can be used for exporting data into CSV files and for datatype like BLOB, CLOB it will have an empty string.
eg:
Table-->
col1(char),col2 (blob),clo3 (date)
video_1,blob, '1998-05-02'
csv file --->
col1,col2,col3
"video_1","",'1998-05-02'
You may generate the corresponding statements dynamically to execute them later in Db2.
Below is an example for 2 system tables SYSIBM.SYSTABLES and SYSIBM.SYSTABLESPACES.
You may run it as is to get 2 EXPORT commands with the corresponding SELECT statements.
The output can be used as is to execute it as a script of commands with the Db2 Command Line Processor (db2 -tf out_file.sql in a session with a database connection established). If you comment out the string with EXPORT, you may get pure corresponding SELECT statements to use them with some another tool capable to save the result of an arbitrary SELECT statement to a CSV file (as the Db2 EXPORT command does it).
SELECT
'EXPORT TO ' || RTRIM (T.TABSCHEMA) || '.' || T.TABNAME || '.csv OF DEL'
||' SELECT ' || LISTAGG (CASE WHEN C.TYPENAME LIKE '%LOB' THEN '''''' ELSE '"' || C.COLNAME || '"' END, ', ') WITHIN GROUP (ORDER BY C.COLNO)
||' FROM "' || T.TABSCHEMA || '"."' || T.TABNAME || '";'
FROM SYSCAT.TABLES T
JOIN SYSCAT.COLUMNS C ON C.TABSCHEMA = T.TABSCHEMA AND C.TABNAME = T.TABNAME
WHERE T.TABSCHEMA = 'SYSIBM' AND T.TABNAME IN ('SYSTABLES', 'SYSTABLESPACES')
GROUP BY T.TABSCHEMA, T.TABNAME
My data contains lots of new line feeds at starting and end of string, which is making difficulty in understanding data. Sample data is shown as below.
'
sample text
new value
'
I tried to replace chr(10) with empty string, but it was removing line feed between two lines which is required.
My Code
select REPLACE(a1, chr(10), ' ') from( select '
Sample Text
New value
' as a1 from dual);
Any suggestions.
Try using TRIM:
SELECT
TRIM(CHR(10) FROM col)
FROM dual
TRIM has the option to specify a character to remove from the beginning and ending of the string. Follow the short demo below to see it in action. Click here to see how to use TRIM with a number of examples.
Edit: If you have a CLOB column and TRIM won't work, then we can still use REGEXP_REPLACE:
SELECT REGEXP_REPLACE(REGEXP_REPLACE(col, '^' || CHR(10) || '*', ''),
CHR(10) || '*$', '') AS output
FROM dual
Here is a demo showing that REGEXP_REPLACE works in place of TRIM:
Demo
Another approach to trim or remove the line feed/carriage returns/tab is using
"translate(column_name, chr(10) || chr(13) || chr(09), ' ') ".
This worked for me.
I want a way of dynamically exporting any given table in oracle 11g to a csv using sql*plus - instead of needing to explicitly hard code the column names each time.
Please use following syntax.
SET MARKUP HTML ON SPOOL ON
HEAD "<title>Data Extract</title> - <meta http-equiv='Content-Type' content='application/vnd.ms-excel;'>
<style type='text/css'>
</style>"
SET ECHO OFF
SPOOL output.xls
select * from &tablename ;
spool off
exit
Enter tablename when prompted.
Abhi
You can use the SET Colsep option.
SET COLSEP ","
SET PAGES 0
SET FEEDBACK OFF
SPOOL output.csv
select * from HR.employees;
spool off
exit
After searching stack overflow I could not find an exact answer to this problem - therefore I developed my own solution.
Here is the sql script that I wrote:
SET echo off
SET verify off
SET heading off
SET pages 50000
SET feedback off
SET newpage none
SET termout off
SET linesize 900
SET trimspool on
SET serveroutput on
define table_name = &1
define spool_path = &2
var rc refcursor
column qry new_val capture
SELECT 'select ''"'' || ' || listagg(column_name,' || ''","'' || ') within group (order by column_id) || ' || ''"'' as rec from &table_name' qry
FROM user_tab_cols
WHERE table_name = '&table_name';
spool &spool_path
SELECT listagg(column_name,',') WITHIN GROUP (ORDER BY column_id)
FROM user_tab_cols
WHERE table_name = '&table_name';
BEGIN
FOR v_rec IN (&capture) LOOP
dbms_output.put_line(v_rec.rec);
END LOOP;
END;
/
spool off
EXIT
The script requires two parameters - the first parameter is the table name, and the second is the spool path.
Essentially the first query:
SELECT 'select ''"'' || ' || listagg(column_name,' || ''","'' || ') within group (order by column_id) || ' || ''"'' as rec from &table_name' qry
FROM user_tab_cols
WHERE table_name = '&table_name';
Dynamically creates a new select statement (which loads into the &capture substitution variable) using user_tab_cols and listagg to force each column name onto a single line - this dynamically created select statement will essentially concatenate each column together separated by commas. Later when we spool we loop through the dynamically generated select statement to produce each row of data.
The header is produced by a similar query (the first action once we begin spooling), however for this we can just listagg the column header names together directly.
It is easier to understand by testing the two queries on a table of your choice to see the result!
in my actual job I need, very often, to read some tables and acting consequently, sometimes updating these data manually.
So I built a PL/SQL block that creates my SELECT statements (yes, with the "FOR UPDATE" clause, just commented).
As an example, this is just one of the queries I build:
phtr_QUERY := 'SELECT *
FROM ' || tabriabi_impianto || '.pdfhtr t
WHERE t.k_abi=''' || tabriabi_abi || ''' ';
if length(myNag) > 0 then
phtr_QUERY := phtr_QUERY || 'and t.ndg like ''%' || myNag || '%'' ';
end if;
if length(myPrat) > 0 then
phtr_QUERY := phtr_QUERY || ' and t.pratica like ''%' || myPrat || '%'' ';
end if;
phtr_QUERY := phtr_QUERY || crlf || ' order by 2 ';
phtr_QUERY := phtr_QUERY || crlf || '--for update';
phtr_QUERY := phtr_QUERY || crlf || ';';
Then I copy these statements from the Output window (obtained through the dbms_output.put_line) and paste it into a new SQL Window and executing it, obtaining the results in multiple tabs.
I was wondering if there is a better way, some commands that I can use just to have the (editable) results directly without the need of cut&paste...
TIA.
F.
A very horrifying/hackish way to do what you want would be to store the resulting query in a temporary table, afterwards you could do something like the process described here:
How can I use an SQL statement stored in a table as part of another statement?
Please Note: This is probably a bad idea.
select a.rowid, a.* from table_name a;
will open in edit mode in many tools.
I was wondering if there is a better way, some commands that I can use just to have the (editable) results directly without the need of cut&paste
You should understand that editing features are features of database tool you are using. When you insert, update or delete some record in the results grid this tool translates your actions into respective SQL statements and executes it on the fly.
As a kind of workaround I suggest you to create a stored procedure which takes some parameters as 'table name', 'where conditions' and then creates updateable database view. After execution of procedure and preparation of the view you can run "select ... for update" and work with returned data as you do it now.
Good day. I have a very specific task: regenerate all sequences in database. There is a 400+ tables in it, so I can't do it by hands.
Can somebody help me to do it?
Thanks a lot..
Please note this is highly dangerous. You may very well make mistakes. Run the select first to check what you're about to do and create a table of the select so you can re-create the synonyms manually later if you need to.
Using all_synonyms or dba_synonyms instead of user_synonyms may result in dropping system synonyms do not do this if you want your database to work afterwards
I'd also recommend testing the code on 1 test synonym you create to ensure that it does exactly what you want.
Plus I don't really see the point of doing this at all? If the synonyms are there why do you need to re-generate them? Or is this being done on another server? If so add #server_name after user_synonyms and remove the drop.
begin
for xx in ( select * from user_sequences ) loop
execute immediate 'drop sequence ' || xx.sequence_name;
execute immediate 'create sequence ' || xx.sequence_name
|| ' start with ' || xx.min_value
|| ' ends with ' || xx.max_value
|| case when xx.cycle_flag = 'N' then ' nocycle '
else ' cycle ' end
|| case when xx.cache_size = 0 then ' nocache '
else ' cache ' end || xx.cache_size
|| case when xx.order_flag = 'N' then ' noorder '
else ' order ' end
;
end loop;
end;