Variables in sql statement make query hang












1















the following sql statement delivers what it should. The min value in a defined timeframe.



select value, datetime from Schuppen 
where (value = (select min(value) from Schuppen
where (measure = 'temp')
and datetime between '2018-11-01 00:00:00' and '2018-11-02 00:00:00'))
and datetime between '2018-11-01 00:00:00' and '2018-11-02 00:00:00';


When I use a variable, instead of the hardcoded dates, the statement hangs.



set @startdate = cast('2018-11-01 00:00:00' as datetime);   
select value, datetime from Schuppen
where (value = (select min(value) from Schuppen
where (measure = 'temp')
and datetime between @startdate and '2018-11-02 00:00:00'))
and datetime between '2018-11-01 00:00:00' and '2018-11-02 00:00:00';


I wonder why this statement does not work against my maria db version 10.1.26-MariaDB-0+deb9u1.



Tried via the mysql console.



mysql --user=XXXX --password=XXXX Outdoor-Air
...
Your MariaDB connection id is 194
Server version: 10.1.26-MariaDB-0+deb9u1 Debian 9.1

...

MariaDB [Outdoor-Air]> set @startdate = cast('2018-11-01 00:00:00' as datetime);
Query OK, 0 rows affected (0.00 sec)
MariaDB [Outdoor-Air]> select @startdate;
+---------------------+
| @startdate |
+---------------------+
| 2018-11-01 00:00:00 |
+---------------------+
1 row in set (0.01 sec)

MariaDB [Outdoor-Air]> select value, datetime from Schuppen where (value = (select min(value) from Schuppen where (measure = 'temp') and datetime between @startdate and '2018-11-02 00:00:00')) and datetime between '2018-11-01 00:00:00' and '2018-11-02 00:00:00';


Now the statement hangs.










share|improve this question

























  • Which version of Mariadb are you using ?

    – Madhur Bhaiya
    Nov 24 '18 at 18:02











  • 10.1.26-MariaDB-0+deb9u1

    – AndiGasman
    Nov 24 '18 at 18:05













  • Are you ensuring that both the queries are running in the same session ?

    – Madhur Bhaiya
    Nov 24 '18 at 18:08











  • I started using HeidiSQL Query windows and changed to mysql console, to rule out HeidiSQL. Just types both commands after each other. Or do i have to consider anything else in order to make sure both queries run in the the session?

    – AndiGasman
    Nov 24 '18 at 18:12













  • Run the set and select query together in terminal and confirm this behaviour. My doubt is that HeidiSQL might be running these two queries in two different sessions.

    – Madhur Bhaiya
    Nov 24 '18 at 18:13
















1















the following sql statement delivers what it should. The min value in a defined timeframe.



select value, datetime from Schuppen 
where (value = (select min(value) from Schuppen
where (measure = 'temp')
and datetime between '2018-11-01 00:00:00' and '2018-11-02 00:00:00'))
and datetime between '2018-11-01 00:00:00' and '2018-11-02 00:00:00';


When I use a variable, instead of the hardcoded dates, the statement hangs.



set @startdate = cast('2018-11-01 00:00:00' as datetime);   
select value, datetime from Schuppen
where (value = (select min(value) from Schuppen
where (measure = 'temp')
and datetime between @startdate and '2018-11-02 00:00:00'))
and datetime between '2018-11-01 00:00:00' and '2018-11-02 00:00:00';


I wonder why this statement does not work against my maria db version 10.1.26-MariaDB-0+deb9u1.



Tried via the mysql console.



mysql --user=XXXX --password=XXXX Outdoor-Air
...
Your MariaDB connection id is 194
Server version: 10.1.26-MariaDB-0+deb9u1 Debian 9.1

...

MariaDB [Outdoor-Air]> set @startdate = cast('2018-11-01 00:00:00' as datetime);
Query OK, 0 rows affected (0.00 sec)
MariaDB [Outdoor-Air]> select @startdate;
+---------------------+
| @startdate |
+---------------------+
| 2018-11-01 00:00:00 |
+---------------------+
1 row in set (0.01 sec)

MariaDB [Outdoor-Air]> select value, datetime from Schuppen where (value = (select min(value) from Schuppen where (measure = 'temp') and datetime between @startdate and '2018-11-02 00:00:00')) and datetime between '2018-11-01 00:00:00' and '2018-11-02 00:00:00';


Now the statement hangs.










share|improve this question

























  • Which version of Mariadb are you using ?

    – Madhur Bhaiya
    Nov 24 '18 at 18:02











  • 10.1.26-MariaDB-0+deb9u1

    – AndiGasman
    Nov 24 '18 at 18:05













  • Are you ensuring that both the queries are running in the same session ?

    – Madhur Bhaiya
    Nov 24 '18 at 18:08











  • I started using HeidiSQL Query windows and changed to mysql console, to rule out HeidiSQL. Just types both commands after each other. Or do i have to consider anything else in order to make sure both queries run in the the session?

    – AndiGasman
    Nov 24 '18 at 18:12













  • Run the set and select query together in terminal and confirm this behaviour. My doubt is that HeidiSQL might be running these two queries in two different sessions.

    – Madhur Bhaiya
    Nov 24 '18 at 18:13














1












1








1








the following sql statement delivers what it should. The min value in a defined timeframe.



select value, datetime from Schuppen 
where (value = (select min(value) from Schuppen
where (measure = 'temp')
and datetime between '2018-11-01 00:00:00' and '2018-11-02 00:00:00'))
and datetime between '2018-11-01 00:00:00' and '2018-11-02 00:00:00';


When I use a variable, instead of the hardcoded dates, the statement hangs.



set @startdate = cast('2018-11-01 00:00:00' as datetime);   
select value, datetime from Schuppen
where (value = (select min(value) from Schuppen
where (measure = 'temp')
and datetime between @startdate and '2018-11-02 00:00:00'))
and datetime between '2018-11-01 00:00:00' and '2018-11-02 00:00:00';


I wonder why this statement does not work against my maria db version 10.1.26-MariaDB-0+deb9u1.



Tried via the mysql console.



mysql --user=XXXX --password=XXXX Outdoor-Air
...
Your MariaDB connection id is 194
Server version: 10.1.26-MariaDB-0+deb9u1 Debian 9.1

...

MariaDB [Outdoor-Air]> set @startdate = cast('2018-11-01 00:00:00' as datetime);
Query OK, 0 rows affected (0.00 sec)
MariaDB [Outdoor-Air]> select @startdate;
+---------------------+
| @startdate |
+---------------------+
| 2018-11-01 00:00:00 |
+---------------------+
1 row in set (0.01 sec)

MariaDB [Outdoor-Air]> select value, datetime from Schuppen where (value = (select min(value) from Schuppen where (measure = 'temp') and datetime between @startdate and '2018-11-02 00:00:00')) and datetime between '2018-11-01 00:00:00' and '2018-11-02 00:00:00';


Now the statement hangs.










share|improve this question
















the following sql statement delivers what it should. The min value in a defined timeframe.



select value, datetime from Schuppen 
where (value = (select min(value) from Schuppen
where (measure = 'temp')
and datetime between '2018-11-01 00:00:00' and '2018-11-02 00:00:00'))
and datetime between '2018-11-01 00:00:00' and '2018-11-02 00:00:00';


When I use a variable, instead of the hardcoded dates, the statement hangs.



set @startdate = cast('2018-11-01 00:00:00' as datetime);   
select value, datetime from Schuppen
where (value = (select min(value) from Schuppen
where (measure = 'temp')
and datetime between @startdate and '2018-11-02 00:00:00'))
and datetime between '2018-11-01 00:00:00' and '2018-11-02 00:00:00';


I wonder why this statement does not work against my maria db version 10.1.26-MariaDB-0+deb9u1.



Tried via the mysql console.



mysql --user=XXXX --password=XXXX Outdoor-Air
...
Your MariaDB connection id is 194
Server version: 10.1.26-MariaDB-0+deb9u1 Debian 9.1

...

MariaDB [Outdoor-Air]> set @startdate = cast('2018-11-01 00:00:00' as datetime);
Query OK, 0 rows affected (0.00 sec)
MariaDB [Outdoor-Air]> select @startdate;
+---------------------+
| @startdate |
+---------------------+
| 2018-11-01 00:00:00 |
+---------------------+
1 row in set (0.01 sec)

MariaDB [Outdoor-Air]> select value, datetime from Schuppen where (value = (select min(value) from Schuppen where (measure = 'temp') and datetime between @startdate and '2018-11-02 00:00:00')) and datetime between '2018-11-01 00:00:00' and '2018-11-02 00:00:00';


Now the statement hangs.







variables mariadb






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 18:39







AndiGasman

















asked Nov 24 '18 at 18:00









AndiGasmanAndiGasman

155




155













  • Which version of Mariadb are you using ?

    – Madhur Bhaiya
    Nov 24 '18 at 18:02











  • 10.1.26-MariaDB-0+deb9u1

    – AndiGasman
    Nov 24 '18 at 18:05













  • Are you ensuring that both the queries are running in the same session ?

    – Madhur Bhaiya
    Nov 24 '18 at 18:08











  • I started using HeidiSQL Query windows and changed to mysql console, to rule out HeidiSQL. Just types both commands after each other. Or do i have to consider anything else in order to make sure both queries run in the the session?

    – AndiGasman
    Nov 24 '18 at 18:12













  • Run the set and select query together in terminal and confirm this behaviour. My doubt is that HeidiSQL might be running these two queries in two different sessions.

    – Madhur Bhaiya
    Nov 24 '18 at 18:13



















  • Which version of Mariadb are you using ?

    – Madhur Bhaiya
    Nov 24 '18 at 18:02











  • 10.1.26-MariaDB-0+deb9u1

    – AndiGasman
    Nov 24 '18 at 18:05













  • Are you ensuring that both the queries are running in the same session ?

    – Madhur Bhaiya
    Nov 24 '18 at 18:08











  • I started using HeidiSQL Query windows and changed to mysql console, to rule out HeidiSQL. Just types both commands after each other. Or do i have to consider anything else in order to make sure both queries run in the the session?

    – AndiGasman
    Nov 24 '18 at 18:12













  • Run the set and select query together in terminal and confirm this behaviour. My doubt is that HeidiSQL might be running these two queries in two different sessions.

    – Madhur Bhaiya
    Nov 24 '18 at 18:13

















Which version of Mariadb are you using ?

– Madhur Bhaiya
Nov 24 '18 at 18:02





Which version of Mariadb are you using ?

– Madhur Bhaiya
Nov 24 '18 at 18:02













10.1.26-MariaDB-0+deb9u1

– AndiGasman
Nov 24 '18 at 18:05







10.1.26-MariaDB-0+deb9u1

– AndiGasman
Nov 24 '18 at 18:05















Are you ensuring that both the queries are running in the same session ?

– Madhur Bhaiya
Nov 24 '18 at 18:08





Are you ensuring that both the queries are running in the same session ?

– Madhur Bhaiya
Nov 24 '18 at 18:08













I started using HeidiSQL Query windows and changed to mysql console, to rule out HeidiSQL. Just types both commands after each other. Or do i have to consider anything else in order to make sure both queries run in the the session?

– AndiGasman
Nov 24 '18 at 18:12







I started using HeidiSQL Query windows and changed to mysql console, to rule out HeidiSQL. Just types both commands after each other. Or do i have to consider anything else in order to make sure both queries run in the the session?

– AndiGasman
Nov 24 '18 at 18:12















Run the set and select query together in terminal and confirm this behaviour. My doubt is that HeidiSQL might be running these two queries in two different sessions.

– Madhur Bhaiya
Nov 24 '18 at 18:13





Run the set and select query together in terminal and confirm this behaviour. My doubt is that HeidiSQL might be running these two queries in two different sessions.

– Madhur Bhaiya
Nov 24 '18 at 18:13












2 Answers
2






active

oldest

votes


















1














SELECT  `value`, `datetime`
FROM Schuppen
WHERE measure = 'temp'
AND `datetime` >= '2018-11-01'
AND `datetime` < '2018-11-01' + INTERVAL 1 DAY
ORDER BY `value` ASC
LIMIT 1;


And this should help:



INDEX(measure, datetime, value)


One potential difference between this formulation and yours: This will show only one row if the lowest value occurs more than once on that day.






share|improve this answer
























  • That's the perfect answer. Avoiding subqueries also allows the usage of variables without optimizer/variable hazzle.

    – AndiGasman
    Nov 25 '18 at 10:28



















0














Ensure that both the queries, viz., SET and SELECT statements are running within a same session. @startdate will persist across a single session only.



You can also convert these two queries into a single query utilzing CROSS JOIN with Derived Table.



SELECT value,
`datetime`
FROM Schuppen
CROSS JOIN (SELECT @startdate := cast('2018-11-01 00:00:00' as datetime)) vars
WHERE ( value = (SELECT Min(value)
FROM Schuppen
WHERE ( measure = 'temp' )
AND `datetime` BETWEEN
@startdate AND '2018-11-02 00:00:00') )
AND `datetime` BETWEEN '2018-11-01 00:00:00' AND '2018-11-02 00:00:00';





