Overview
When connecting an object type to an external database, if you have SQL statement (such as SELECT) which has over 3000 characters, you will get and error message similar to:
MCommand.h, 638, Incorrect syntax near 'THE'. (ERROR: 102, SQLSTATE: 42000) (0x80040E14)
(M-Files 18.10.6895.5)
The exact place and keyword that will throw the error will vary depending on the statement itself.
Details
The SQL statements are stored in the vault database and the structure of the table truncates strings that are too long:
- SELECT statement is limited to 3000 characters.
- SELECT statement for getting the new ID after insertion is limited to 500 characters.
- UPDATE statement is limited to 3000 characters.
- DELETE statement is limited to 500 characters.
- Connection string is limited to 1000 characters.
Solution
There are several approaches for shortening the query given to M-Files:
- Create a view in the external database that hides the complexity and only returns the needed columns. Thus, the statement would be about as long as if you use a single table and quite likely will fit below 3000 symbols.
- Create a stored procedure in the external database to do the necessary work and call it from M-Files (the syntax is like with any other SQL command: EXEC sp_mySP ?, ?, ? which assumes the SP is called "sp_mySP" and it takes 3 arguments, as set up for any other statement used with M-Files for update/insert/delete. It does not even have to take arguments). This can be useful if you need to somehow process input coming from M-Files as well.
- Use an intermediate database in a SQL server you own and point commands, views and/or stored procedures to it. This can be useful when you cannot alter the originating database (e.g., because it is part of a ready-made ERP solution). You then have to keep it in sync with the original database.
- Create a custom external object type connector with .NET code where you can store and execute the commands you need as this is your own vault application.
