Build a Dynamic Statement

Hi All,

I was wondering if it was possible to dynamically build a X++ statement and execute it. For example:

str sTable = ‘CustTable’;

str sSQLStatement = ‘’;

sSQLStatement = "Select * from " + sTable;

so that I could have one generic procedure that would accept TableName as a parameter, and I could do a select on any table based upon the parameter.

Thanks in Advance for any help

Brian

U can use the Statement and ResultSet system classes for this purpose.

this is a sample:

BuildTSQL()
{
str SQL;
connection cnn = new connection();
statement statement;
resultset rs;
int i=0;
LedgerAccount _AccountNum;
TransDate _TransDate;
;

SQL = “select AccountNum,TransDate from LedgerTrans “;
SQL += strfmt(“WHERE dataareaid=’%1’”,curext());
SQL += strfmt(” AND AccountNum=%1”,LedgerTable.AccountNum);
statement = cnn.createStatement(ResultSetType::Dynamic);
rs = statement.executeQuery(sql);

while ( rs.next() )
{

_AccountNum = rs.getString(1);
_TransDate = rs.getDate(2);
}

}