Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to use some sort of a temporary table with a lifespan of a query?

I have the following table that I generate with a certain query:

+-------------+------------------+
| error_codes | number_of_orders |
+-------------+------------------+
| 2066        |              104 |
| 5092        |              642 |
+-------------+------------------+

I’d like to append the corresponding message for each and every error. The error message table does not exist but I hope it’s possible to create a temporary one within a query which will be destroyed as soon as the query is complete.

+-------------+------------------+
| error_codes | error_message    |
+-------------+------------------+
| 2066        |         Tralalala|
| 5092        |         Ohje     |
+-------------+------------------+

Given the uniqueness of the error codes it can be used as the index to join the tables on. But the point is that I don’t want this table in the DB, it should be a virtual table or something of the sort.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The end result is expected to look as follows:

+--------------------------------+--------------+
| error_codes | number_of_orders | error_message|                                                                          
+-------------+------------------+--------------+
| 2066        |              104 | Tralalala    |
| 5092        |              642 | Ohje         |
+-------------+------------------+--------------+

>Solution :

Did you try using a Common Table Expression (CTE) in your query to create a virtual table containing the error messages ? Check this link

A simple usage example :

WITH ErrorMessages AS (
    SELECT 2066 AS error_codes, 'Tralalala' AS error_message
    UNION ALL
    SELECT 5092 AS error_codes, 'Ohje' AS error_message
)
SELECT 
    e.error_codes, 
    e.number_of_orders,
    em.error_message
FROM 
    your_table_name e
JOIN 
    ErrorMessages em ON e.error_codes = em.error_codes;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading