How to keep SQLPlus from trimming trailing spaces - oracle

If I execute the following script in SQLPlus:
CREATE TABLE trailing_spaces (text VARCHAR2(100))
/
-- Note that there is a blank space after 'one' and 'two'
INSERT INTO trailing_spaces (text) VALUES ('one
two
three')
/
COMMIT
/
SQLPlus automatically trims the lines and removes the trailing spaces, so that instead of inserting the value one two three is inserting onetwothree.
Does anyone know how to keep SQLPlus from trimming those lines and execute the script as it is?

You can do it as in SQLPLUS :
SQL> CREATE TABLE trailing_spaces (text VARCHAR2(100));
Table created.
SQL> INSERT INTO trailing_spaces (text) VALUES ('one'||' '||
'two'||' '||
'three') ;
1 row created.
SQL> COMMIT;
Commit complete.
SQL> SELECT * FROM TRAILING_SPACES;
TEXT
--------------------------------------------------------------------------------
one two three

If I execute your insert statement, I get the text on three different lines. How could you get the text in one line as onetwothree? I am using Oracle 11.2.0.4
In my case, if I cannot alter the script, I can run the existing script and manipulate it during the selection as shown below.
SQL> insert into tbl1 values('one
2 two');
1 row created.
SQL> insert into tbl1 values('three
2 four');
1 row created.
SQL> select * from tbl1;
TEXT
--------------------
one
two
three
four
SQL> select REPLACE(REPLACE(text, CHR(10)), CHR(13)) as text from tbl1;
TEXT
--------------------
one two
three four

Related

How to Search and filter "&" sign?

I need to search for string and extract row data.
In particular, the string has a special character (& sign). How to filter those kinds of strings.
select DISTINCT d.name
from table d
where d.name in ('BUHARY & COMPANY (PVT) LTD','K S KUMARAN')
If you are running your query on sqlplus, keep in mind that & is a special character reserved for variables.
Example
SQL> create table my_test ( c1 varchar2(100) ) ;
Table created.
SQL> insert into my_test values ( 'Hello & Goodbye' ) ;
Enter value for goodbye: ^C
Above, sqlplus stops the execution of the insert because it has found the character ampersand. It asks me to enter the value for that variable. To avoid this, I need to tell sqlplus that I am going to use & as a normal string, for that I have the option define
SQL> set define off
SQL> insert into my_test values ( 'Hello & Goodbye' ) ;
1 row created.
SQL> commit;
Commit complete.
SQL> select * from my_test where c1 like '%&%' ;
C1
--------------------------------------------------------------------------------
Hello & Goodbye
SQL>
If you use another tool, like Toad or Sql Developer, remember to apply the set define off before
Example with Toad

How to replace specified substring with another string for the complete session

I want to create a csv file using sqlplus command for multiple tables as below and it will be called inside shell script.
sqlplus Username/Password#SSD << EOF
set colsep ,
set pagesize 0
set trimspool on
set linesize 32768
set echo off
spool $TABLE.csv;
SELECT * FROM $TABLE;
spool off;
EXIT;
EOF
My Data can also contain a comma(,). if any column data contains comma I want to replace it with "\,".
How can I replace comma for multiple tables and multiple columns? I checked replace function but I don't want to mention column names
As long as you are on SQL Plus 12.2 or above, you should be able to get close to your needs with SET MARKUP
SQL> create table t ( x varchar2(10), y varchar2(10));
Table created.
SQL> insert into t values ('Hello','There');
1 row created.
SQL> insert into t values ('He,llo','The,re');
1 row created.
SQL> set markup csv on
SQL> select * from t;
"X","Y"
"Hello","There"
"He,llo","The,re"
SQL> set markup csv on quote off
SQL> select * from t;
X,Y
Hello,There
He,llo,The,re
You don't get the escape, but if you are enclosing things in quotes, then perhaps that won't matter.

SQL developer: How to make log output from a trigger?

I've made a trigger in SQL and need him to write an output after inserting a new row in the table. Please see the example:
CREATE OR REPLACE TRIGGER GAS_CODES AFTER
INSERT ON blablatable
FOR EACH ROW
BEGIN
insert into blabla2table (...,...,...,...)
values (:new...,...,...,..);
---output:
dbms_output.put_line('New row has been added.');
END;
/
When I compile the trigger, it shows in the Script Output, but if I add a new row into the table, there's nothing.
You are missing SET SERVEROUTPUT ON. This command is understandable also by SQLDeveloper.
Let's do a quick test inside the SQLDeveloper.
CREATE USER "TEST_SCHEMA" IDENTIFIED BY "TEST";
User "TEST_SCHEMA" created.
GRANT UNLIMITED TABLESPACE TO "TEST_SCHEMA";
Grant succeeded.
CREATE TABLE "TEST_SCHEMA"."NAMES" ("ID" NUMBER, "NAME" VARCHAR2(25), PRIMARY KEY("ID"));
Table "TEST_SCHEMA"."NAMES" created.
CREATE OR REPLACE TRIGGER "TEST_SCHEMA"."NAMES_TRG_1" AFTER
INSERT ON "TEST_SCHEMA"."NAMES"
FOR EACH ROW
BEGIN
DBMS_OUTPUT.PUT_LINE('New row has been added.');
END;
/
Trigger NAMES_TRG_1 compiled
SET SERVEROUTPUT ON
This command won't print anything in SQL Developer. No worries here.
INSERT INTO "TEST_SCHEMA"."NAMES" VALUES (1, 'Mark Smith');
1 row inserted.
New row has been added.
As you can see, the output was there and it was inserted after the actual row was inserted into the table. Works fine.
To cleanup the testcase, run this:
DROP USER "TEST_SCHEMA" CASCADE;
EDIT 1:
When you are working with Table Data Editor, this is behaving differently. Table Data Editor has its own Oracle session and it has different way of capturing DBMS Output.
To open the DBMS capture window, you need to click on "VIEW" menu and select "DBMS Output" option.
Then click the green plus button and set the database, that will be captured.
Now you can see the output.
Beware as the output here is not "realtime", this window will show something only when there is a buffer flush, and the buffer flush cannot be invoked manually/directly.
Most likely the client (SQLDeveloper) doesn't read the output buffer.
To enable this you must choose from menu "view" -> "dbms output" and then click the green "+" in the dbms output window to read the output buffer for your connection ...
In sqlplus you can do it like this:
SQL> drop table tst purge;
Table dropped.
SQL> drop table tst2 purge;
Table dropped.
SQL> create table tst ( tst_no integer);
Table created.
SQL> create table tst2 ( tst_no integer);
Table created.
SQL> create or replace trigger tst_trg after insert on tst
for each row
begin
insert into tst2 (tst_no) values (:new.tst_no);
dbms_output.put_line('new row with tst_no='|| :new.tst_no);
end;
/ 2 3 4 5 6 7
Trigger created.
SQL> set serveroutput on;
exec dbms_output.enable;
insert into tst values (1); SQL>
PL/SQL procedure successfully completed.
SQL> SQL>
new row with tst_no=1
1 row created.
SQL> r
1* insert into tst values (1)
new row with tst_no=1
1 row created.
SQL> select * from tst2;
TST_NO
----------
1
1
SQL>
as you can see the output is read and printed in sqlplus, and rows are inserted into the target table tst2
hope it helps...

Data in oracle DB has trailing spaces but they're being stripped by sqlplus

I'm retrieving data from an oracle database using sqlplus.
The command is something like this:
select property_name||'|'||property_value from some_table where x = 'foo'
and the data in the database definitely has trailing spaces (this is a thing which causes problems in an application I work with).
When I retrieve that data the spaces have been automatically trimmed somehow. I can see them when I use SQLDeveloper and when retrieved by the application directly.
Is there a way I can stop this happening?
Here is how it should be working.
SQL> create table spaces (blanks varchar2(20));
Table created.
SQL> insert into spaces values ('A');
1 row created.
SQL> insert into spaces values ('A ');
1 row created.
SQL> insert into spaces values ('A ');
1 row created.
SQL> Insert into SPACES (BLANKS) values ('A
B ');
SQL> commit;
Commit complete.
SQL> select blanks, length(blanks), blanks || '!' from spaces;
BLANKS LENGTH(BLANKS) BLANKS||'!'
-------------------- -------------- ---------------------
A 1 A!
A 6 A !
A 3 A !
A 9 A
B B !
SQL>
The last column shows that none of the 'blanks' are being trimmed. Can you share your scenario in your question details? Or try what I've demonstrated and compare.

how to fire two oracle insert statement together?Oracle 11g

I am trying to fire two insert statements at a time. Actually i have tried with below query but its inserting in only one table.
EXECUTE IMMEDIATE 'select * from abc.test where test_NAME = ''aaa''' BULK COLLECT INTO T_SC;
IF T_SC.count = 0 THEN
Insert into abc.test (test_ID,test_NAME,status)
VALUES(1,'aaa','a') BULK COLLECT INTO insert_cnt;
IF insert_cnt.count = 1 THEN
INSERT INTO abc.test1(test1_id,test1_NAME,test1_ALIAS,test_ID)
VALUES(1,'bbb','b',1);
COMMIT;
END IF;
it is only inserting in abc.test1 table..What i am going to missing. If anyone knows than plz help me in this.
This whole code of yours doesn't seem right:
why the dynamic sql ?
there are lots of syntax errors
unclosed "if" (as vj shah commented)
missing returning keyword
why do you need the bulk collect if your returning from the insert only one row ?
what is the second "if" for ?
and so on...
Anyway, this code works:
EXECUTE IMMEDIATE 'select * from abc.test where test_NAME = ''aaa''' BULK COLLECT INTO T_SC;
/* BTW, why not
select * bulk collect into T_SC from abc.test where test_NAME = 'aaa';
*/
IF T_SC.count = 0 THEN
Insert into abc.test (test_ID, test_NAME, status)
VALUES(1,'aaa','a') returning test_ID, test_NAME, status BULK COLLECT INTO insert_cnt;
IF insert_cnt.count = 1 THEN
INSERT INTO abc.test1(test1_id,test1_NAME,test1_ALIAS,test_ID)
VALUES(1,'bbb','b',1);
END IF;
COMMIT;
END IF;
Can you explain your problem a little better. Neither your logic nor the data that you show gives any idea of what you are trying to accomplish. (the logic behind the if).
This is also not functional code (too many syntax errors), can you update with the real code you are firing? May be just change the table names?
If you want to make sure both the statements either complete successfully or both of them are rolled back, your approach of including them in a block is correct.
SQL> create table test_rc_2(
2 id number
3 );
Table created.
--Sample 1 : Submitting inserts seperately (only the latest statement is rolled
SQL> insert into test_rc_2 values (100);
1 row created.
SQL> insert into test_rc_2 values ('hello');
insert into test_rc_2 values ('hello')
*
ERROR at line 1:
ORA-01722: invalid number
SQL> commit;
Commit complete.
SQL> select * from test_rc_2;
ID
----------
100
--case 2 : submittig them in a block.
SQL> truncate table test_rc_2
2 ;
Table truncated.
SQL> begin
2 insert into test_rc_2 values(100);
3 insert into test_rc_2 values('hello..');
4 end;
5 /
begin
*
ERROR at line 1:
ORA-01722: invalid number
ORA-06512: at line 3
SQL> commit;
Commit complete.
SQL> select * from test_rc_2;
no rows selected

Resources