Search code examples
mysqlsql-serverodbc

Problem with SELECT * in MySQL through ODBC from Microsoft SQL Server


I have a MySQL server as a linked server in Microsoft SQL Server 2008. For the link I use MySQL ODBC Connector version 5.1.8. When invoking queries using OPENQUERY (the only way I found of performing queries), problems occur. Simple queries, such as

SELECT * FROM OPENQUERY(MYSQL, 'SHOW TABLES')

work fine. Selection of individual columns, e.g.,

SELECT * FROM OPENQUERY(MYSQL, 'SELECT nr FROM letter')

works fine as well, but SELECT * syntax does not work. The query:

SELECT * FROM OPENQUERY(MYSQL, 'SELECT * FROM mytable')

raises an error:

Msg 7347, Level 16, State 1, Line 6 OLE DB provider 'MSDASQL' for linked server 'MYSQL' returned data that does not match expected data length for column '[MSDASQL].let_nr'. The (maximum) expected data length is 40, while the returned data length is 0.

How can I make the SELECT * syntax work?


Solution

  • This problem happens if you are querying a MySQL linked server and the table you query has a datatype char(). This means fixed length and NOT varchar(). This happens when your fixed length field has a shorter string than the maximum length that SQL Server expected to get from the ODBC.

    To fix this go to your MySQL server and change the datatype to varchar() leaving the length as it is. Example change char(10) to varchar(10).