web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Executing Stored Procedures in Dynamics AX 2009

(0) ShareShare
ReportReport
Posted on by 1,385

I created a method in a class (I set the property to run on server) and called this method from a job.  This method tries to execute a stored procedure (this SP inserts a dummy record in a table).  The process runs without any error but I cannot see the results!

If I run this SP from SQL Management Studio it works perfectly

 

    Connection con = new Connection();
    Statement stmt = con.createStatement();
    ResultSet r;
    str sql;
    SqlStatementExecutePermission perm;
    ;

    sql = strfmt('EXEC [insertdaxjob]');


    // Set code access permission to help protect the use of
    // Statement.executeUpdate.
    perm = new SqlStatementExecutePermission(sql);
    perm.assert();

    try
    {
        stmt.executeUpdate(sql);
    }
    catch (exception::Error)
    {
        print "An error occured in the query.";
        pause;
    }
    // Code access permission scope ends here.
    CodeAccessPermission::revertAssert();

 

Can anyone help?

*This post is locked for comments

I have the same question (0)
  • akuehn Profile Picture
    1,995 on at

    Hi,

    i'm not really sure, but did you tried it with "stmt.executeQuery" ?

  • Abdallah Zein Al Abdin Profile Picture
    1,385 on at

    Hi akuehn,

     Yes I did try executeQuery and it didnt work either :(

     

    Here is the SP:

    CREATE PROCEDURE [dbo].[insertdaxjob]

    AS

    BEGIN

    Set
    NoCount On

     

    insert into daxjob values('test', getdate())

    END

  • jaestevan Profile Picture
    1,719 on at

    May be some kind of security issue? The user that executes the AX client has permisions to execute this SP? Is this job executing in the AOS?

  • Abdallah Zein Al Abdin Profile Picture
    1,385 on at

    JA,

    Actually I am running the SP from DAX client. 

  • jaestevan Profile Picture
    1,719 on at

    And are you sure that the user that runs this DAX client has permissions in the SQL Server that you wants to run the stored procedure?

    Think that an AX user don't need those permissions because the AOS "impersonate" the sql conections but if you run some code throw .net interop, it will try to execute directly in the sql server and this will need specific sql authentication.

    Hope this helps.

  • akuehn Profile Picture
    1,995 on at

    Hi,

    i have analysed your problem a little bit in more detail (ok, i have tried to.. :-) )

    Please check the following:

    Got to the properties of your StoredProcedure in SQL Server Management Studio. Check that your AOS-Account has the right to execute the SP. For a new created SP the account should not have these rights..
    Also check that your code, which calls the SP, is running on server side.

    I have check this with the following code and the following SP. After setting the execute right for the SP everything works.

    AX Code:
    server static void executeSP()
    {
        Connection con = new Connection();
        Statement stmt = con.createStatement();
        ResultSet r;
        str sql;
        SqlStatementExecutePermission perm;
        ;

        sql = strfmt('EXEC [insertdaxjob]');

        perm = new SqlStatementExecutePermission(sql);
        perm.assert();

        try
        {
            stmt.executeUpdate(sql);
        }
        catch (exception::Error)
        {
            print "An error occured in the query.";
            pause;
        }

        CodeAccessPermission::revertAssert();
    }

    Stored Procedure:
    USE [AX2009Database]
    GO

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    ALTER PROCEDURE [dbo].[insertdaxjob]

    AS
    BEGIN

     SET NOCOUNT ON;
     insert into dbo.TestTable(ID, Name, RECID, RECVERSION, DATAAREAID) values('1', 'Test', 5637144590, 1, 'ceu')

    END

    As you can see i simple have added the SP to the AX Database. That is the reason for RecId, RecVersion and DataAreaId fields. But this should also work with any other database were the SP is located in.

     

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Akuehn

    I had just try your method, it is very useful for me. Thank you.

    The new created SP's permission is needed to set before using it.

  • ismailkrt Profile Picture
    5 on at

    I want to execute a stored procedure but it wants a variable to run the stored procedure.how can I add a variable into ax code?

    i tried like this and it seems work ing

    sql = strfmt('EXEC insertdaxjob %1',variable1);

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 28

#2
Michel ROY Profile Picture

Michel ROY 14

#3
imran ul haq Profile Picture

imran ul haq 8

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans