Home » SQL & PL/SQL » SQL & PL/SQL » Dynamic SQL - with CONCAT function (Oracle 11g)
Dynamic SQL - with CONCAT function [message #650277] Thu, 21 April 2016 07:20 Go to next message
ORAGENASHOK
Messages: 240
Registered: June 2006
Location: Chennai
Senior Member
Hi,

Am using dynamic SQL way to form a insert statement as given below.
when i EXECUTE IMMEDIATE its giving error. As i couldn't able to copy full script.
I have given my sample script
as

ORA-00984: column not allowed here
ORA-06512: at "SS_REPO.PKG_SS_LOAD_ALL_TRANS", line 539
ORA-06512: at "SS_REPO.PKG_SS_LOAD_ALL_TRANS", line 42
ORA-06512: at line 6



  create table t1 ( c1 varchar2(100), c2 varchar2(100), c3 varchar2(100))
  
  insert into t1 a values ('ashok','kumar',CONCAT(c1,c2))


Re: Dynamic SQL - with CONCAT function [message #650279 is a reply to message #650277] Thu, 21 April 2016 07:31 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Should be
insert into t1 a values ('ashok','kumar',CONCAT('ashok', 'kumar'))

Or, you could create a database trigger which would do the concatenation.

Or, you could use a virtual column, such as
create table t1
( c1 varchar2(100), 
  c2 varchar2(100), 
  c3 varchar2(200) generated always as (concat(c1, c2)) virtual
);
   
insert into t1 (c1, c2) values ('ashok','kumar');
Re: Dynamic SQL - with CONCAT function [message #650280 is a reply to message #650279] Thu, 21 April 2016 07:37 Go to previous messageGo to next message
ORAGENASHOK
Messages: 240
Registered: June 2006
Location: Chennai
Senior Member
So whenever insert or update this will get automatically concatenate?

Its like filler and bound filler in sql loader.

[Updated on: Thu, 21 April 2016 07:38]

Report message to a moderator

Re: Dynamic SQL - with CONCAT function [message #650282 is a reply to message #650280] Thu, 21 April 2016 08:04 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Yes
Re: Dynamic SQL - with CONCAT function [message #650284 is a reply to message #650280] Thu, 21 April 2016 08:08 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Why do you want to use CONCAT instead of || operator?
Imagine you have to concatenate 10 values...


Re: Dynamic SQL - with CONCAT function [message #650285 is a reply to message #650282] Thu, 21 April 2016 08:34 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
cookiemonster wrote on Thu, 21 April 2016 09:04
Yes


No. That "generated always" means value is generated on reference - it will not be automatically concatenated whenever there is insert or update.

SY.
Re: Dynamic SQL - with CONCAT function [message #650287 is a reply to message #650285] Thu, 21 April 2016 10:20 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
True - but as far interacting with the data goes it basically makes no difference. OP wanted to know it's always reflect the current values of the referenced columns.
Re: Dynamic SQL - with CONCAT function [message #650295 is a reply to message #650287] Thu, 21 April 2016 21:47 Go to previous messageGo to next message
ORAGENASHOK
Messages: 240
Registered: June 2006
Location: Chennai
Senior Member
Yes , I want to know whenever DML operation happen this virtual column should have current values.
I will test this and let know.
Re: Dynamic SQL - with CONCAT function [message #650296 is a reply to message #650287] Thu, 21 April 2016 21:50 Go to previous messageGo to next message
ORAGENASHOK
Messages: 240
Registered: June 2006
Location: Chennai
Senior Member
Its reflecting current values. Thanks Smile

Re: Dynamic SQL - with CONCAT function [message #650316 is a reply to message #650295] Fri, 22 April 2016 06:18 Go to previous message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
ORAGENASHOK wrote on Thu, 21 April 2016 22:47
Yes , I want to know whenever DML operation happen this virtual column should have current values.


Again, not when DML operation happens but when virtual column is referenced:


SQL> create table t1
  2  ( c1 number,
  3    c2 number,
  4     c3 number generated always as (c1 / c2) virtual
  5  );

Table created.

SQL> insert into t1(c1,c2) values(1,0);

1 row created.

SQL> select c1,c2 from t1;

        C1         C2
---------- ----------
         1          0

SQL> select * from t1;
select * from t1
    *
ERROR at line 1:
ORA-01476: divisor is equal to zero


SY.
Previous Topic: Dbms_schedule
Next Topic: need to get row count for database with 20 k tables
Goto Forum:
  


Current Time: Thu Apr 25 14:18:11 CDT 2024