share|improve this answer


























  • I tried your approach (copy paste into sql console) but the effect is the same. The Statement hangs.

    – AndiGasman
    Nov 24 '18 at 18:27











  • @AndiGasman check the updated query. Added backticks around datetime as it is a keyword. I am also not sure about cast .. as datetime part. Also casting is not required; it should work as it is

    – Madhur Bhaiya
    Nov 24 '18 at 18:32











  • Hate to say it but same effect with your updated query (with backticks and no cast).

    – AndiGasman
    Nov 24 '18 at 18:37











  • @AndiGasman is it possible for you to put some sample data in a fiddle ? Sometimes looking at relevant, minimal yet complete sample data helps. There is definitely a very minor error getting overlooked. You can setup the fiddle here: db-fiddle.com and save and share the link with me.

    – Madhur Bhaiya
    Nov 24 '18 at 18:39











  • Nice tool, never heard of it. I put in the create and inserts and my original sql statement with variable on the right and it worked. db-fiddle.com/#&togetherjs=hl4MJkwBtm

    – AndiGasman
    Nov 24 '18 at 18:52













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460954%2fvariables-in-sql-statement-make-query-hang%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














SELECT  `value`, `datetime`
FROM Schuppen
WHERE measure = 'temp'
AND `datetime` >= '2018-11-01'
AND `datetime` < '2018-11-01' + INTERVAL 1 DAY
ORDER BY `value` ASC
LIMIT 1;


And this should help:



INDEX(measure, datetime, value)


One potential difference between this formulation and yours: This will show only one row if the lowest value occurs more than once on that day.






share|improve this answer
























  • That's the perfect answer. Avoiding subqueries also allows the usage of variables without optimizer/variable hazzle.

    – AndiGasman
    Nov 25 '18 at 10:28
















1














SELECT  `value`, `datetime`
FROM Schuppen
WHERE measure = 'temp'
AND `datetime` >= '2018-11-01'
AND `datetime` < '2018-11-01' + INTERVAL 1 DAY
ORDER BY `value` ASC
LIMIT 1;


And this should help:



INDEX(measure, datetime, value)


One potential difference between this formulation and yours: This will show only one row if the lowest value occurs more than once on that day.






share|improve this answer
























  • That's the perfect answer. Avoiding subqueries also allows the usage of variables without optimizer/variable hazzle.

    – AndiGasman
    Nov 25 '18 at 10:28














1












1








1







SELECT  `value`, `datetime`
FROM Schuppen
WHERE measure = 'temp'
AND `datetime` >= '2018-11-01'
AND `datetime` < '2018-11-01' + INTERVAL 1 DAY
ORDER BY `value` ASC
LIMIT 1;


And this should help:



INDEX(measure, datetime, value)


One potential difference between this formulation and yours: This will show only one row if the lowest value occurs more than once on that day.






share|improve this answer













SELECT  `value`, `datetime`
FROM Schuppen
WHERE measure = 'temp'
AND `datetime` >= '2018-11-01'
AND `datetime` < '2018-11-01' + INTERVAL 1 DAY
ORDER BY `value` ASC
LIMIT 1;


And this should help:



INDEX(measure, datetime, value)


One potential difference between this formulation and yours: This will show only one row if the lowest value occurs more than once on that day.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 23:12









Rick JamesRick James

66.8k55899




66.8k55899













  • That's the perfect answer. Avoiding subqueries also allows the usage of variables without optimizer/variable hazzle.

    – AndiGasman
    Nov 25 '18 at 10:28



















  • That's the perfect answer. Avoiding subqueries also allows the usage of variables without optimizer/variable hazzle.

    – AndiGasman
    Nov 25 '18 at 10:28

















That's the perfect answer. Avoiding subqueries also allows the usage of variables without optimizer/variable hazzle.

– AndiGasman
Nov 25 '18 at 10:28





That's the perfect answer. Avoiding subqueries also allows the usage of variables without optimizer/variable hazzle.

– AndiGasman
Nov 25 '18 at 10:28













0














Ensure that both the queries, viz., SET and SELECT statements are running within a same session. @startdate will persist across a single session only.



You can also convert these two queries into a single query utilzing CROSS JOIN with Derived Table.



SELECT value,
`datetime`
FROM Schuppen
CROSS JOIN (SELECT @startdate := cast('2018-11-01 00:00:00' as datetime)) vars
WHERE ( value = (SELECT Min(value)
FROM Schuppen
WHERE ( measure = 'temp' )
AND `datetime` BETWEEN
@startdate AND '2018-11-02 00:00:00') )
AND `datetime` BETWEEN '2018-11-01 00:00:00' AND '2018-11-02 00:00:00';





