C/ODBC - alternative to TOP / LIMIT / MAX() ?

booga
Member Posts: 3
Hi,
First time poster, so please forgive any faux paus
.
I'm trying to add a record to the end of a table via C/ODBC. As far as I can see, there is no auto increment mechanism, so it looks like I'm going to have to determine the last primary key value, and do my insert with that value + 1.
'SELECT MAX(id)' gives me the result I need, but it consistently takes between 20 and 30 seconds to process the query - which is a bit of a problem. The table in question has around 27,000 rows.
'SELECT TOP n' and 'LIMIT' don't seem to be supported in C/ODBC.
So my question is: does anyone know of an alternative way to get the last row in a table, or determine the highest value of a field, hopefully a quick method than MAX(id)?
I'm running Navision 3.01
Cheers!
First time poster, so please forgive any faux paus

I'm trying to add a record to the end of a table via C/ODBC. As far as I can see, there is no auto increment mechanism, so it looks like I'm going to have to determine the last primary key value, and do my insert with that value + 1.
'SELECT MAX(id)' gives me the result I need, but it consistently takes between 20 and 30 seconds to process the query - which is a bit of a problem. The table in question has around 27,000 rows.
'SELECT TOP n' and 'LIMIT' don't seem to be supported in C/ODBC.
So my question is: does anyone know of an alternative way to get the last row in a table, or determine the highest value of a field, hopefully a quick method than MAX(id)?
I'm running Navision 3.01
Cheers!
0
Comments
-
you could add a new field (boolean) in the table. and from navision as new records get inserted, to update this field and uncheck the previous record. That way you can filter on this new boolean field and get your last row in a table0
-
Or take a look at codeunit 396, wich contains the number series manager.0
-
The boolean idea is rather smart, might give that a go. Unfortunately I don't have the requiste license granule to view codeunits, which is a fairly major PITA
Thanks both!0 -
Incase anyone is looknig for a generic way to solve this, the ODBC API call SQLGetInfo(handle, SQL_KEYWORDS, ...) gives a comma seperated list of keywords supported by the ODBC driver. MS Access includes TOP in this list, whereas others (SQLite for example) don't. You can use this to programatically construct your SQL without knowing the underlying ODBC database type. i.e
If keywords contains "TOP" then sql = "SELECT TOP n * FROM <table>" else sql = "SELECT * FROM <table> LIMIT n"
See MSDN for SQLGetInfo parameters and usage http://msdn.microsoft.com/en-us/library ... 81(v=vs.85).aspx
The DBMS name is also available from SQLGetInfo(handle, SQL_DBMS_NAME, ...) which returns "ACCESS" for MS Access & "SQLite" for SQLite. If you're using a driver which is giving incorrect keywords back you could additionally use the specific DBMS name to determine which type of syntax to use
Jay0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions