SELECT * FROM Linked MySQL server - mysql

I have a SQL Server 2012.(120.120.55.15)
Today I linked MySQL server(120.120.55.30) to my SQLServer and gave it a name "MYSQL".
In Object Explorer everything seems fine. I can see MySQL server's database "exampleDataBase" and tables in it.
But when I try to run select query like this:
SELECT *
FROM openquery
(
MYSQL,
'
SELECT *
FROM [exampleDataBase].[msProcMatrix]
'
)
I get a mistake:
Msg 7399, Level 16, State 1, Line 1 The OLE DB provider "MSDASQL" for
linked server "MYSQL" reported an error. The provider did not give any
information about the error. Msg 7350, Level 16, State 2, Line 1
Cannot get the column information from OLE DB provider "MSDASQL" for
linked server "MYSQL".
What should be additionally done to use my linked MySQL server?

Found the decision:
SELECT *
FROM openquery(MYSQL, 'SELECT * FROM exampleDataBase.msProcMatrix')
Without brackets!
Strange for me but works...

This worked great for me after fighting the same issue on MS SQL Server 2008 64bit using the MY SQL 3.51 64 bit ODBC driver
SELECT *
FROM OPENQUERY
(
linked_server_name,
'SELECT * FROM linked_database_name.linked_table_name'
)

When I working with linked server, I never use Select * From.
Try with Select Column1, Column2, ... ColumnN From.
Always works fine for me.

You might need a schema name between the database name and the table name.
SELECT *
FROM openquery
(
MYSQL,
'
SELECT *
FROM [exampleDataBase].**[dbo]**.[msProcMatrix]
'
)

If the default catalog ("exampleDataBase") is configured in ODBC, the following will work as well:
select * from MYSQL...msProcMatrix

You Can try this query .
EXEC
(
'SELECT * FROM [exampleDataBase].[msProcMatrix]'
)
AT MYSQL

Please try the statement in the format below ..
for me it works very well
SELECT *
FROM openquery
(
MYSQL,
'
SELECT *
FROM [MYSQL]...[exampleDataBase].[msProcMatrix]
'
)
Including the extra levels in the name may solve your issue.

Related

Talend - How to execute multiple statement along with prepare statement in tMySQLInput

I am new to Talend. I tried to run multiple select statements as MSSQL Query 1 to retrieve maximum Id's in different tables using tMSSQLInput component. It worked fine.
MSSQL Query 1 "DECLARE #sqlCommand varchar(1000)
SET #sqlCommand = 'SELECT(SELECT max(Id) FROM table1) AS maxIdAC,
(SELECT max(Id) FROM table2' + CAST((YEAR(GETDATE())-1) as varchar(10)) +')
AS maxIdACT, (SELECT max(Id) FROM table3) AS maxIdAF'
EXEC (#sqlCommand)"
I tried to achieve the same logic with MSSQL Query 2 using tMySQLInput Component. It throws the ERROR. When I run the same query in MySQL Server console. It works fine without any error.
MSSQL Query 2 "set #multiplequery = concat('select (', concat('select Max(Id) FROM table1',CAST(YEAR(CURDATE())-1 as CHAR(50))),')AS maxIdACT,(' , 'select max(Id) FROM table2',') AS maxIdAC,(' , 'select max(Id) FROM table3',') AS maxIdAF');
prepare execQuery FROM #multiplequery;
execute execQuery"
Error Exception in component tDBInput_1 (ExtAndStoreData)
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'prepare execQuery FROM #multiplequery;
execute execQuery' at line 2
Could someone tell me how to achieve this in Talend. I also read that it is possible using tMysqlRow component. But I could not figure out how to run the above-mentioned MSSQL Query 2 using tMySqlRow Component. Could someone help

How to create a query against 2 databases in mysql linked server? [duplicate]

I have a SQL Server 2012.(120.120.55.15)
Today I linked MySQL server(120.120.55.30) to my SQLServer and gave it a name "MYSQL".
In Object Explorer everything seems fine. I can see MySQL server's database "exampleDataBase" and tables in it.
But when I try to run select query like this:
SELECT *
FROM openquery
(
MYSQL,
'
SELECT *
FROM [exampleDataBase].[msProcMatrix]
'
)
I get a mistake:
Msg 7399, Level 16, State 1, Line 1 The OLE DB provider "MSDASQL" for
linked server "MYSQL" reported an error. The provider did not give any
information about the error. Msg 7350, Level 16, State 2, Line 1
Cannot get the column information from OLE DB provider "MSDASQL" for
linked server "MYSQL".
What should be additionally done to use my linked MySQL server?
Found the decision:
SELECT *
FROM openquery(MYSQL, 'SELECT * FROM exampleDataBase.msProcMatrix')
Without brackets!
Strange for me but works...
This worked great for me after fighting the same issue on MS SQL Server 2008 64bit using the MY SQL 3.51 64 bit ODBC driver
SELECT *
FROM OPENQUERY
(
linked_server_name,
'SELECT * FROM linked_database_name.linked_table_name'
)
When I working with linked server, I never use Select * From.
Try with Select Column1, Column2, ... ColumnN From.
Always works fine for me.
You might need a schema name between the database name and the table name.
SELECT *
FROM openquery
(
MYSQL,
'
SELECT *
FROM [exampleDataBase].**[dbo]**.[msProcMatrix]
'
)
If the default catalog ("exampleDataBase") is configured in ODBC, the following will work as well:
select * from MYSQL...msProcMatrix
You Can try this query .
EXEC
(
'SELECT * FROM [exampleDataBase].[msProcMatrix]'
)
AT MYSQL
Please try the statement in the format below ..
for me it works very well
SELECT *
FROM openquery
(
MYSQL,
'
SELECT *
FROM [MYSQL]...[exampleDataBase].[msProcMatrix]
'
)
Including the extra levels in the name may solve your issue.

What is MYSQL equivalent for DATABASENAME..TABLENAME?

In MSSQL Server, I can execute the following SQL
SELECT * FROM DATABASENAME..TABLENAME
or
SELECT * FROM DATABASENAME.dbo.TABLENAME
What is the equivalent syntax on MYSQL to perform this SQL (DATABASE..TABLE)?
If you want to select all the records from a database table the syntax is quite similar it's
SELECT * FROM DatabaseName.TableName
You can do below
SELECT * from mydatabase1.tblName

pulling data from MySql Server using SQL server openquery

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
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')
try this:
SELECT * FROM OPENQUERY(LinkedServer, 'SELECT login_user FROM mydb_name.users')

Equivalent of the query from sql server in mysql

I have query in SQL Server , but i don't know its equivalent in mysql.
sql code is:
select username as t ,*from users
this code running in sql but this code not run in mysql.
is there way?
You can do either
select *, username as t from users
or
select username as t, users.* from users
for some reason in MySQL if you specify specific columns first you have to qualify the *
you can use this code :
select *,username as t from users