how to write multiple Insert queries in single procedure? - mysql

how to write multiple Insert queries in single procedure?
CREATE DEFINER=`root`#`localhost` PROCEDURE `Proc_Insert`(IN `newt` VARCHAR(500), IN `news` TEXT, IN `status` VARCHAR(500), IN `ntype` VARCHAR(500), IN `img_file` VARCHAR(1500), IN `vlink` VARCHAR(500))
INSERT INTO tbl_news(newt, news, status, ntype) VALUES (newt,news,status,ntype);
SET LID = LAST_INSERT_ID();
INSERT INTO tbl_img(pic, cid, imgfile, imgtype, imgstatus) VALUES (LID,LID,img_file,ntype,status);
INSERT INTO tbl_video(cid, vlink, vdis, vstatus) VALUES (LID,v_link,news,status);
This is the first time i am using this stored procedure.

You must enclose the statements with begin and end, and add all statements that you want:
DELIMITER $$
CREATE PROCEDURE `Proc_Insert`(IN `newt` VARCHAR(500), IN `news` TEXT, IN `status` VARCHAR(500), IN `ntype` VARCHAR(500), IN `img_file` VARCHAR(1500), IN `vlink` VARCHAR(500))
BEGIN
DECLARE LID INT;
INSERT INTO tbl_news(newt, news, status, ntype) VALUES (newt,news,status,ntype);
SET LID = LAST_INSERT_ID();
INSERT INTO tbl_img(pic, cid, imgfile, imgtype, imgstatus) VALUES (LID,LID,img_file,ntype,status);
INSERT INTO tbl_video(cid, vlink, vdis, vstatus) VALUES (LID,v_link,news,status);
END$$

DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `paymentStoreProcedure`(IN `inEmail` VARCHAR(100))
BEGIN
DECLARE x, y, lastID INT DEFAULT 0;
INSERT INTO `payments`(`user_name`, `email`, `billing_address`, `ch_contact`, `city`, `ch_payment_method`, `ch_card_title`, `ch_card_number`, `ch_card_expiration`, `ch_card_cvv`, `created_at`) VALUES ('Nabeel',inEmail,'nazimabad','03211234127','Karachi','bank','Nabeel Ahmed','4242424242424242','02 / 24','123', now());
SELECT MAX(id) INTO lastID FROM `payments`;
SELECT id INTO x FROM `users` WHERE email = inEmail;
INSERT INTO `payment_products`(`payment_id`, `cart_id`, `product_name`, `price`, `quantity`, `product_id`, `created_at`) SELECT lastID, x, `product_name`, `price`, `quantity`, `product_id`, now() FROM `cart_infos` WHERE cart_id = x AND is_sold = 0;
UPDATE `cart_infos` SET is_sold =1 WHERE cart_id=x;
SELECT "Payment received";
END$$
DELIMITER ;
Note: I have used this code in my website so I copied it so you can figure it out and it's executable.

Related

how can I form a stored procedure in mysql, which can first check multiple values on multiple tables and then insert into db if true

