pulling data from MySql Server using SQL server openquery

Mike picture Mike · Apr 29, 2013 · Viewed 12.1k times · Source

I am trying to pull data from Mysql server using SQL server OPENQUERY function. When I run a query to count the records that are in thew mysql server I get the value no problem

-this query works and it return the total records found
SELECT count(*) AS total  FROM OPENQUERY(LinkedServer, 'SELECT * FROM mydb_name.users')

But when I do

SELECT login_user FROM OPENQUERY(LinkedServer, 'SELECT * FROM mydb_name.users')

I get this error

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

I have tried but did not work

SELECT CONVERT(CHAR(60), login_user) AS name  FROM OPENQUERY(LASWEB, 'SELECT * FROM mydb_name.users')

I am assuming it is a data type issue but how can I around it? How can I pull the data that I need?

Thanks

Answer

Mike picture Mike · Apr 29, 2013

Cast the variable like so worked

SELECT login_user  FROM OPENQUERY(LASWEB, 'SELECT CAST(u.login_user AS CHAR) AS login_user  FROM mydb_name.users AS u')