If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Informix > Using Dynamic Sql in Stored Procedure

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-22-03, 10:27
lloydnwo lloydnwo is offline
Registered User
 
Join Date: Aug 2003
Location: India
Posts: 262
Using Dynamic Sql in Stored Procedure

Hi All,

I need to know how we can use dynamic sql in a stored procedure. Basically i need to use the procedure with insert & update clause. My users will use the procedure in java & pass the parameters for insert & in back-end i will insert the data into the table. Basically we don't want the users to give insert/update clause in their application. Please advice.

Regards,

lloyd
Reply With Quote
  #2 (permalink)  
Old 08-22-03, 16:30
fprose fprose is offline
Registered User
 
Join Date: Apr 2003
Location: Phoenix, AZ
Posts: 177
No you can't.
__________________
Fred Prose
Reply With Quote
  #3 (permalink)  
Old 12-03-03, 03:24
butty butty is offline
Registered User
 
Join Date: Dec 2003
Posts: 10
Are you sure? And do you know if we can call another procedure in a dynamic way from a procedure?
Reply With Quote
  #4 (permalink)  
Old 12-05-03, 08:27
vpshriyan vpshriyan is offline
Registered User
 
Join Date: Nov 2003
Location: Mumbai, India
Posts: 92
Hi,

One of the major drawback of Informix Stored Procedure is it's inability to perform dynamic SQL. A SP is parsed & optimized when compiled, preventing the use of dynamic SQL.

Here is an attempt to make a workaround stored procedure which attempts to insert a row for any table, any column dynamically. The inbound parameters are table name and dynamic number of column values.

Sample SP considers max of 5 column values, but you can edit it to suit according to your need. Data once passed, committed. There no provision to rollback. Should something go wrong, the exception handler pushes an internal rollback mechanism, hence data will not be committed. In such cases SP returns non-zero value, implying failure. Please note that the invoked SYSTEM command creates a separate transaction of its own.

The Informix SYSTEM statement provides the ability to execute any operating system command from within a stored procedure. A word of caution though, of the overhead required to execute an operating system command, the SYSTEM command should be avoided in routines with tight performance requirements. Use this procedure on your own cost and risk.

create dba procedure dynamic_insert(xtabname varchar(18),x1 varchar(64),x2 varchar(64),x3 varchar(64),x4 varchar(64),x5 varchar(64)) returning smallint;

define xowner, xdb varchar(18);
define xcols, xcoltype, xtabid, i smallint;
define xsql varchar(255);

on exception
--check for failure.
raise exception -746, 0, xtabname||": insert failed.";
return 1;
end exception

select owner,ncols,tabid into xowner,xcols,xtabid from systables where tabname=xtabname;
if xowner is null or xowner[1,1]=" " then
raise exception -746, 0, xtabname||" table does not exists.";
return 1;
end if

let xsql="insert into "||xtabname||" values (";

for i=1 to xcols
select coltype into xcoltype from syscolumns where tabid=xtabid and colno=i ;
if ( xcoltype=0 or xcoltype=7 or xcoltype=10 or xcoltype=13 ) then
if i=1 then
let x1="'"||x1||"'" ;
end if
if i=2 then
let x2="'"||x2||"'" ;
end if
if i=3 then
let x3="'"||x3||"'" ;
end if
if i=4 then
let x4="'"||x4||"'" ;
end if
if i=5 then
let x5="'"||x5||"'" ;
end if
end if
end for

if xcols=1 then
let xsql=xsql||x1 ;
end if
if xcols=2 then
let xsql=xsql||x1||","||x2 ;
end if
if xcols=3 then
let xsql=xsql||x1||","||x2||","||x3 ;
end if
if xcols=4 then
let xsql=xsql||x1||","||x2||","||x3||","||x4 ;
end if
if xcols=5 then
let xsql=xsql||x1||","||x2||","||x3||","||x4||","||x5;
end if

let xsql=xsql||")";

select trim(odb_dbname) into xdb from sysmaster:sysopendb
where odb_sessionid=dbinfo('sessionid') and odb_iscurrent='Y';

system 'echo "begin;set lock mode to wait;'||xsql||';commit"'|| '|$INFORMIXDIR/bin/dbaccess '||xdb ;
return 0;

end procedure;

Regards,
Shriyan

Last edited by vpshriyan; 12-09-03 at 03:52.
Reply With Quote
  #5 (permalink)  
Old 12-09-03, 05:04
butty butty is offline
Registered User
 
Join Date: Dec 2003
Posts: 10
thanks a lot for your extense answer.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On