简体   繁体   中英

Inconsistent results querying temporary table in stored procedure

I am creating a temporary table alarm_clusters within a stored procedure. If I return results via SELECT * FROM alarm_clusters I get the expected results set:

id |alarm_timestamp     |alarm_rule_id |sensor_id |cluster_duration |time_delta |cluster_id |
---|--------------------|--------------|----------|-----------------|-----------|-----------|
1  |2018-01-01 00:00:00 |1             |1         |60               |0          |1          |
2  |2018-01-01 00:02:00 |1             |1         |60               |120        |2          |
3  |2018-01-01 00:02:20 |1             |1         |60               |20         |2          |

whereas if I return only a single column SELECT alarm_timestamp FROM alarm_clusters; I get the value of that column in the last row repeated:

alarm_timestamp     |
--------------------|
2018-01-01 00:02:20 |
2018-01-01 00:02:20 |
2018-01-01 00:02:20 |

I have created a DB Fiddle with two versions of the stored proc.

What have I missed?

You are getting the same time stamp because you are using the declared variable in your select query inside the procedure alarm_timestamp

Check below line in your getAlarmTimestamps procedure.

SELECT alarm_timestamp FROM alarm_clusters;

Ideally you should use different name variable rather than same as table column variable to avoid such kind of small mistakes.

Variable alarm_timestamp has last value which you have set in your cursor, and this value display total times of your rows in alarm_clusters table

I hope this will solve your confusion

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM