I need to use table output of stored procedure outside of it.
Stored procedure produces pivot table which means:
I don't know exact structure of output table - cannot use INSERT INTO for permanent / temporary table (they expect exact table structure).
Complex SQL statements inside (not only SELECT statements) - cannot export SQL code outside of stored procedure in some SELECT.
How to use SP table output meaning these conditions?
The problem was in non-fixed structure of SP output.
So, CREATE TABLE flds / INSERT INTO not working when table structure not exactly defined before.
But exists very flexible syntax for CREATE [TEMPORARY] TABLE AS which is not require exactly defined table structure. It helped me.
Hope this will help to MYSQL beginners.
Related
In SQL Server, I used to create a table variable to store results from a certain stored procedures. This is how I usually do with table variable.
DECLARE #My_Table_Variable TABLE(col_1 FLOAT, col_2 FLOAT)
INSERT INTO #My_Table_Variable(col_1, col_2) EXEC [My_Procedure]'param_1','param_2'
Now, while using MySQL, I recognized that table variable doesn't exist. I've seen some questions related to this, for instance, this one says it's not possible to SELECT something FROM a procedure.
How about temporary table? Can we CALL a procedure, and then put the result into a temporary table? With the syntax something like this:
CALL my_procedure('my_first_parameter','my_second_parameter') INTO my_temporary_table;
which allows me to query from my_temporary_table. Is this possible to be performed?
I need to create two temporary tables inside a stored procedure, and wherever I put the CREATE statements MySQL gives me that nonsense syntax error missing 'end'.
This useful page says nothing about where to put the CREATE statements, though I have everything else is in the desired order.
I also would like to note that only the semicolon after second CREATE statement is recognized as a syntax error, if I swap them, the error is still in the latter statement.
Here's my code, but I guess only first 20 lines matter.
The statements I was (unsuccessfully) trying to add:
-- temporary tables
CREATE TEMPORARY TABLE filtered
LIKE seances;
CREATE TEMPORARY TABLE probs (
solution_id INT,
priori DOUBLE);
i'm new to MySQL.
I need to call stored procedure from a stored procedure and use the first stored procedure as a table.
How to do this without use temporary table?
How to do this without use temporary table?
create a fact table then
Not sure why there is a requirement saying: can't use temporary table as you are using store procedure. but that must be unreasonable.
if the RDBS takes care of that for you , the underlying mechanism is still store the 1st result set somewhere in the memory as middle step result. temporary table for you.
so just: create a temporary table, call the store procedure, insert data into that . why not
I have a stored procedure that writes results to a Result table. How do you truncate / erase a table from within a stored procedure?
example
call peformTest()
truncate TestResultTable;
//do stuff with new data to insert into TestResultTable
end
If you want to remove all data from the table, then your syntax is correct:
truncate testResultTable;
or
truncate table testResultTable;
Depending on your specific needs, if you need to get rid of the table correctly and then re-create it, you can do:
drop table testResultTable;
create table testResultTable as select ... from ... where ...
Not sure if there is any difference in SQL vs stored procedure in the way they execute. But usually the format for truncating is: Truncate Table tableName; here is the reference: http://dev.mysql.com/doc/refman/5.0/en/truncate-table.html
In the stored procedure simple:
DELETE FROM table WHERE 1 = 1
Other example in PL/SQL:
PL/SQL SP Truncate
Or solution by #Churk.
so the code I am working on has this statement executed by PHP (note:This is taken from the PostgreSQL log file so it doesn't include any PHP stuff):
CREATE temporary table IF NOT EXIST temp tablename(id int primary key,
shared int default 0) replace select 1, userid as id
from tablefoo where sharedid = 1337
I don't quite understand what's going on here exactly, I know what a temporary table is, and I can quite accurately guestimate what IF NOT EXIST does, but what is replace doing here? I know replace is like insert but it replaces stuff as well, but in this case, nothing is specified for it to replace with, so does it just replace something with nothing and why the Select 1, I know that pretty much just tells you if your table has rows or something, but what is the point of using it here?
After some research, I found that IF NOT EXIST and replace do not exist in PostgreSQL. Most online sources suggest that SQL functions be used to replace them.
Should I use an SQL function to emulate IF NOT EXIST? If so, what would I write (sorry, I am pretty new to SQL) or should I just use a PHP function.
What about replace?
Sorry for the trouble, thanks for your time, oh and if you guys aren't busy or anything, you could also tell me about how to emulate "ignore", my current solution involves arbitrarily removing it.
Many uses in MySQL for temporary tables can be replaced in PostgreSQL with common table expressions or ordinary subselects.
WITH someCTE AS (
SELECT
...
) SELECT/UPDATE/DELETE ... WHERE sometable.column = someCTE.another_column;
Look into CREATE TABLE documentation. Temporary tables are just as name suggests not permanent:
If specified, the table is created as a temporary table. Temporary
tables are automatically dropped at the end of a session, or
optionally at the end of the current transaction (see ON COMMIT
below). Existing permanent tables with the same name are not visible
to the current session while the temporary table exists, unless they
are referenced with schema-qualified names. Any indexes created on a
temporary table are automatically temporary as well.
In particular temp tables are stored in diffrent (non-public) schema, e.g.:
=> Create Temporary Table someTempTable (value integer);
CREATE TABLE
=> \dt someTempTable
List of relations
Schema | Name | Type | Owner
-----------+---------------+-------+----------
pg_temp_2 | sometemptable | table | postgres
(1 row)
PostgreSQL doesn't have IF NOT EXISTS like in MySQL's CREATE TABLE, so you can't use it. If you want to create some table you need to firstly drop existing one (if it exists). Fortunately you could use SQL command DROP TABLE IF EXISTS to handle this:
=> Drop Table If Exists someTempTable;
DROP TABLE
=> Drop Table If Exists someTempTable;
NOTICE: table "sometemptable" does not exist, skipping
DROP TABLE