Forum Discussion

alicia57's avatar
alicia57
Helper I
9 years ago
Solved

Use a parameter in a query in a MySQL database

Hello,

 

I would like to use a list of results from a first query in a second one.

 

I launch a first query on a SQL Server database. I click on a column "id" and "Add as New Query", then "New Parameter" : my parameter is the list of id named "tx_id".

 

 Now I would like to launch a query on another database (MySQL this time) using this parameter. 

select * from...

where id = #'tx_id'

I tried this query with and without quotes, with and without double quotes, with @ and $ instead of # but I always have an error.

 

Would you have an idea?

 

Thank you,

Alicia

  • Hi

    because tx_id is a series of id numbers you need to pass them into SQL in a format it expects.

    I used Text.Combine to create my parameter so it read '123','124','125' which is what i needed to pass to the SQL where clause. As you are using a number then i do not think you need the '' so you need to pass into SQL IN clause (123,123,123,123) etc if tx_id looks like 123,123,123,123 then you can concatenate it into your SQL like your example.

    So the key to solving this is to make sure that tx_id results look exactly what SQL requires.

    Regards

    Mike

16 Replies

    • alicia57's avatar
      alicia57
      Helper I

      If I write "... where id in @tx_id" the error is "MySQL: Fatal error encountered during command execution."

       

      Otherwise

       

      the error is "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@'tx_id'' at line 13

       

      I suppose it is a syntax error because when I replace the condition by "where id in (1,2)" I obtain results I am expecting.

       

      I have also tried to modify the query directly in the Advanced Editor : 

      "let

      dbQuery="select ... from ... where id in" & @tx_id,

      Source = MySQL.Database("server", "name_database", [Query=dbQuery, CommandTimeout=#duration(0, 16, 40, 0), ReturnSingleDatabase=true])
      in
      Source"

       

      but I have no result.

  • hi alicia, help-me. I want to create a query in mysql and i dont know how create

    if I select mysql, I put the IP and user, and I see tables, but   I what create a query thats use many tables .

    can you help me?

    Humberto Buzzi