My MySQL 'select into' clause is not working like on SQL? - mysql

I have 'select into' local variables in my MySQL database and it's only working on first variable. I am not sure why it's not working. 'select into' works like in SQL on MySQL?
SELECT
adresse_a,
adresse_b,
adresse_c,
adresse_d_oid
into
#a,
#b,
#c,
#d_oid
from table where oid = my_oid limit 1;
and it's in procedure / loop and always shows
a - good value
b,c,d is empty / null.

Your current syntax is perfectly fine. See it running in Fiddle at: http://sqlfiddle.com/#!2/a2581/25436
Using Mysql Workbench may create problem as reported here: https://stackoverflow.com/a/7338940/
For the Workbench, query could be re-written like this:
SELECT
#a:=adresse_a,
#b:=adresse_b
FROM `table`
WHERE id = my_oid limit 1;

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

Cannot use regexp_substr in the old version MySQL Query Browser

I am using MYSQL Query Browser version 1.2.11, and write the below SQL query cannot work in MYSQL Query Browser version 1.2.11.
select id,name
from(select t.*,cast(regexp_substr(name,'[0-9]+') as unsigned) col1
,cast(regexp_substr(name,'[0-9]+',1,2) as unsigned) col2
,cast(regexp_substr(name,'[0-9]+',1,3) as unsigned) col3
,cast(regexp_substr(name,'[0-9]+',1,4) as unsigned) col4
from filing_code_management t) c
order by col1,col2,col3,col4
I have tried this SQL query to use in the online dbfiddle, just SQL 8.0 can work in https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=e43d94d0418854375a07c2bbf1e44339
May I know got any method can replace this SQL query code to suitable in my old MYSQL DB version 5.5? My side need to use old version to run the SQL. Hope someone can guide me, thanks.
The error appears FUNCTION db_12668458.regexp_substr does not exist in This old version of DB.

MySQL temporary variable in table name

I have a series of tables with the same prefix, and I need to select data from the latest version --whose postfix with the highest numeric number. Here is what I have:
SELECT
#latest_version_number :=
MAX(
CAST(SUBSTRING_INDEX(table_name,'_',-1) AS UNSIGNED)
)
FROM information_schema.tables
WHERE lower(table_name) like '{table_prefix}%';
SELECT
*
FROM CONCAT('`{table_prefix}', CAST(#latest_version_number AS CHAR), '`')
It behaved like what I expected when ran as 2 separate queries in the console. But I got "syntax error" trying to run it as a single query. What's the cleanest way to refactor this into a single query? Thanks
The only way I'm aware that you can use a variable in a table name in MySQL is using the prepare + execute statements.
Here is an example I found online that gives you exact instructions.
https://www.tutorialspoint.com/set-user-defined-variable-with-table-name-in-mysql-prepare-statement

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.

SQL Like with double pipe concatenation

I'm having a problem with a SQL query that must match the username of a user out of a column that contains all the users usernames.
So the column will contain something like:
|USER1|USER2|USER3|USER11|USER22|
The user have pipes on the left and right to prevent "USER1" be matched even in "USER11".
My query is
SELECT * FROM myTable WHERE CONCATUSERS LIKE ('%|' || 'USER1' || '|%')
Note that the USER1 in the query is a variable generated from our code so I must keep the concatenation syntax and I must use a standard syntax too (the code will run in mySQL, SQLServer etc..
So what is the correct way of concatenating strings in a LIKE clause?
MySQL uses the double pipes for concat. SQL Server you can use +.
SELECT *
FROM myTable
WHERE CONCATUSERS LIKE ('%' + '|user1|' + '%')
Use CONCAT.
(available on SQL server 2012 and beyond)
It has the benefit that it implicitly converts types to add the value to the string. And it's not just available on Sql Server and MySql.
SELECT * FROM myTable WHERE CONCATUSERS LIKE CONCAT('%|','USER1','|%');
Do note that in MySQL the result will be NULL if one of the concatenated values is NULL. But not on Sql Server.
It's just Oracle that's being stubborn by only allowing 2 values to that function.
So if the SQL needs to run unchanged on MySQL, a recent Sql Server AND Oracle then this should work:
SELECT * FROM myTable WHERE CONCATUSERS LIKE CONCAT(CONCAT('%|','USER1'),'|%');
You can go with CONCAT function.
As it is supported in both SQL and MySQL
SELECT *
FROM xpat
WHERE plname LIKE concat('%' ,'|user1|' ,'%');