PHPmyadmin Stored Procedures Syntax Error if else - mysql

Stored Procedures syntax error at line 11 'BEGIN INSERT INTO customer(CUSTOMER_ID,NAME)'.............................................................................................................................................................................................................
BEGIN
IF EXISTS
(
SELECT
*
FROM
country
WHERE
country_code = #country_code
)
BEGIN
INSERT INTO customer(
CUSTOMER_ID,
NAME,
EMAIL,
COUNTRY_CODE,
BUDGET,
USED
)
VALUES(
#CUSTOMER_ID,
#NAME,
#EMAIL,
#COUNTRY_CODE,
#BUDGET,
#USED
)
END ELSE
BEGIN
INSERT INTO customer(
CUSTOMER_ID,
NAME,
EMAIL,
COUNTRY_CODE,
BUDGET,
USED
)
VALUES(
#CUSTOMER_ID,
#NAME,
#EMAIL,
#COUNTRY_CODE,
#BUDGET,
#USED
)
END
END

DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `AddCustomer`(IN `CUSTOMER_ID` VARCHAR(4), IN `NAME` VARCHAR(50), IN `EMAIL` VARCHAR(50), IN `COUNTRY_CODE` VARCHAR(2), IN `COUNTRY_NAME` VARCHAR(50), IN `BUDGET` DECIMAL(18,2))
BEGIN
IF EXISTS
(
SELECT
*
FROM
country
WHERE
country_code = country_code
) THEN
INSERT INTO customer(
CUSTOMER_ID,
NAME,
EMAIL,
COUNTRY_CODE,
BUDGET,
USED
)
VALUES(
CUSTOMER_ID,
NAME,
EMAIL,
COUNTRY_CODE,
BUDGET,
USED
) ; ELSE
INSERT INTO country(COUNTRY_CODE, COUNTRY_NAME)
VALUES(COUNTRY_CODE, COUNTRY_NAME) ;
INSERT INTO customer(
CUSTOMER_ID,
NAME,
EMAIL,
COUNTRY_CODE,
BUDGET,
USED
)
VALUES(
CUSTOMER_ID,
NAME,
EMAIL,
COUNTRY_CODE,
BUDGET,
USED
) ;
END IF ;
END$$
DELIMITER ;

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)$$

How can I avoid duplicate values in my result set?

CREATE TABLE student
(
s_id int(10) NOT NULL AUTO_INCREMENT,
s_roll_no int(30),
s_name varchar(30),
s_gender varchar(4) not null,
class int(2) not null,
PRIMARY KEY (s_id)
);
CREATE TABLE attendance_date
(
date_today varchar(10),
PRIMARY KEY (date_today)
);
CREATE TABLE attendance_today
(
s_id int(10),
s_roll_no int(30),
s_name varchar(30),
s_gender varchar(4),
class int(2),
date_today varchar(10),
attendance_status varchar(2) not null default 'P'
);
delimiter $$
create trigger after_insertion_into_attendance_date
after insert on attendance_date
for each row
begin
insert into attendance_today(s_id, s_roll_no, s_name, s_gender, class, date_today)
select * from student cross join attendance_date order by date_today, s_id;
end$$
delimiter ;
INSERT INTO student
VALUES
(1,1,'Mridul Kumar','M',1),
(2,2,'Harish Paul','M',1),
(3,3,'Imtiaz Hossain','M',1);
INSERT INTO attendance_date
VALUES
('1st Jan'),
('2nd Jan');
now,
select * from attendance_today;
giving duplicates after every insertion, is there any way to avoid such duplicates inside trigger?
I'm not looking for
select distinct * from attendance_today;
after the trigger gets activated.
This will prevent duplicate values from being inserted
insert into attendance_today(s_id, s_roll_no, s_name, s_gender, class, date_today)
select *
from student cross join attendance_date ad
WHERE NOT EXISTS (
SELECT null FROM attendance_today at
WHERE ad.s_id = at.s_id and ad.date_today = at.date_today
)
order by date_today, s_id;
Try this:
begin
insert into attendance_today(s_id, s_roll_no, s_name, s_gender, class, date_today)
select * from student cross join attendance_date group by s_id order by date_today, s_id;
end$$

Mysql stored procedure insert into multiple tables

Am trying to insert data into 2 tables but only 1 table gets the data but the other one doesn't. I don't get any error messages but only the login table gets populated but the user doesn't.
Here is my code
CREATE DEFINER=`root`#`localhost` PROCEDURE `new_user`(
_email varchar(50),
_password varchar(255),
_name varchar(35),
_surname varchar(35)
)
BEGIN
start transaction;
insert into logins_copy (
email,
password,
created_at,
updated_at
)
values
(
_email,
_password,
current_timestamp(),
current_timestamp()
);
SELECT #user_id := id from logins_copy where email = _email;
insert into users (
user_id,
name,
surname,
updated_at
)
values
(
#user_id,
_name,
_surname,
current_timestamp()
);
commit;
END
and to test this I called the procedure:
call `new_user`("test#hotmail.com", "test123", "Adam", "James");

Error 1064 in a procedure

ERROR 1064 (42000): 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 '= (se
lect ifnull(max(manufactureId+1),1) from tbl_Manufacturer
) ;
create procedure ManufactureAdd(
p_manufactureName longtext,
p_address longtext,
p_phone varchar(50),
p_email varchar(50),
p_description longtext
)
begin
declare p_manufactureId = (select ifnull(max(manufactureId+1),1) from tbl_Manufacturer
) ;
insert into tbl_Manufacturer(
manufactureId,
manufactureName,
address,
phone,
email,
description
)
VALUES
(
p_manufactureId,
p_manufactureName,
p_address,
p_phone,
p_email,
p_description
) ;
SELECT p_manufactureId ;
end
Try to use SELECT..INTO clause and simple user variable -
CREATE PROCEDURE ManufactureAdd(
p_manufactureName longtext,
p_address longtext,
p_phone varchar(50),
p_email varchar(50),
p_description longtext)
BEGIN
SELECT IFNULL(MAX(manufactureId + 1), 1) INTO #p_manufactureId FROM tbl_Manufacturer;
INSERT INTO tbl_Manufacturer (manufactureId, manufactureName, address, phone, email, DESCRIPTION)
VALUES (#p_manufactureId, p_manufactureName, p_address, p_phone, p_email, p_description);
SELECT #p_manufactureId;
END
Try this:
CREATE PROCEDURE ManufactureAdd(
p_manufactureName LONGTEXT,
p_address LONGTEXT,
p_phone VARCHAR(50),
p_email VARCHAR(50),
p_description LONGTEXT
)
BEGIN
DECLARE p_manufactureId INT;
SELECT IFNULL(MAX(manufactureId+1),1) INTO p_manufactureId FROM tbl_Manufacturer;
INSERT INTO tbl_Manufacturer(
manufactureId, manufactureName, address, phone, email, description
)
VALUES (
p_manufactureId, p_manufactureName, p_address, p_phone, p_email, p_description
);
SELECT p_manufactureId ;
END

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;