MySql - Create a table and select from it in the same procedure - mysql

I’m trying to execute this simple code as a procedure in MySql using the PHPMyAdmin
CREATE TEMPORARY TABLE IF NOT EXISTS jobs AS (SELECT
*
FROM
all_jobs
WHERE
job_object_type LIKE 'jobName'
OR
job_object_type LIKE 'jobStatus');
SELECT
*
FROM
jobs
But I’m getting an error (#1064) because of my second SELECT query. I’m coming from MS-SQL and there the SELECT would be executed without any problems. How can I execute the CREATE TABLE X query and direct after that the SELECT * FROM X query in the same procedure?
EDIT: forgot to mention that I’m using the MariaDB

Well that is how it's done using the PHPMyAdmin:
BEGIN
CREATE TEMPORARY TABLE IF NOT EXISTS jobs AS (SELECT
*
FROM
all_jobs
WHERE
job_object_type LIKE 'jobName'
OR
job_object_type LIKE 'jobStatus');
SELECT
*
FROM
jobs;
END
Now everything is being executed as it should be.

Related

SET NOCOUNT ON for google bigquery

I'm trying to run a pass-through query on MS Access via BigQuery ODBC. Here is the simple query:
begin
create temp table table2 as(
select * from table1
);
end;
select * from table2;
After googling around a bit, it seems like MS Access stops the query after temp tables are created. As such this returns no results. The workaround to this to set nocount on. Does such functionality exist in BigQuery?
Thank you

MySQL Script Applied to Many Databases

I have one MySQL script and many databases that the script is to be applied.Simplified script is like this.
CREATE TABLE aaa1 AS SELECT * FROM dbname1.aaa ;
CREATE TABLE aaa2 AS SELECT * FROM dbname2.aaa ;
CREATE TABLE aaa3 AS SELECT * FROM dbname3.aaa ;
...
CREATE TABLE bbb1 AS SELECT * FROM dbname1.bbb ;
CREATE TABLE bbb2 AS SELECT * FROM dbname2.bbb ;
CREATE TABLE bbb3 AS SELECT * FROM dbname3.bbb ;
My Problem:
The easiest way that I can think of is applying "USE dbname" inside the stored procedure, but it doesn't work. I searched the internet and some people say dynamic SQL for this. I tried this and that but all of them are not working.
Could you help me resolve this with a working script example? Thank you in advance.

MySQL VIEW Incorrect key file for table try to repair it

I have a FooBar view like:
CREATE VIEW `FooBar` AS
SELECT * FROM `Foo`.`Bar`
UNION ALL
SELECT * FROM `Foo1`.`Bar`
When I SELECT * FROM FooBar I get:
Incorrect key file for table '/tmp/#sql_1234_5.MYI'; try to repair it
When I run the select statements like this:
SELECT * FROM `Foo`.`Bar`
UNION ALL
SELECT * FROM `Foo1`.`Bar`
Everything goes well. The problem is clearly with my FooBar view.
When I REPAIR TABLE FooBar I get
'WhiskerDatabase.VisualDiscrimSuperimposed_Results' is not BASE TABLE
Corrupt
I tried to DROP VIEW FooBarand reCREATE VIEW FooBar... but the problems persists.
And I can't locate the sql_1234_5.MYI file in /var/lib/mysql/MyDB/.
Found the explanation here. MySQL build a temporary file. The temporary file is too big to fit in memory.
Adjusting the system memory or using a LIMIT clause could help solve the problem.
I solve my problem by creating a procedure that create a table instead of a view. Such as:
DROP TABLE `FooBar` IF EXISTS;
CREATE TABLE `FooBar` AS SELECT * FROM `Foo`.`Bar`;
INSERT INTO `FooBar` SELECT * FROM `Foo1`.`Bar`;

How to run complex sql query in Laravel

I have some complex SQL which consists of a series of non-query statements, that make use of temporary tables in MySql, and then a SELECT statement at the end to return the result.
e.g
DROP TABLE IF EXISTS temp_foo;
CREATE TEMPORARY TABLE IF NOT EXISTS temp_foo AS (
SELECT *
FROM foo
);
SELECT * from temp_foo;
How can I run this all in the one DB call from Laravel and get the results of that last SELECT statment?
I've tried doing something like this in laravel, but it gives me a MySql syntax error, which is strange as that exact sql works ok when I run it directly in MySQl.
DB::select("
DROP TABLE IF EXISTS temp_foo;
CREATE TEMPORARY TABLE IF NOT EXISTS temp_foo AS (
SELECT *
FROM foo
);
SELECT * from temp_foo;
");
Any ideas on how can make this work?
You need to use DB::raw() to make your query work, for example:
DB::select(DB::raw("
DROP TABLE IF EXISTS temp_foo;
CREATE TEMPORARY TABLE IF NOT EXISTS temp_foo AS (
SELECT *
FROM foo
);
SELECT * from temp_foo;
"));
I think it's even possible to simply use selectRaw() but I'm not sure. Also, you can select all rows from temp_foo like this:
DB::table('temp_foo')->get();
You need to look at DB::raw() inside the select(), but also, try using this:
DB::statement('drop table users');
i've done something similar in a migration where i had to create and run a stored procedure. in my up() i used something like this.
public function up() {
$sql = <<<SQL
DROP TABLE IF EXISTS temp_foo;
CREATE TEMPORARY TABLE IF NOT EXISTS temp_foo AS (
SELECT *
FROM foo
);
SELECT * from temp_foo;
SQL;
DB::connection()->getPdo()->exec($sql);
}
you may be able to use
DB::connection()->getPdo()->exec($sql);

Can't UNION ALL on a temporary table?

I'm trying to run the following simple test- creating a temp table, and then UNIONing two different selections:
CREATE TEMPORARY TABLE tmp
SELECT * FROM people;
SELECT * FROM tmp
UNION ALL
SELECT * FROM tmp;
But get a #1137 - Can't reopen table: 'tmp'
I thought temp tables were supposed to last the session. What's the problem here?
This error indicates that the way in which MySQL tables manages the temporary tables has been changed which in turn affects the joins, unions as well as subqueries. To fix MySQL error "can’t reopen table", try out the following solution:
mysql> CREATE TEMPORARY TABLE tmp_journals_2 LIKE tmp_journals;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO tmp_journals_2 SELECT * FROM tmp_journals;
After this you can perform the union operation.
Useful reading
http://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html
http://www.mysqlrepair.org/mysqlrepair/cant-reopen-table.php
Figured it out thanks to sshekar's answer- the solution in this case would be
Create an empty temp table
Insert the results we want to UNION into the table separately
Query the temp table
SQL:
CREATE TEMPORARY TABLE tmp LIKE people;
INSERT INTO tmp SELECT * FROM people; /* First half of UNION */
INSERT INTO tmp SELECT * FROM people; /* Second half of UNION */
SELECT * FROM tmp;
(See Using MySQL Temporary Tables to save your brain)
As documented under TEMPORARY Table Problems:
You cannot refer to a TEMPORARY table more than once in the same query. For example, the following does not work:
mysql> SELECT * FROM temp_table, temp_table AS t2;
ERROR 1137: Can't reopen table: 'temp_table'
This error also occurs if you refer to a temporary table multiple times in a stored function under different aliases, even if the references occur in different statements within the function.
As others may wander past this question/solution thread... if they have an older Ubuntu 16.04LTS machine or similar.
The limitation exists in Ubuntu 16.04, mysql 5.7, as documented here, like eggyal reported above. The bug/feature was logged here, and ignored for more than a decade. Similarly, it was also logged against mariadb, and was resolved in version 10.2.1. Since Ubuntu 16.04LTS uses mariadb 10.0, the feature is out of easy reach without upgrading to 18.04 etc. You have to download from external repo and install directly.