share|improve this answer


























  • I tried your approach (copy paste into sql console) but the effect is the same. The Statement hangs.

    – AndiGasman
    Nov 24 '18 at 18:27











  • @AndiGasman check the updated query. Added backticks around datetime as it is a keyword. I am also not sure about cast .. as datetime part. Also casting is not required; it should work as it is

    – Madhur Bhaiya
    Nov 24 '18 at 18:32











  • Hate to say it but same effect with your updated query (with backticks and no cast).

    – AndiGasman
    Nov 24 '18 at 18:37











  • @AndiGasman is it possible for you to put some sample data in a fiddle ? Sometimes looking at relevant, minimal yet complete sample data helps. There is definitely a very minor error getting overlooked. You can setup the fiddle here: db-fiddle.com and save and share the link with me.

    – Madhur Bhaiya
    Nov 24 '18 at 18:39











  • Nice tool, never heard of it. I put in the create and inserts and my original sql statement with variable on the right and it worked. db-fiddle.com/#&togetherjs=hl4MJkwBtm

    – AndiGasman
    Nov 24 '18 at 18:52


















0














Ensure that both the queries, viz., SET and SELECT statements are running within a same session. @startdate will persist across a single session only.



You can also convert these two queries into a single query utilzing CROSS JOIN with Derived Table.



SELECT value,
`datetime`
FROM Schuppen
CROSS JOIN (SELECT @startdate := cast('2018-11-01 00:00:00' as datetime)) vars
WHERE ( value = (SELECT Min(value)
FROM Schuppen
WHERE ( measure = 'temp' )
AND `datetime` BETWEEN
@startdate AND '2018-11-02 00:00:00') )
AND `datetime` BETWEEN '2018-11-01 00:00:00' AND '2018-11-02 00:00:00';





share|improve this answer


























  • I tried your approach (copy paste into sql console) but the effect is the same. The Statement hangs.

    – AndiGasman
    Nov 24 '18 at 18:27











  • @AndiGasman check the updated query. Added backticks around datetime as it is a keyword. I am also not sure about cast .. as datetime part. Also casting is not required; it should work as it is

    – Madhur Bhaiya
    Nov 24 '18 at 18:32











  • Hate to say it but same effect with your updated query (with backticks and no cast).

    – AndiGasman
    Nov 24 '18 at 18:37











  • @AndiGasman is it possible for you to put some sample data in a fiddle ? Sometimes looking at relevant, minimal yet complete sample data helps. There is definitely a very minor error getting overlooked. You can setup the fiddle here: db-fiddle.com and save and share the link with me.

    – Madhur Bhaiya
    Nov 24 '18 at 18:39











  • Nice tool, never heard of it. I put in the create and inserts and my original sql statement with variable on the right and it worked. db-fiddle.com/#&togetherjs=hl4MJkwBtm

    – AndiGasman
    Nov 24 '18 at 18:52
















0












0








0







Ensure that both the queries, viz., SET and SELECT statements are running within a same session. @startdate will persist across a single session only.



You can also convert these two queries into a single query utilzing CROSS JOIN with Derived Table.



SELECT value,
`datetime`
FROM Schuppen
CROSS JOIN (SELECT @startdate := cast('2018-11-01 00:00:00' as datetime)) vars
WHERE ( value = (SELECT Min(value)
FROM Schuppen
WHERE ( measure = 'temp' )
AND `datetime` BETWEEN
@startdate AND '2018-11-02 00:00:00') )
AND `datetime` BETWEEN '2018-11-01 00:00:00' AND '2018-11-02 00:00:00';





share|improve this answer















Ensure that both the queries, viz., SET and SELECT statements are running within a same session. @startdate will persist across a single session only.



You can also convert these two queries into a single query utilzing CROSS JOIN with Derived Table.



SELECT value,
`datetime`
FROM Schuppen
CROSS JOIN (SELECT @startdate := cast('2018-11-01 00:00:00' as datetime)) vars
WHERE ( value = (SELECT Min(value)
FROM Schuppen
WHERE ( measure = 'temp' )
AND `datetime` BETWEEN
@startdate AND '2018-11-02 00:00:00') )
AND `datetime` BETWEEN '2018-11-01 00:00:00' AND '2018-11-02 00:00:00';






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 19:04

























