Just wondering, im new to stored procedures,
I just tried this
INSERT INTO new.emp SELECT * FROM old.users
and it worked and inserted the data of users from the old table users to the new emp table.
But when I did that inside a stored procedure im getting a syntax error
CREATE PROCEDURE insertnew
INSERT INTO new.emp SELECT * FROM old.users
What's the difference?
In what part of the query are you getting a syntax error? The correct syntax for creating a stored procedure is something like this:
DELIMITER //
CREATE PROCEDURE InsertEmployee()
BEGIN
INSERT INTO new.emp SELECT * FROM old.users;
END //
DELIMITER ;
Related
I want to create an trigger inside an procedure. but after some research I got to know that it is not possible. can u suggest me another way I can achieve the below actions. (I cant share exact data and queries due to some reason. please refer similar queries.)
What I want
I have created an temporary table containing data i need.
eg. CREATE TEMPORARY TABLE temp1 SELECT id, col_1 FROM table1 WHERE col_1=2;
I want to inset data in table table2 when data is inserted in temp1, which i can achieve by creating a TRIGGER. but the problem is I want to give a value in table2 which will be dynamic and will be taken from nodejs backend. so i created a PROCEDURE which takes parameter neededId. but i cant created trigger inside a procedure. is their any other way i can achieve this?
Procedure I Created
here neededId is the foreign key I get from backend to insert
DELIMITER $$
USE `DB`$$
CREATE PROCEDURE `MyProcedure` (IN neededID int)
BEGIN
DROP TABLE IF EXISTS temp1;
CREATE TEMPORARY TABLE temp1 SELECT id, col_1 FROM table1 WHERE col_1=2;
DROP TRIGGER IF EXISTS myTrigger;
CREATE TRIGGER myTrigger AFTER INSERT ON temp1 FOR EACH ROW
BEGIN
INSERT into table2("value1", "value2", neededId);
END;
END$$
DELIMITER ;
SQL Statements Not Permitted in Stored Routines
Generally, statements not permitted in SQL prepared statements are also not permitted in stored programs. ... Exceptions are SIGNAL, RESIGNAL, and GET DIAGNOSTICS, which are not permissible as prepared statements but are permitted in stored programs.
SQL Syntax Permitted in Prepared Statements
CREATE TRIGGER is not listed.
Finally: the trigger cannot be created in stored procedure, function, prepared statement, trigger or event procedure.
In MySQL, you can't create a trigger for a temporary table, regardless of whether you do it in a stored procedure or not.
https://dev.mysql.com/doc/refman/8.0/en/create-trigger.html says:
The trigger becomes associated with the table named tbl_name, which must refer to a permanent table. You cannot associate a trigger with a TEMPORARY table or a view.
I assume the same is true of MariaDB. It's not clear from your question which one you use. You say MySQL at first, but you tagged the question mariadb. Be aware that these are not the same database product, and they are not necessarily compatible.
Here's a demo of the error, tested on MySQL 8.0.28:
mysql> create temporary table t ( i int );
mysql> create trigger t before insert on t for each row set NEW.i = 42;
ERROR 1361 (HY000): Trigger's 't' is view or temporary table
So in your case, you cannot use a trigger for the temporary table. You'll have to think of a different way to implement inserts to your second table.
one Stored Procedure is returning me a table and I want to hold that result into another Stored Procedure.
and I am facing SQL syntax error.
First Stored Procedure
DELIMITER $$
USE `dataBase`$$
DROP PROCEDURE IF EXISTS `testReturnTable`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `testReturnTable`()
BEGIN
SELECT `user_id`, `email` FROM `users`;
END$$
DELIMITER ;
and I am trying to call this Store Procedure into another and want to hold the data into a view or table
DELIMITER $$
USE `dataBase`$$
DROP PROCEDURE IF EXISTS `testReturnCall`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `testReturnCall`()
BEGIN
DROP VIEW IF EXISTS `my_view`;
CREATE OR REPLACE VIEW my_view AS (CALL testReturnTable());
SELECT * FROM my_view;
END$$
DELIMITER ;
and getting error
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 'CALL testReturnTable());
I can use function here as well, but I don't know how to handle result set,
please advice.
it is not possible to get table ouput from one stored procedure to another stored procedure. But we have alternate to do this which is Temp table
I am trying to create a simple stored procedure in phpmyadmin on namecheap.com server. I have created this procedure on my localhost and there was no problem however I'm unable to understand why is it not being created now.
I have tried by using mySQL as well as from Add Routine option.
My table name is users and here is the stored procedure
CREATE PROCEDURE getUsers (myUsername varchar (32))
BEGIN
SELECT * FROM `users` WHERE username = myUsername;
END$$
However I have another table called albums and I've written similar procedure for that table and it is working fine. Please help
Stucks on Loading Screen
You need the DELIMITER and IN for the params:
DELIMITER //
CREATE PROCEDURE getUsers (IN myUsername varchar (32))
BEGIN
SELECT * FROM `users` WHERE username = myUsername;
END //
DELIMITER ;
Two questions.
I need to create a procedure called SITE_SP, to count the number of records in the 'SHOWSITE' table.
Then I need to run the site_sp procedure to display the number of records in the 'SHOWSITE' table. My guess here is with the EXEC site_sp.
So far, all I have is (I know it isn't correct but that's all I can do, i'm really stuck).
DROP PROCEDURE IF EXISTS site_sp;
CREATE PROCEDURE site_sp
BEGIN
Select * FROM showsite
END;
Any help appreciated. I've asked before but can't seem to get any positive feedback, so I skipped this question and did the others, but this one I just can't solve.
One solution should be to create a temporary table with your result in it:
DROP PROCEDURE IF EXISTS site_sp;
CREATE PROCEDURE site_sp BEGIN DROP TABLE IF EXISTS tmp_result;
CREATE TABLE tmp_result SELECT COUNT(*) FROM showsite END//
delimiter ;
I want to store the output from a PROCEDURE into a global userdefined VAR so i can use this "list" in an other PROCEDURE on a different database.
Second, if the VAR is used on the 2nd PROCEDURE it should be unset, because on next CALL it will append or?
Thanks for response!
BEGIN
SELECT `steamid` FROM `mybb_users` WHERE `steamid`!='';
END
The SELECT shout go into a global variable, so i can use the result in another procedure...
As far as I know, you can't return a row set as a result of a procedure in MySQL.
I would solve it by creating a temporary table in the first procedure, and then use that temp table in the second procedure. Something like this:
delimiter $$
create procedure procedure1()
begin
drop table if exists temp_table;
create temporary table temp_table
select steamid from mybb_users where steamid != '';
-- add the appropriate indexes here
-- alter table temp_table
-- add index ...
end $$
create procedure procedure2()
begin
-- Do whatever you want to do with temp_table
end $$
delimiter ;
Remember:
A temporary table is visible only to the connection that created it.
If the connection is closed, the temporary table will be deleted.
Temporary tables are created directly to RAM (if they are not very big), so they can be pretty fast to read.
Each connection can create temporary tables with the same name, as each connection will have a "copy" of that temp table.