The actual problem statement looks like :
• Product(prod_id, prod_name, qty_on_hand)
• Order(cust_id, prod_id, order_date, qty_ordered)
• Customer(cust_id, cust_name, phone, address)
"Write a stored procedure to take the cust_id, prod_id and qty_ordered as
input. Procedure should check if the order for a particular customer can be
fulfilled and if yes then insert the new order and update the product
quantity on hand. Display appropriate message if the order cannot be
fulfilled.
Output parameter must have updated value of the qty_on_hand"
I am new to plsql so ignore silly mistakes. I tried to code something like this :
delimiter $$
-- creating procedure
CREATE PROCEDURE order_request( cust_id int, prod_id int, qty_ordered int)
BEGIN
IF( customer.cust_id != cust_id AND product.prod_id != prod_id AND qty_ordered > product.qty_on_hand) THEN
SELECT 'invalid details' ;
ELSE INSERT INTO `orders` (`cust_id`, `prod_id`, `order_date`, `qty_ordered`) VALUES (cust_id, prod_id, current_date(), qty_ordered) ;
END IF ;
END $$
CALL order_request(3,3,2)$$
which showing me error : unknown table customer in field list
I have solved the question. Here is the solution:
CREATE DATABASE shopping;
USE shopping;
CREATE TABLE product( prod_id INT PRIMARY KEY, prod_name varchar(20), qty_on_hand INT CHECK(qty_on_hand >= 0));
CREATE TABLE customer( cust_id INT PRIMARY KEY, cust_name varchar(20), phone INT, address varchar(20) );
-- product table insertion
INSERT INTO `product` (`prod_id`, `prod_name`, `qty_on_hand`) VALUES ('1', 'mug', '2');
INSERT INTO `product` (`prod_id`, `prod_name`, `qty_on_hand`) VALUES ('2', 'bowl', '15');
INSERT INTO `product` (`prod_id`, `prod_name`, `qty_on_hand`) VALUES ('3', 'plate', '7');
INSERT INTO `product` (`prod_id`, `prod_name`, `qty_on_hand`) VALUES ('4', 'fork', '40');
INSERT INTO `product` (`prod_id`, `prod_name`, `qty_on_hand`) VALUES ('5', 'spoon', '30');
-- customer table insertion
INSERT INTO `customer` (`cust_id`, `cust_name`, `phone`, `address`) VALUES ('1', 'duke', '1212121212', 'pune');
INSERT INTO `customer` (`cust_id`, `cust_name`, `phone`, `address`) VALUES ('2', 'finn', '190120138', 'waterland');
INSERT INTO `customer` (`cust_id`, `cust_name`, `phone`, `address`) VALUES ('3', 'buck', '98989898', 'delhi');
INSERT INTO `customer` (`cust_id`, `cust_name`, `phone`, `address`) VALUES ('4', 'larry', '738197232', 'jaipur');
INSERT INTO `customer` (`cust_id`, `cust_name`, `phone`, `address`) VALUES ('5', 'edna', '184194791', 'mumbai');
CREATE TABLE orders( cust_id INT, prod_id INT, order_date DATE, qty_ordered INT CHECK(qty_ordered > 0) , FOREIGN KEY (cust_id) REFERENCES customer(cust_id), FOREIGN KEY (prod_id) REFERENCES product(prod_id));
-- orders table insertion
INSERT INTO `orders` (`cust_id`, `prod_id`, `order_date`, `qty_ordered`) VALUES ('1', '2', '2022-09-15', '2');
delimiter $$
-- creating procedure
CREATE PROCEDURE order_request( cust_id_param INT, prod_id_param INT, qty_ordered_param INT)
BEGIN
IF EXISTS (SELECT cust_id,prod_id,qty_on_hand FROM customer,product WHERE cust_id = cust_id_param AND prod_id = prod_id_param AND qty_on_hand >= qty_ordered_param)
THEN INSERT INTO `orders` (`cust_id`, `prod_id`, `order_date`, `qty_ordered`) VALUES (cust_id_param, prod_id_param, current_date(), qty_ordered_param);
UPDATE `product` SET product.qty_on_hand = qty_on_hand - qty_ordered_param WHERE prod_id = prod_id_param ;
ELSE SELECT 'invalid details' ;
END IF ;
END $$
CALL order_request(1,1,2)$$ -- valid
CALL order_request(3,2,14)$$ -- valid
CALL order_request(5,4,4)$$ -- valid
CALL order_request(10,10,2)$$ -- invalid
select * from orders$$
select * from product$$
-- creating the funtion
CREATE FUNCTION order_details (cust_id_param INT, prod_id_param INT ) RETURNS INT
BEGIN
DECLARE ans int;
SELECT SUM(qty_ordered) INTO ans FROM orders WHERE cust_id = cust_id_param AND prod_id = prod_id_param;
RETURN ans;
END$$
-- calling the funtion
SELECT order_details(5,4)$$

MySQL/MariaDB trigger error

MariaDB version 10.1.16.
I can't find what is the problem in the below trigger:
DROP TRIGGER IF EXISTS `insert_customer`;
DELIMITER |
CREATE TRIGGER `insert_customer` AFTER INSERT ON `installations`
FOR EACH ROW BEGIN
DECLARE customerId INT DEFAULT NULL;
DECLARE versionCode INT DEFAULT NULL;
SELECT c.`id`, c.`version_code` INTO #customerId, #versionCode FROM `customers` c WHERE c.`phone` = NEW.`phone`;
IF #customerId IS NULL THEN
INSERT INTO `customers` (`phone`, `imei`, `platform`, `version_code`, `created`, `modified`) VALUES (NEW.`phone`, NEW.`imei`, NEW.`platform`, NEW.`version_code`, NOW(), NOW());
ELSEIF NEW.`version_code` > #versionCode THEN
UPDATE `customers` SET `version_code` = NEW.`version_code` WHERE `id` = #customerId;
END IF;
END;|
DELIMITER ;
It says 1064 - syntax error near THEN INSERT but I'm sure the problem is elsewhere. Please help.

Trigger MySQL not working

CREATE DEFINER=`root`#`%` TRIGGER `trg_add_role_type_to_auth_item` AFTER INSERT ON role_type
FOR EACH ROW
BEGIN
INSERT INTO `auth_item` (`name`, `type`, `description`, `bizrule`, `data`)
SELECT role_type.name, "2", role_type.name, "NULL", "NULL" FROM role_type WHERE role_type.role_type_id =
(SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='role_type');
END;
what is wrong in this trigger query....
my row got inserted in role_type but do not create in sert in auth_item table.. is it proper way .... I want to insert value from the last row to the auth_item table on insert in role_type table
In trigger you can directly access the fields of main table using new or old objects.
Try this:
CREATE DEFINER=`root`#`%` TRIGGER `trg_add_role_type_to_auth_item` AFTER INSERT ON role_type
FOR EACH ROW
BEGIN
INSERT INTO `auth_item` (`name`, `type`, `description`, `bizrule`, `data`)
VALUES (new.name, "2", new.name, NULL, NULL);
END;

