Dynamics Ax SQL statements (SQL Strings in DAx)
2009-03-09 11:06
281 查看
Dynamics
Ax SQL statements (SQL Strings in DAx)
Good afternoon,Yesterday I hit a snag, took me a few hours to figure out (!). I had
tinkered with SQL strings in ReleaseUpdate classes often before, tweaking them,
rewriting them for optimization etc... but I don't recall ever writing up a new
class that needed to run SQL Statements as strings. Yesterday I needed to do
that because a customer wanted a report that would display some data accross all
companies in an instance. Using the 'changecompany' keyword proved to be heavily
inefficient since all data was in the same table and I only had to group by
fields accros companies on the one table.
After fighting for a few minutes trying to fool QueryBuildRanges to span
multiple DataAreaIds, or trying to change the company on a QueryBuildDataSource
at runtime, I decided to use an SQL string. But then I kept getting the error
"Request for the permission of type 'SqlStatementExecutePermission'
failed."...
The issue I had is that the calling context for SQL Statement execution has
to be RUN ON SERVER. The solution was to create a new class, a main() and a
menuitem running it. Here is a sample of a class executing an SQL statement
(obviously not my customer's!). It returns the customer Id for all customers
accross all DataAreaIds (companies) on a database instance; whereas a standard
select only returns the customers for the current company (curext()).
In short, any operation directly accessing an SQL Database must be called
from a Static Server context. Static because you need to start another thread on
another machine and Server for security reasons (this is only true for 3-tier
installations of Ax 2.5, 3.0, and it is true for all of Dynamics Ax 4.0
installations).
public class MySQLTest
{
//AOSRunMode::Server
}
public static server void main(Args args)
{
Connection connection = new Connection();
Statement stmt =
connection.createStatement();
SqlSystem sqlSystem = new
SqlSystem();
str sqlStmt;
int i;
ResultSet resultSet;
SqlStatementExecutePermission
sqlStatementExecutePermission;
;
sqlStmt = 'SELECT CUSTACCOUNT ';
sqlStmt += 'FROM CUSTTABLE';
sqlStatementExecutePermission = new
SqlStatementExecutePermission(sqlStmt);
sqlStatementExecutePermission.assert();
resultSet =
stmt.executeQuery(sqlStmt);
while (resultSet.next())
{
info(resultSet.getString(1));
}
}
The //AOSRunMode::Server marker is only for the BP checker. You can either
specify 'server' on the static method declaration or as a property on the
calling menuitem.
Of course, in Ax2009, that would never have been an issue, since I'd have
had the crosscompany keyword... hint hint.
相关文章推荐
- Compelling Analytics: SQL Server Reporting Services in Dynamics AX 2009
- 14.5.3 Locks Set by Different SQL Statements in InnoDB
- How to temporarily suppress infolog messages in Dynamics Ax (deux)
- Automatic creation of a PDF from a customer invoice in Dynamics Ax 4.0
- New unified DateTime type in Dynamics Ax 5.0
- Save results to different files when executing multi SQL statements in DB Query Analyzer 7.01
- Database cannot be started in this edition of SQL Server" error when restoring a Microsoft Dynamics CRM database
- Create a simple report in Dynamics AX 2012 R3
- 14.5.3 Locks Set by Different SQL Statements in InnoDB
- 资源产能(Resource Capabilities) in Dynamics AX 2012
- 14.2.2.6 Locks Set by Different SQL Statements in InnoDB
- Support of basic operations on Sets in Dynamics AX
- Understanding the Settlement Mechanism in Microsoft Dynamics AX
- Call DirectSQL in AX (other database by ODBC)
- Save results to different files when executing multi SQL statements in DB Query Analyzer 7.01
- Changes in Microsoft Dynamics AX 2012 InventTrans data model
- 172.The SQL statements executed in a user session are as follows:
- [bbk4980]第07集 - Chapter 04- Interacting with Oracle Database Server:SQL Statements in PL/SQL Programs(02)
- E-Mail notifications in MS Dynamics AX 2012
- Using Dynamic SQL Statements in Stored Procedures