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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

Is it possible to access SQL from C/AL?

(0) ShareShare
ReportReport
Posted on by 179

I need to get data from another databases. I tryied using linked objects and it works but it is too slow and expensive as I need a table for each view.

Is it possible to connect to a SQL Server and run a command? Maybe with an automation or DotNet variable?

It would be perfect if I can tun this: Select * from MyTable and read the rows. So I can generate a temporaly table and run a page.

Thanks.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Suresh Kulla Profile Picture
    50,243 Super User 2025 Season 2 on at

    Yes, you can connect to SQL and execute the query or stored procedure. Below is an example using DotNet "'System.Data Variables

    GetSQLConnectionString returns the connection string

    'Data Source=' + "SQL Server Name" + ';'
    
                             + 'Initial Catalog=' + "SQL Database Name" + ';'
    
                             + 'Uid=' + "SQL UserID" + '; '
    
                             + 'Pwd=' + "SQL Password" + ';'
    
    SQLConnection    := SQLConnection.SqlConnection(GetSQLConnectionString);
    
         SQLCommand := SQLCommand.SqlCommand();
    
         SQLCommand.Connection := SQLConnection;
    
         SQLConnection.Open;
    
         SQLCommand.CommandText :=  'sp_abcd';
    
         SQLCommand.CommandType := SQLCommand.CommandType.StoredProcedure;
    
         SQLCommand.CommandTimeout := 120;
    
        // If you have parameters you need to add parameters using the SQL Parameter      
    
         SQLParameter := SQLParameter.SqlParameter;
    
         SQLParameter.ParameterName := '@CustomerNo';
    
         SQLParameter.SqlDbType := SQLParameter.SqlDbType.VarChar;
    
         SQLParameter.Direction := SQLParameter.Direction.Input;
    
         SQLParameter.Size := 50;
    
         SQLParameter.Value:=CustNo;
    
         SQLCommand.Parameters.Add(SQLParameter);
    
         SQLCommand.ExecuteScalar;
    
         SQLConnection.Close;
    
         SQLConnection.Dispose;


  • Jaime Figueras Profile Picture
    179 on at

    Thanks for your reply. It sound great. What if I cant Find System.Data.Variables...?

    7446.Nav.jpg

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics NAV (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans