OiO.lk Blog SQL Sqlite union with non existing table
SQL

Sqlite union with non existing table


I have an application where tables are dynamically created, while reading data we Union All the data from various tables and show the results, but issue comes when a table is not existing.. Is there a better way to write this query so that it doesnt fail when a table doesnt exists..

SELECT * 
FROM 
 (
     SELECT * FROM
     (
     SELECT *,cast( unhex('54616730') as TEXT) as tagName 
                FROM [54616730] 
                WHERE sampleTime >= 0 AND sampleTime < 1
     ) 
     WHERE EXISTS (SELECT 1 FROM sqlite_master WHERE type="table" and name="54616730")
                
UNION ALL 
     SELECT * FROM
     (
     SELECT *,cast( unhex('54616731') as TEXT) as tagName 
                FROM [54616731] 
                WHERE sampleTime >= 0 AND sampleTime < 1
     ) 
     WHERE EXISTS (SELECT 1 FROM sqlite_master WHERE type="table" and name="54616731")
) 
ORDER BY tagName,sampleTime ASC;



You need to sign in to view this answers

Exit mobile version