I have a UNION statement that executes just fine by itself:
SELECT "1999999999" AS MobileNo, "Test" AS FirstName, "Last" AS LastName, "268" AS TemplateID, "" AS MISC1, "" AS MISC2 UNION SELECT cust.cellp AS MobileNo, acct.firstname AS FirstName, acct.lastname AS LastName, "268" AS TemplateID, "" AS MISC1, "" AS MISC2 FROM acct INNER JOIN cust ON (cust.socsec=acct.socsec)
However when I try to wrap it with a CREATE TEMPORARY TABLE:
CREATE TEMPORARY TABLE temptable (SELECT "1999999999" AS MobileNo, "Test" AS FirstName, "Last" AS LastName, "268" AS TemplateID, "" AS MISC1, "" AS MISC2 UNION SELECT cust.cellp AS MobileNo, acct.firstname AS FirstName, acct.lastname AS LastName, "268" AS TemplateID, "" AS MISC1, "" AS MISC2 FROM acct INNER JOIN cust ON (cust.socsec=acct.socsec))
I get this error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION SELECT cust.cellp AS MobileNo, acct.firstname AS FirstName, ac' at line 1
Either SELECT statement works fine with the CREATE TEMPORARY TABLE around it, it is just the UNION that doesn't work. The MySQL error returned is vague so I can't quite figure out what the issue is. I tried wrapping each SELECT statement in parenthesis but it didn't like that either.
I know that in theory I could just create the temporary table using one SELECT and then add the data from the second one to the table, but that solution is not really feasible in this case as it is a report generator I am using that is pretty strict about only allowing a single MySQL statement as input so unless I wanted to redesign the whole system (I don't, nor would management want me to spend time on that right now) I have to find a way to make this work in a single MySQL statement.
Any ideas on what I'm doing wrong here?
Table variables are created like any other variable, using the DECLARE statement. Many believe that table variables exist only in memory, but that is simply not true. They reside in the tempdb database much like local SQL Server temp tables.
To create a Global Temporary Table, add the “##” symbol before the table name. Global Temporary Tables are visible to all connections and Dropped when the last connection referencing the table is closed. Global Table Name must have an Unique Table Name.
SQL Server provides two types of temporary tables according to their scope: Local Temporary Table. Global Temporary Table.
Like Local temporary tables, Global temporary tables (they begin with ##) are automatically dropped when the session that created the table ends: However, because global tables aren't private to the process that created it, they must persist thereafter until the last Transact-SQL statement that was actively referencing ...
Here' a workaround:
CREATE TABLE AS
SELECT *
FROM (
SELECT ...
UNION ALL
SELECT ...
) AS foo
You can't do the union directly for the create table
, but you can make it a sub-select:
mysql> create table foo as (select * from ((select 'foo') union all (select 'bar')) as foo);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With