answered Nov 24 '18 at 18:17









Madhur BhaiyaMadhur Bhaiya

19.6k62236




19.6k62236













  • I tried your approach (copy paste into sql console) but the effect is the same. The Statement hangs.

    – AndiGasman
    Nov 24 '18 at 18:27











  • @AndiGasman check the updated query. Added backticks around datetime as it is a keyword. I am also not sure about cast .. as datetime part. Also casting is not required; it should work as it is

    – Madhur Bhaiya
    Nov 24 '18 at 18:32











  • Hate to say it but same effect with your updated query (with backticks and no cast).

    – AndiGasman
    Nov 24 '18 at 18:37











  • @AndiGasman is it possible for you to put some sample data in a fiddle ? Sometimes looking at relevant, minimal yet complete sample data helps. There is definitely a very minor error getting overlooked. You can setup the fiddle here: db-fiddle.com and save and share the link with me.

    – Madhur Bhaiya
    Nov 24 '18 at 18:39











  • Nice tool, never heard of it. I put in the create and inserts and my original sql statement with variable on the right and it worked. db-fiddle.com/#&togetherjs=hl4MJkwBtm

    – AndiGasman
    Nov 24 '18 at 18:52





















  • I tried your approach (copy paste into sql console) but the effect is the same. The Statement hangs.

    – AndiGasman
    Nov 24 '18 at 18:27











  • @AndiGasman check the updated query. Added backticks around datetime as it is a keyword. I am also not sure about cast .. as datetime part. Also casting is not required; it should work as it is

    – Madhur Bhaiya
    Nov 24 '18 at 18:32











  • Hate to say it but same effect with your updated query (with backticks and no cast).

    – AndiGasman
    Nov 24 '18 at 18:37











  • @AndiGasman is it possible for you to put some sample data in a fiddle ? Sometimes looking at relevant, minimal yet complete sample data helps. There is definitely a very minor error getting overlooked. You can setup the fiddle here: db-fiddle.com and save and share the link with me.

    – Madhur Bhaiya
    Nov 24 '18 at 18:39











  • Nice tool, never heard of it. I put in the create and inserts and my original sql statement with variable on the right and it worked. db-fiddle.com/#&togetherjs=hl4MJkwBtm

    – AndiGasman
    Nov 24 '18 at 18:52



















I tried your approach (copy paste into sql console) but the effect is the same. The Statement hangs.

– AndiGasman
Nov 24 '18 at 18:27





I tried your approach (copy paste into sql console) but the effect is the same. The Statement hangs.

– AndiGasman
Nov 24 '18 at 18:27













@AndiGasman check the updated query. Added backticks around datetime as it is a keyword. I am also not sure about cast .. as datetime part. Also casting is not required; it should work as it is

– Madhur Bhaiya
Nov 24 '18 at 18:32





@AndiGasman check the updated query. Added backticks around datetime as it is a keyword. I am also not sure about cast .. as datetime part. Also casting is not required; it should work as it is

– Madhur Bhaiya
Nov 24 '18 at 18:32













Hate to say it but same effect with your updated query (with backticks and no cast).

– AndiGasman
Nov 24 '18 at 18:37





Hate to say it but same effect with your updated query (with backticks and no cast).

– AndiGasman
Nov 24 '18 at 18:37













@AndiGasman is it possible for you to put some sample data in a fiddle ? Sometimes looking at relevant, minimal yet complete sample data helps. There is definitely a very minor error getting overlooked. You can setup the fiddle here: db-fiddle.com and save and share the link with me.

– Madhur Bhaiya
Nov 24 '18 at 18:39





@AndiGasman is it possible for you to put some sample data in a fiddle ? Sometimes looking at relevant, minimal yet complete sample data helps. There is definitely a very minor error getting overlooked. You can setup the fiddle here: db-fiddle.com and save and share the link with me.

– Madhur Bhaiya
Nov 24 '18 at 18:39













Nice tool, never heard of it. I put in the create and inserts and my original sql statement with variable on the right and it worked. db-fiddle.com/#&togetherjs=hl4MJkwBtm

– AndiGasman
Nov 24 '18 at 18:52







Nice tool, never heard of it. I put in the create and inserts and my original sql statement with variable on the right and it worked. db-fiddle.com/#&togetherjs=hl4MJkwBtm

– AndiGasman
Nov 24 '18 at 18:52




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460954%2fvariables-in-sql-statement-make-query-hang%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)