pulling data from MySql Server using SQL server openquery - mysql

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')

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 pass the results of a local MSSQL query into the query string for a linked MySQL database

Background:
I am working on a project which generates documents based on the contents of a remote MySQL (v8) database . To accomplish this, the program uses a local MSSQL (2017) database with a link to the remote MySQL db.
Issue:
I need to update the MySQL database based the contents of a query from the MSSQL as follows:
EXEC('UPDATE LinkedMySQLDB.TableToUpdate AS a SET a.MySQLField=''updated'' WHERE a.ID IN (LocalMSSQLDB.LocalTable.ID) ;') AT LinkedMySQLServer
However, every time I try this it throws an error at:
(LocalMSSQLDB.LocalTable.ID)
Question:
Is there a way to pass the results of a local query into the query string for the remote database?
Or, is there a different approach that is recommended?
Thanks in advance.
You are trying to access the values of LocalMSSQLDB.LocalTable.ID on MySQL server. That is not possible.
You could use a variable to calculate the statement.
In the example below I use some other names for the databases and fields. But you should get the general idea. By using concat I combine text parts with the result of the subqueries converted to a comma separated list using STRING_AGG.
declare #strSQL varchar(200);
select #strSQL=concat('UPDATE test.test set text=''updated'' WHERE ID in (',(SELECT STRING_AGG(ID, N', ') FROM [SQLTest].dbo.tblTest),');');
EXEC(#strSQL) AT MYSQL2;
This part (SELECT STRING_AGG(ID, N', ') FROM [SQLTest].dbo.tblTest) returns a string. If the local table contains the ID's 1 and 2, the result will be 1, 2. Concatenated with 'UPDATE test.test set text=''updated'' WHERE ID in (' and ');' the final result will be:
UPDATE test.test set text='updated' WHERE ID in (1, 2);
When this is send to the MySQL server named MYSQL2, you get the desired result.
On your servers it would be something like:
declare #strSQL varchar(200);
select #strSQL=concat('LinkedMySQLDB.TableToUpdate AS a SET a.MySQLField=''updated'' WHERE ID in (',(SELECT STRING_AGG(ID, N', ') FROM LocalMSSQLDB..LocalTable),');');
EXEC(#strSQL) AT LinkedMySQLServer;
Be sure to make room for all your id's in #strSQL. 200 characters might not be enough.

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.

sqlsrv find maximum from the table

Previously I was using the MySQL. With that I was able to use the query below to get the maximum number from the database.
Here 'No' is the varchar(10):
SELECT max(cast(No as unsigned)) as No FROM `tableName` LIMIT 1
The above query working fine in MySQL. I want to do the same thing in the MS SQL. When I run the same query, I get the following error:
Warning: sqlsrv_fetch_array() expects parameter 1 to be resource, boolean given
Any advice on this?
There is no LIMIT in SQL Server, no unsigned datatype, and no need to quote the table name.
Does this work:
SELECT max(cast(No as bigint)) as No FROM tableName

SELECT * FROM Linked MySQL server

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.