Home > database >  A dynamic SQL
A dynamic SQL

Time:09-26

Note: there are five fields in table1, T, T1, T2, T3, T4; A and B, STR, ls_sql is variable,
Code there is a such a dynamic statement:
Ls_sql="select" + aa + "into" bb "+" from table1 where t="+" '" + ls_str + "' "
The EXECUTE IMMEDIATE: ls_sql;
The sqlca. The autocommit=false
commit;

///
The key is that A is A variable, from other place to take A string, string may be to take out the T1 or T2 or T3 and T4, but can only be one, is A field of the table1, now is to take the table1's value in this field,
For example, the AA value is "T2", made the execution of SQL should be: select T2 into: bb from table1 where t=: ls_str;
But the use of the above method and variable b is not value,

Should be to use dynamic SQL, how to write?

CodePudding user response:

Check the syntaxfromsql () method,
By joining together the SQL to build DW/DS

CodePudding user response:

To use dynamic SQL, access to a dynamic SQL3 or 4

CodePudding user response:

According to the format change
 
The integer Emp_id_var

DECLARE my_cursor DYNAMIC CURSOR FOR SQLSA;

PREPARE SQLSA FROM "SELECT emp_id FROM employee";

The OPEN DYNAMIC my_cursor;

The FETCH my_cursor INTO: Emp_id_var;

The CLOSE my_cursor;

CodePudding user response:


 Datastore lds_i 
String ls_sql, ls_syntax
Int I

Lds_i=CREATE datastore

Ls_sql="select" + aa + "from table1 where t='" + ls_str +"' "

Ls_syntax=sqlca. Syntaxfromsql (ls_sql, 'style (type=grid)', ls_error)

Lds_i. Create (ls_syntax ls_error)

Lds_i. SetTransObject (sqlca)

Lds_i. Retrieve ()

For I=1 to lds_i. Rowcount ()//traverse T1234 result set

Bb=lds_i. Getitemstring (I, aa)

Next

CodePudding user response:

I think it is feasible
  • Related