# Is it possible to use @variables with MySQL Selects? It's not working

**URL:** <https://forum.xojo.com/t/is-it-possible-to-use-variables-with-mysql-selects-its-not-working/81420>\
**Category:** Databases\
**Created:** [August 19, 2024, 8:47pm UTC](https://forum.xojo.com/t/is-it-possible-to-use-variables-with-mysql-selects-its-not-working/81420 "2024-08-19T20:47:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hugo\_Hollmann](https://forum.xojo.com/user_avatar/forum.xojo.com/hugo_hollmann/32/779_2.png) [@Hugo\_Hollmann](https://forum.xojo.com/u/Hugo_Hollmann)\
**Post date:** [August 19, 2024, 8:47pm UTC](https://forum.xojo.com/t/is-it-possible-to-use-variables-with-mysql-selects-its-not-working/81420/1 "2024-08-19T20:47:28Z")

</div>

I use to write mysql querys with TablePlus app and when I’m sure that the query is ok I copy it to xojo as a string variable to run with db.SelectSQL(query).

The problem is that I can’t specify a query with more than one line separated by a semicolon like

`"SET @var1="xyz"; SELECT @var1"`

it doesn’t work and I guess it has to do with the fact that the queries are one line only, but I can’t get the following to work either:

`"SELECT @id:=217 as idFac, total from Facs where id=@id"`

and I don’t understand why.

Can someone help me?

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [August 20, 2024, 3:22am UTC](https://forum.xojo.com/t/is-it-possible-to-use-variables-with-mysql-selects-its-not-working/81420/2 "2024-08-20T03:22:27Z")

</div>

Would using the built-in prepared statement syntax work?

```auto
var idnumber as integer = 127
selectSQL("select $1, total from Facs where id=$1", idnumber)

```

---

<div class="post-metadata">

**Author:** ![Hugo\_Hollmann](https://forum.xojo.com/user_avatar/forum.xojo.com/hugo_hollmann/32/779_2.png) [@Hugo\_Hollmann](https://forum.xojo.com/u/Hugo_Hollmann)\
**Post date:** [August 20, 2024, 5:25am UTC](https://forum.xojo.com/t/is-it-possible-to-use-variables-with-mysql-selects-its-not-working/81420/3 "2024-08-20T05:25:37Z")

</div>

It works with

```auto
var idnumber as integer = 127
SelectSQL("SELECT total from liquidaciones where idliquidacion=?", idnumber)

```

but I need to reuse that variable inside a little more complex query

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [August 20, 2024, 6:16am UTC](https://forum.xojo.com/t/is-it-possible-to-use-variables-with-mysql-selects-its-not-working/81420/4 "2024-08-20T06:16:32Z")

</div>

I’m not sure how the complex query changes things for you, but  
one messy but viable solution is to use a parameter table in a subquery.

> Create a table called **variables** , with an integer field called **num** and a string field called **str**  
> Add one row to it only.

Then, for your queries:  
**“Update variables set num = 127”**  
followed by

**“SELECT total from liquidaciones where idliquidacion=(select num from variables)”**

and you can use _**(select num from variables)**_ wherever you wanted the id to be used.  
The str field lets you do the same thing with string values.

---

<div class="post-metadata">

**Author:** ![Hugo\_Hollmann](https://forum.xojo.com/user_avatar/forum.xojo.com/hugo_hollmann/32/779_2.png) [@Hugo\_Hollmann](https://forum.xojo.com/u/Hugo_Hollmann)\
**Post date:** [August 20, 2024, 3:22pm UTC](https://forum.xojo.com/t/is-it-possible-to-use-variables-with-mysql-selects-its-not-working/81420/5 "2024-08-20T15:22:06Z")

</div>

Thanks Jeff. It’s a little messy as you say but it’s a solution indeed although I guess in terms of performance it’s not the best.  
What I don’t understand is where the problem or limitation is for Xojo with the sintax:

`"SELECT @id:=217 as idFac, total from Facs where id=@id"`

Is it in the plugin MySQLCommunityServer?

The query I need involve the union of several subqueries where in each one the specified @id variable must be used so if I specify @id in each one I don’t have to pass dozens of parameters that are always the same.

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [August 20, 2024, 3:38pm UTC](https://forum.xojo.com/t/is-it-possible-to-use-variables-with-mysql-selects-its-not-working/81420/6 "2024-08-20T15:38:12Z")

</div>

> [@Hugo\_Hollmann](#):
>
> the union of several subqueries

I would expect that you could use

```auto
total from Facs where id = ( select num from variables)  
followed by (stuff from other tables **where othertableid = facs.id** )

```

if you filter on one table, that table can be the key for all the others, can’t it?

---

<div class="post-metadata">

**Author:** ![Hugo\_Hollmann](https://forum.xojo.com/user_avatar/forum.xojo.com/hugo_hollmann/32/779_2.png) [@Hugo\_Hollmann](https://forum.xojo.com/u/Hugo_Hollmann)\
**Post date:** [August 20, 2024, 4:44pm UTC](https://forum.xojo.com/t/is-it-possible-to-use-variables-with-mysql-selects-its-not-working/81420/7 "2024-08-20T16:44:33Z")

</div>

Yes, it’s clear, some subqueries can run that way, other can not, thank you.

Main question is if is it possible tu use @variables with MySQLCommunityServer plugin. I can’t find this documented and I’m never quite sure if a correct query made in phpMyAdmin or TablePlus will work in Xojo.

---

<div class="post-metadata">

**Author:** ![Jürg\_Otter](https://forum.xojo.com/user_avatar/forum.xojo.com/j%C3%BCrg_otter/32/584_2.png) [@Jürg\_Otter](https://forum.xojo.com/u/J%C3%BCrg_Otter)\
**Post date:** [August 20, 2024, 7:54pm UTC](https://forum.xojo.com/t/is-it-possible-to-use-variables-with-mysql-selects-its-not-working/81420/8 "2024-08-20T19:54:00Z")

</div>

> [@Hugo\_Hollmann](#):
>
> The problem is that I can’t specify a query with more than one line separated by a semicolon like

I can’t get that to work, too.  
Maybe Xojo internally interprets a command starting with `SET...` to be a SQL-Execute, and therefore doesn’t populate the rowset of the later Select. Dunno…

Anyway…

> As of MySQL 3.23.6, you can assign a value returned by a `SELECT` statement to a variable, then refer to the variable later in your **mysql session**.

This seems to work: Execute the statements one after another (not multiple ones in a single call) in the same connection/session.

```auto
Var db As New MySQLCommunityServer
db.Host = "127.0.0.1"
db.UserName = "xxx"
db.Password = "yyy"
db.Port = 3306

Call db.Connect

db.ExecuteSQL("SET @x = 987;")
Var rs As RowSet = db.SelectSQL("SELECT @x as test;")

Var test As String = rs.Column("test").StringValue
Break

```

---

<div class="post-metadata">

**Author:** ![Jürg\_Otter](https://forum.xojo.com/user_avatar/forum.xojo.com/j%C3%BCrg_otter/32/584_2.png) [@Jürg\_Otter](https://forum.xojo.com/u/J%C3%BCrg_Otter)\
**Post date:** [August 20, 2024, 8:23pm UTC](https://forum.xojo.com/t/is-it-possible-to-use-variables-with-mysql-selects-its-not-working/81420/9 "2024-08-20T20:23:06Z")

</div>

> [@Hugo\_Hollmann](#):
>
> but I can’t get the following to work either:
> 
> `"SELECT @id:=217 as idFac, total from Facs where id=@id"`
> 
> and I don’t understand why.

To me this SQL statement doesn’t make much sense. I don’t see why a variable is needed as it’s meant to be probably simply: `SELECT id as idFac, total from Facs where id=217`

And I honestly haven’t used variables much.

What I think is happening here: The database server will only “execute” the `@id:=217` when filling the column value of a row. But it doesn’t come to that since the query (which most likely is interpreted first) is `where id=@id` - and at that time `@id` is not yet set.

This works:

```auto
Var db As New MySQLCommunityServer
db.Host = "127.0.0.1"
db.UserName = "xxx"
db.Password = "yyy"
db.Port = 3306
db.DatabaseName = "mysql"

db.ExecuteSQL("SET @host = 'localhost';")
Var rs As RowSet = = db.SelectSQL("SELECT Host, User FROM user WHERE Host=@host")

```

---

<div class="post-metadata">

**Author:** ![Hugo\_Hollmann](https://forum.xojo.com/user_avatar/forum.xojo.com/hugo_hollmann/32/779_2.png) [@Hugo\_Hollmann](https://forum.xojo.com/u/Hugo_Hollmann)\
**Post date:** [August 20, 2024, 9:41pm UTC](https://forum.xojo.com/t/is-it-possible-to-use-variables-with-mysql-selects-its-not-working/81420/10 "2024-08-20T21:41:34Z")

</div>

Thanks Jurg, I’ll try this. Intersting to know I can use mysql variables like that, never used variables in different calls within the same session, it makes sense.

That SQL statement was the simplest example I could think of, the query I need to write using variables involves unions of subqueries to different tables and if I can write them referencing variables in the same db it makes it much easier for me to pass them to Xojo.

Thanks to all.
