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 :
Finance | Project Operations, Human Resources, ...
Answered

Insert directly into SQL using X++

(2) ShareShare
ReportReport
Posted on by 525
I'm trying to insert directly into SQL but I get the error: Cannot execute the required database operation.
The SQL database has issued an error.
 
public static void main(Args _args)
    {

        Connection      connection;
        Statement       statement;
        str             query;
        Resultset       resultSet;

        connection = new Connection();

        statement = connection.createStatement();
        query = 'insert into xdscusttableregular (CustAccount) select ACCOUNTNUM from CUSTTABLE where CUSTTABLE.DATAAREAID = "adc" '
                + 'and CUSTTABLE.INVENTSITEID = "ADC"';
        new SqlStatementExecutePermission(query).assert();
        statement.executeUpdate(query);
        CodeAccessPermission::revertAssert();
        Info('Insertion done');
    }
 
  • Suggested answer
    André Arnaud de Calavon Profile Picture
    307,097 Super User 2026 Season 2 on at
    Hi Abdullah,
     
    Is the table xdscusttableregular a table defined in the AOT and synced to the SQL server? Do you get more details than just "The SQL database has issued an error"?
     
    Why do you want to use this statement using direct SQL instead of achieving the same with X++ coding? E.g.
     
    CustTable            custTable
    Xdscusttableregular  xdscusttableregular;
    
    
    insert_recordset xdscusttableregular (CustAccount)
        select AccountNum from custTable
            where custTable.DataAreaId == 'adc'
               && custTable.InventSiteId == 'ADC';
    This code will be translated in the same statement you wrote and will be fully executed on the SQL server.
     
     
  • Verified answer
    AbdullahAhmed_ Profile Picture
    525 on at
    Thank you for your response..
     
    I discovered why the error happening.. it has something to do with quotation mark (" ") and (' ')!
     
    Anyway.. I wanna insert a lot of records in synced table with database (+1.2M records), using x++ to insert these records it takes 2:33 minutes and inserting them in SQL server takes like 2 minutes...
    The difference will be more than 30 seconds when inserting +10M records, so I thought to prepare a query string and the insertion happens in the database.
  • Martin Dráb Profile Picture
    240,742 Most Valuable Professional on at
    Did you use insert_recordset statement in X++?

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 429 Most Valuable Professional

#2
CU10121822-0 Profile Picture

CU10121822-0 366

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 360 Super User 2026 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans