Why does the result in TOAD and SQLPlus differ? - oracle

I have this query.
select
dbms_metadata.get_ddl('USER', username) || '/' usercreate
from
dba_users where username = 'NSAGUN';
In TOAD, I get this text. (using SAVE AS TAB DELIMITED)
USERCREATE
CREATE USER "NSAGUN" IDENTIFIED BY VALUES '1EE5F58CB716B194'
DEFAULT TABLESPACE "PIN01"
TEMPORARY TABLESPACE "PINTEMP"
/
But in SQLPlus I only get this:
USERCREATE
--------------------------------------------------------------------------------
CREATE USER "NSAGUN" IDENTIFIED BY VALUES '1EE5F58CB716B194'
DEFAULT T
Why is that? And how can I make the output in SQLPlus the same as in TOAD?

Try using these settings in SQL*Plus before executing the query:
set long 1000000
set longchunk 1000000
set linesize 200
The dbms_metadata.get_ddl function returns a CLOB value and by default SQL*Plus sets the LONG variable to 80 bytes.

SQL> set long 1000000
SQL> set pagesize 0
SQL> SELECT
2 dbms_metadata.get_ddl('USER', 'LALIT') || '/' usercreate
3 from
4 dba_users where username = 'LALIT'
5 /
CREATE USER "LALIT" IDENTIFIED BY VALUES 'S:F10EA8C6778ACE16430E4714FE8C41CFB
2C9E5BC73ADDC503E134EA91AF9;H:076ADC10B6F6540DEEB030DF6C97A752;C6F71E6F6BA0F4BD'
DEFAULT TABLESPACE "USERS"
TEMPORARY TABLESPACE "TEMP"/
SQL>
LONG {80|n}
Set the maximum width (in chars) for displaying and copying LONG values.
SET PAGES[IZE] {14 | n}
Sets the number of rows on each page of output in iSQL*Plus, and the
number of lines on each page of output in command-line and Windows
GUI. You can set PAGESIZE to zero to suppress all headings, page
breaks, titles, the initial blank line, and other formatting
information.

Related

script to fetch oracle database table in csv format

I am using this script as below
set colsep ','
set heading on
set headsep on
set pagesize 0
set trimspool off
spool C:\DXC\books11.csv
Select * from test_extract;
spool off
exit
but the problem with this is
ARKO ,1A , 20
ARKO1 ,1B , 20
space is comming after the values of each attribute as per the lenght of the attribute.
required output :
ARKO,1A,20
ARKO1,1B,20
As far as I can tell, no SET command will help.
One option - that helps - is to name all columns you're spooling and concatenate them using desired column separator.
For example, this is what you currently have:
SQL> set colsep ','
SQL> set heading on
SQL> set headsep on
SQL> set pagesize 0
SQL> set trimspool off
SQL> select * From dept;
10,ACCOUNTING ,Zagreb
20,RESEARCH ,DALLAS
30,SALES ,CHICAGO
40,OPERATIONS ,BOSTON
But, if you do it as follows:
SQL> select deptno ||','|| dname ||','|| loc from dept;
10,ACCOUNTING,Zagreb
20,RESEARCH,DALLAS
30,SALES,CHICAGO
40,OPERATIONS,BOSTON
it looks as you wanted. Drawback? You'll have to type all those columns.
Use SQLcl, it's in your SQL Developer bin directory, works like SQLPlus, only better.
It's SQL.exe on Windows for example, but also available as it's own 25mb download on Oracle.com.
set sqlformat csv
spool file.csv
select * from table;
It'll give you exactly what you're asking for.

Sqlplus parameters and variables with default values

Problem
I have sql scripts which may use different tablespaces for different database users.
In order to remain flexible with the table creation I'd like to keep only 1 script and apply it to the various users. For that purpose I have something like this:
Tablespaces:
CREATE TABLESPACE MY_TABLESPACE DATAFILE 'MY_TABLESPACE.dat' SIZE 40M ONLINE;
CREATE TABLESPACE MY_INDEXSPACE DATAFILE 'MY_INDEXSPACE.dat' SIZE 40M ONLINE;
And the table creation script:
define default_tablespace = 'MY_TABLESPACE';
define default_indexspace = 'MY_INDEXSPACE';
drop table test_table;
create table test_table ( id number ) tablespace &default_tablespace;
create index my_index on test_table( id) tablespace &default_indexspace;
i. e. I can't set a default tablespace for the user, because the index uses a different tablespace.
Question
Is it possible to override the definition of default_tablespace and default_indexspace depending on e. g. an environment variable?
Something like:
define default_tablespace = isEnviromentVariableSet( 'OTHER_TABLESPACE') ? getEnvironmentVariable( OTHER_TABLESPACE) : 'MY_TABLESPACE';
That way I could use different tablespaces whenever I invoke the script externally by some utility and at the same time I could keep the default tablespace.
Thank you very much for the help!
In DDL operation (create, drop, etc.) u can't use variables.
Easy way is use pl/sql anonymous block like this.
declare
my_tabable_space varchar2(100) default 'my_some_tablespace';
other_tablespace varchar2(100);
begin
DBMS_SYSTEM.get_env('OTHER_TABLESPACE',other_tablespace);
if other_tablespace is not null then
my_tabable_space := other_tablespace ;
end if;
execute immediate 'create table test_table ( id number ) tablespace' || my_tabable_space;
end;
/
and for select ENV variable u can use DBMS_SYSTEM.get_env ('NAME of VARIABLE', my_variable) but this package need DBA right (i think.. :-) )

When or Why to use a "SET DEFINE OFF" in Oracle Database