select inside insert statmen and procedure

can i want to create store procedure that get arguments and run an select statment inside the insert statment?
i got 2 question:
1)how to get arguments on store procedure?
2)can i run select statment in insert statment like this?
DELIMITER $$
CREATE PROCEDURE `insertGuide` #m_name varchar(45) ,#m_last varchar(45) ,#addres varchar(45) ,#mphone int
BEGIN
INSERT INTO `guides` (`id`, `name`, `lastName`, `address`, `phone`)
VALUES (select max(id)+1 from seq,#m_name,#m_last,#addres,#mphone);
END
I don't understand the first question, but regarding the INSERT statement, this should work
INSERT INTO `guides` (`id`, `name`, `lastName`, `address`, `phone`)
select max(id)+1, #m_name, #m_last, #addres, #mphone
from seq;
try like this:
DELIMITER $$
DROP PROCEDURE IF EXISTS insertGuide;
CREATE PROCEDURE insertGuide (id int, name varchar(45) ,last varchar(45) ,addres varchar(45) ,phone int(12))
BEGIN
INSERT INTO `guides` (`id`, `name`, `lastName`, `address`, `phone`)
VALUES (id, name, last, addres, phone);
END
you can run select query in insert like:
INSERT INTO `guides` (`id`, `name`, `lastName`, `address`, `phone`)
SELECT MAX(id)+1, #m_name, #m_last, #addres, #mphone
FROM seq;

MySQL: What's wrong with this CREATE PROCEDURE statement?

DROP PROCEDURE IF EXISTS CreateTopic;
CREATE PROCEDURE CreateTopic
(
i_forum_id INT,
i_user_id INT,
i_title VARCHAR(255),
i_language VARCHAR(50),
i_content TEXT,
i_stickied TINYINT,
i_announce TINYINT,
i_closed TINYINT
)
BEGIN
INSERT INTO forum_topics (forum_id, user_id, title, language)
VALUES (i_forum_id, i_user_id, i_title, i_language);
SET #tid := LAST_INSERT_ID();
INSERT INTO forum_posts (topic_id, user_id, subject, content) VALUES (#tid, i_user_id, i_title, i_content);
INSERT INTO core_logs (obj_id, user_id, type, action) VALUES (#tid, i_user_id, 'Topics', 'Topic Created');
END;
I'm not sure what's wrong with it. MySQL tells me all sorts of things are incorrect, it just doesn't want to be created. Also, the parameters are identical types and lengths to their respective tables.
It is your first ";" that breaks procedure definition and mysql thinks you are done and treats whatever goes after ";" as another query.
You have to use delimiter for stored procedures.
DELIMITER $$
DROP PROCEDURE IF EXISTS CreateTopic$$
CREATE PROCEDURE CreateTopic
(
i_forum_id INT,
i_user_id INT,
i_title VARCHAR(255),
i_language VARCHAR(50),
i_content TEXT,
i_stickied TINYINT,
i_announce TINYINT,
i_closed TINYINT
)
BEGIN
INSERT INTO forum_topics (forum_id, user_id, title, language)
VALUES (i_forum_id, i_user_id, i_title, i_language);
SET #tid := LAST_INSERT_ID();
INSERT INTO forum_posts (topic_id, user_id, subject, content) VALUES (#tid, i_user_id, i_title, i_content);
INSERT INTO core_logs (obj_id, user_id, type, action) VALUES (#tid, i_user_id, 'Topics', 'Topic Created');
END
$$
DELIMITER ;
There are a few problems. I hope I got them all:
DROP PROCEDURE IF EXISTS CreateTopic;
CREATE PROCEDURE CreateTopic
(
i_forum_id INT,
i_user_id INT,
i_title VARCHAR(255),
i_language VARCHAR(50),
i_content TEXT,
i_stickied TINYINT,
i_announce TINYINT,
i_closed TINYINT
)
BEGIN
DECLARE tid INT;
INSERT INTO forum_topics (`forum_id`, `user_id`, `title`, `language`)
VALUES (i_forum_id, i_user_id, i_title, i_language);
SET tid = LAST_INSERT_ID();
INSERT INTO forum_posts (`topic_id`, `user_id`, `subject`, `content`) VALUES (tid, i_user_id, i_title, i_content);
INSERT INTO core_logs (`obj_id`, `user_id`, `type`, `action`) VALUES (tid, i_user_id, 'Topics', 'Topic Created');
END;