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.
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 have the following stored procedure in phpMyAdmin:
I need to save the result into the following existing table:
but I don't know how to code this. The code should clear/update(?) the table every-time the stored procedure is executed and populate the last field "profile_completion_percentage" with the sum of the previous fields (excluded the id). Can you please help?
use
INSERT ... SELECT Statement
https://dev.mysql.com/doc/refman/8.0/en/insert-select.html
So you can add your result very simple to your table
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.
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
Problem is that, I want a trigger that deletes old rows in the same table that the new rows are being inserted into.
MsSQL and oracle can do this,
but looks like mySQL can't,
It allows the trigger to be created, but when it runs it gives the error
"can't update table "tbl" in stored procedure or function/trigger
because it is already used by statemtent whicgh invoked this stored
procedure or function/trigger"
Any work around for this?
Is it planned in future releases?
I have in my database all tables with filed name GHOST, so when GHOST is TRUE this row is like DELETED. So if You want change row after insert maybe use like this:
CREATE TRIGGER my_trigg
BEFORE INSERT ON table
FOR EACH ROW
BEGIN
SET NEW.ghost = TRUE;
END
$$
DELIMITER ;