I'm watching a Script in Oracle and I see something I don't recognize
REM INSERTING into database1."Users"
SET DEFINE OFF;
Insert into database1."Users" ("id","right") values ('1','R');
I'm looking for documentation about "set define off" and it's literally writing "disable the parsing of commands to replace substitution variable with their values"
I don't really understand what they want to say.
Can anyone help me?
By default, SQL Plus treats '&' as a special character that begins a substitution string. This can cause problems when running scripts that happen to include '&' for other reasons:
SQL> insert into customers (customer_name) values ('Marks & Spencers Ltd');
Enter value for spencers:
old 1: insert into customers (customer_name) values ('Marks & Spencers Ltd')
new 1: insert into customers (customer_name) values ('Marks Ltd')
1 row created.
SQL> select customer_name from customers;
CUSTOMER_NAME
------------------------------
Marks Ltd
If you know your script includes (or may include) data containing '&' characters, and you do not want the substitution behaviour as above, then use set define off to switch off the behaviour while running the script:
SQL> set define off
SQL> insert into customers (customer_name) values ('Marks & Spencers Ltd');
1 row created.
SQL> select customer_name from customers;
CUSTOMER_NAME
------------------------------
Marks & Spencers Ltd
You might want to add set define on at the end of the script to restore the default behaviour.
Here is the example:
SQL> set define off;
SQL> select * from dual where dummy='&var';
no rows selected
SQL> set define on
SQL> /
Enter value for var: X
old 1: select * from dual where dummy='&var'
new 1: select * from dual where dummy='X'
D
-
X
With set define off, it took a row with &var value, prompted a user to enter a value for it and replaced &var with the entered value (in this case, X).

Oracle: how to "unalter" a session parameter?

I'm doing a case-insensitive query with
alter session set NLS_COMP=LINGUISTIC;
alter session set NLS_SORT=BINARY_CI;
Is there a way to easily capture the session state prior to altering it so that I can restore the session to its original state?
You can obtain the current values using:
select *
from nls_session_parameters;
before you change your session. To restore it, you just use the saved values.
I am not aware of any statement that resets the session parameters to the default value.
The NLS parameters are exposed through a series of views, starting with NLS_. In your case you need NLS_SESSION_PARAMETERS. There are equivalent views for Instance and Database.
This is neater than using v$parameter, although that view does allow us to tell whether a paarmeter is changed from the default value.
You can get the value of a given session parameter by:
SELECT value
FROM nls_session_parameters
WHERE parameter = 'NLS_SORT'; -- replace NLS_SORT with parameter of your choice
This answer demonstrates other means of doing a case insensitive search.
Using UPPER()/LOWER() functions with a function based index.
Regular expressions: REGEXP_LIKE()
You can see the parameter values initially :
SQL> SHOW PARAMETER NLS_SORT;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
nls_sort string BINARY
SQL> SHOW PARAMETER NLS_COMP;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
nls_comp string BINARY
And then set the session parameter accordingly :
SQL> alter session set nls_comp='LINGUISTIC';
Session altered
SQL> alter session set nls_sort='BINARY_CI';
Session altered
In PL/SQL, you can do the following to fetch the parameter value and store it in session variable :
SQL> DECLARE
2 VAR_NLS_SORT VARCHAR2(10);
3 var_nls_comp VARCHAR2(10);
4 BEGIN
5 SELECT VALUE
6 INTO VAR_NLS_SORT
7 FROM NLS_SESSION_PARAMETERS
8 WHERE PARAMETER = 'NLS_SORT';
9 SELECT VALUE
10 INTO VAR_NLS_COMP
11 FROM NLS_SESSION_PARAMETERS
12 WHERE PARAMETER = 'NLS_COMP';
13 DBMS_OUTPUT.PUT_LINE('NLS_SORT = '||VAR_NLS_SORT);
14 DBMS_OUTPUT.PUT_LINE('NLS_COMP = '||VAR_NLS_COMP);
15 END;
16 /
NLS_SORT = BINARY
NLS_COMP = BINARY
PL/SQL procedure successfully completed.
For more information, you can have a look at Oracle – Case Insensitive Sorts & Compares

A useful example of when to use vsize function instead of length function in Oracle?

It seems vsize() and length() return the same results. Does anyone know of a practical example of when to use vsize instead of length?
select vsize(object_name), length(object_name) from user_objects
Result:
/468ba408_LDAPHelper 20 20
/de807749_LDAPHelper 20 20
A4201_A4201_UK 14 14
A4201_PGM_FK_I 14 14
A4201_PHC_FK_I 14 14
Well, Length() takes a character argument (CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB) whereas VSize() takes just about any data type, so if you pass Length() a noncharacter data type there has to be an implicit conversion.
Length is also sensitive to to character sets.
drop table daa_test;
create table daa_test as select sysdate dt from dual;
alter session set nls_date_format = 'YYYY-MM-DD';
select vsize(dt) from daa_test;
select length(dt) from daa_test;
alter session set nls_date_format = 'YYYY-MM-DD HH24:mi:ss';
select vsize(dt) from daa_test;
select length(dt) from daa_test;
... giving ...
drop table daa_test succeeded.
create table succeeded.
alter session set succeeded.
VSIZE(DT)
----------------------
7
1 rows selected
LENGTH(DT)
----------------------
10
1 rows selected
alter session set succeeded.
VSIZE(DT)
----------------------
7
1 rows selected
LENGTH(DT)
----------------------
19
1 rows selected
VSize is really of use IMHO in understanding internal storage requirements of data.
see: http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1897591221788

Resources