Here is how you can create a stored procedure in mysql. It’s very simple and easy to create. In the below syntax we have used IF statements as well as inserts or updates based on the IF STATEMENTS.
DELIMITER //
CREATE PROCEDURE `AddNewAccount`(
IN `AccNo` VARCHAR(50) CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci,
IN `EA1` TEXT CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci,
IN `EA2` TEXT CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci,
IN `AccountUuid` CHAR(50) CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci
)
BEGIN
SET @AccExist := (SELECT COUNT(*) AS account_exist from employees WHERE acc_number=AccNo);
SET @AccExist = IF (@AccExist < 1, 'NOT EXIST', 'EXIST');
SELECT AccNo;
IF @AccExist = 'NOT EXIST' THEN
UPDATE s_db.not_exist s_db SET s_db.sp_remarks = 'SP Start' WHERE s_db.ACC_N_C = AccNo;
IF EA1 != '' THEN
SET @EmailExist1 := (SELECT COUNT(*) FROM DB1.emails WHERE email_address=EA1);
SET @EmailExist1 = IF (@EmailExist1 < 1, 'EMAIL NOT EXIST', 'EMAIL EXIST');
SELECT @EmailExist1;
IF @EmailExist1 = 'EMAIL NOT EXIST' THEN
SET @EmailUUID1=UUID();
SELECT @EmailUUID1;
INSERT INTO DB1.emails (id,email_address,email_address_caps,invalid_email,opt_out,date_created,date_modified,deleted)
VALUES (@EmailUUID1,EA1,UPPER(EA1),'0','0',NOW(),NOW(),'0');
END IF;
SET @EmailUUID1 := (SELECT id FROM DB1.emails WHERE email_address=EA1 LIMIT 1);
INSERT INTO DB1.table_1 (id,date_created,email_address_id,bean_id,bean_module,primary_address,deleted)
VALUES (UUID(),NOW(),@EmailUUID1,AccountUuid,'Accounts','1','0');
END IF;
IF EA2 != '' THEN
SET @EmailExist2 := (SELECT COUNT(*) FROM DB1.emails WHERE email_address=EA2 );
SET @EmailExist2 = IF (@EmailExist2 < 1, 'EMAIL NOT EXIST', 'EMAIL EXIST');
SELECT @EmailExist2;
IF @EmailExist2 = 'EMAIL NOT EXIST' THEN
SET @EmailUUID2=UUID();
SELECT @EmailUUID2;
INSERT INTO DB1.emails (id,email_address,email_address_caps,invalid_email,opt_out,date_created,date_modified,deleted)
VALUES (@EmailUUID2,EA2,UPPER(EA2),'0','0',NOW(),NOW(),'0');
END IF;
SET @EmailUUID2 := (SELECT id FROM DB1.emails WHERE email_address=EA2 LIMIT 1);
INSERT INTO DB1.table_1 (id,date_created,email_address_id,bean_id,bean_module,primary_address,deleted)
VALUES (UUID(),NOW(),@EmailUUID2,AccountUuid,'Accounts','1','0');
END IF;
## add account in accounts table
INSERT INTO DB1.accounts (id,NAME)
SELECT AccountUuid,NAME FROM s_db.not_exist WHERE ACC_N_C = AccNo;
# add account in acc_sys table
INSERT INTO DB1.acc_sys (id_c,ACC_N_C)
SELECT AccountUuid,ACC_N_C FROM s_db.not_exist WHERE ACC_N_C = AccNo;
UPDATE s_db.not_exist s_db SET s_db.sp_remarks = 'Account Added Successfully' WHERE s_db.ACC_N_C = AccNo;
ELSE
UPDATE s_db.not_exist s_db SET s_db.sp_remarks = 'Account Already Exist' WHERE s_db.ACC_N_C = AccNo;
SET @AccAlreadyExist = 'Account Already Exist';
SELECT @AccAlreadyExist;
END IF;
END//
DELIMITER ;