I want to create procedure in mysql over phpmyadmin which insert paramters in two tables but phpmyadmin throws error that this is not possible.
So i want to try edit procedures over notepad++. Can you please tell me where are they saved on file system.
I run phpmyadmin on localhost with xampp.
Or if any other option to create procedure with this two insert statement.
INSERT INTO `table1`(`att1`, `att2`, `att3`)
VALUES (p_att1, p_att2, p_att3);
INSERT INTO `table2`(`att1`, `att2`, `att3`, `att4`, `att5`)
VALUES ('admin','admin', p_att4, p_att5, p_att6)
As already mentioned in comments stored procedures are not stored on the filesystem but rather in a special table in a database itself.
If you have appropriate permissions you can see a procedure's definition by issuing the following command
SHOW CREATE PROCEDURE my_proc;
Now your procedure can look like this
DELIMITER $$
CREATE PROCEDURE my_proc
(
IN _att1 VARCHAR(32),
IN _att2 VARCHAR(32),
IN _att3 VARCHAR(32),
IN _att4 VARCHAR(32),
IN _att5 VARCHAR(32)
)
BEGIN
START TRANSACTION;
INSERT INTO table1(att1, att2, att3)
VALUES (_att1, _att2, _att3);
INSERT INTO table2(att1, att2, att3, att4, att5)
VALUES ('admin','admin', _att3, _att4, _att5);
COMMIT;
END$$
DELIMITER ;
Here is SQLFiddle demo
Related
I'm trying to write a stored procedure that inserts into one table (A), then queries another table (B), then finally inserts into table (C) the last insert id, along with the result from table B. I have written a stored procedure named VetIdFromCode to do the selecting from table B, which works fine in isolation. When I run the query in isolation, subbing in value for the IN parameters then it runs fine, but when I try and save it as a stored procedure it tells me invalid SQL near 'SET #LIID...'
Many thanks for any help.
CREATE PROCEDURE `NewClientUser`(
IN `uemail` VARCHAR(60),
IN `uphash` CHAR(40),
IN `uvcode` VARCHAR(11))
DETERMINISTIC
MODIFIES SQL DATA
SQL SECURITY INVOKER
INSERT INTO users (user_id,user_email,user_hash,user_role)
VALUES (NULL,uemail,uphash,'1');
SET #LIID = LAST_INSERT_ID();
CALL `VetIdFromCode`(uvcode, #VID);
INSERT INTO user_vet_lookup(user_id,vet_id)
VALUES (#LIID,#VID);
You need to start the "code" of the procedure with the key word "BEGIN" and put an "END" at the end. Like:
CREATE PROCEDURE `NewClientUser`(
IN `uemail` VARCHAR(60),
IN `uphash` CHAR(40),
IN `uvcode` VARCHAR(11))
BEGIN
DETERMINISTIC
MODIFIES SQL DATA
SQL SECURITY INVOKER
INSERT INTO users (user_id,user_email,user_hash,user_role)
VALUES (NULL,uemail,uphash,'1');
SET #LIID = LAST_INSERT_ID();
CALL `VetIdFromCode`(uvcode, #VID);
INSERT INTO user_vet_lookup(user_id,vet_id)
VALUES (#LIID,#VID);
END
Check out the documentation: http://dev.mysql.com/doc/refman/5.7/en/create-procedure.html
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 ;
I created stored procedure to insert data and I want to get the id of inserted record in same procedure
DELIMITER $$
USE `dbname`$$
DROP PROCEDURE IF EXISTS `sp1`$$
CREATE DEFINER=`root`#`%` PROCEDURE `sp1`(
Cname VARCHAR(30),
Camount INT(10)
)
BEGIN
INSERT INTO user( Username, PAmount )
VALUES( Cname, Camount) ;
END$$
DELIMITER ;
I tried
SELECT MAX(id) AS pid FROM user;
but this cause error in next query
error :
Commands out of sync; you can't run this command now
If you get Commands out of sync; you can't run this command now in your client code, you are calling client functions in the wrong order.
From here: http://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html
If the id is declared as a primary key , make use of last_insert_id() function which will resolve the issue. For more details refer to last_insert_id() in mysql documentation
https://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id
I'm attempting to create a procedure that is a basic insert into a table, and then performs a quick update on another table afterwards in MySQL. Please find the code below:
DROP PROCEDURE IF EXISTS `sp_insertUserSocial`
GO
CREATE PROCEDURE sp_insertUserSocial
(
IN p_userSocialID INT(11),
IN p_socialID INT(11),
IN p_userID INT(11),
IN p_referralID INT(11)
)
BEGIN
INSERT INTO UserSocial
(
userSocialID,
socialID,
userID,
referralID
)
VALUES
(
IN p_userSocialID,
IN p_socialID,
IN p_userID,
IN p_referralID
) ;
UPDATE Users
SET connCount = connCount + 1
WHERE UserID = p_referralID;
END
GO
In PHPAdmin it's giving me a syntax error, but I'm not sure where exactly it is? It says line 23, which makes me think it's the semi-colon but I thought that these were needed after an insert statement?
Any help is appreciated, thanks!
I see a couple of problems:
GO, must specify the DELIMITER. 19.1. Defining Stored Programs.
In the INSERT (13.2.5. INSERT Syntax), the IN is optional to pass parameters to the stored procedure (13.1.15. CREATE PROCEDURE and CREATE FUNCTION Syntax), but not part of the syntax of the INSERT.
SQL Fiddle demo
Is it possible to apply trigger for cross database access in MySQL If so please give one example. My purpose is to insert/update/delete data in database2 if there is any new data inserted/updated/deleted in database1. I am using MySQL 5.1
I know it is an old topic but I just had to implement a cross-database trigger myself and I would like to show the solution here as other readers might benefit from it.
Inside the code of the trigger it is possible to refer to the target database with its full name database_name.table_name.
Suppose you have two databases on the same server: database1 and database2. In each of the two databases you have the following table:
CREATE TABLE 'test' (
'id' int(10) DEFAULT NULL,
'name' varchar(100) DEFAULT NULL
)
In the database1 you create the following INSERT trigger:
USE database1;
DELIMITER //
CREATE TRIGGER sync_insert AFTER INSERT ON test
FOR EACH ROW
BEGIN
INSERT INTO database2.test SET id = NEW.id, name=NEW.name;
END; //
DELIMITER ;
Define similar triggers for UPDATE and DELETE.
I'd say this solution is more simple and straightforward than using procedures.
I tested the above on MySQL 5.1.73 and MariaDB 10.2.13.
Yes, you can. You could make a procedure and call it in your trigger. Procedure example :
DELIMITER //
CREATE PROCEDURE delete(in table VARCHAR(300), in db VARCHAR(300), in id INT)
BEGIN
set #query0 = CONCAT('DELETE FROM ', new_db, '.', tabela, ' WHERE id=',id);
PREPARE select_query0 FROM #query0;
EXECUTE select_query0;
DEALLOCATE PREPARE select_query0;
END; //
DELIMITER ;
And then to create the trigger:
CREATE TRIGGER del_trigger BEFORE DELETE ON table
FOR EACH ROW BEGIN
CALL delete(db, table, OLD.id);
END;