Search This Blog

Tuesday, August 23, 2011

Executing SQL directly from X++

The X++ language supports a number of ways to execute native SQL against any SQL data source. Below are two samples for retrieving data as well as manipulating data.
Following are the Example using Connection Class.
Example 1#Retrieve

void RetrieveSQLData()
{
   Connection con;
   Statement Stmt;
   ;
   Con = new Connection();
   Stmt = Con.createStatement();
   ResultSet Resultset =Stmt.executeQuery(‘SELECT VALUE FROM SQLSYSTEMVARIABLES’);
    while ( Resultset.next() )
    {
         print Resultset.getString(1);
     }
}

Example 2#Manipulation

void DeleteCustomerData()
{
    str sql;
    Connection conn;
    SqlStatementExecutePermission permission;
;

    sql = ‘delete from custTable where CustId='1005'’;
    permission = new SqlStatementExecutePermission(sql);
   conn = new Connection();
   permission = new SqlStatementExecutePermission(sql);
  //Define the Permission to be started for Executing any manipulation Statements directly
   permission.assert();
   conn.createStatement().executeUpdate(sql);
   // the permissions needs to be reverted back to original condition.
   CodeAccessPermission::revertAssert();

}

No comments:

Post a Comment

Thanks for visiting my blog,
I will reply for your comment within 48 hours.

Thanks,
krishna.