I have tried this procedure to insert data:
CREATE PROCEDURE myproc()
BEGIN
DECLARE i int DEFAULT 30;
DECLARE acc = 'access';
WHILE i <= 80 DO
acc = acc + i + '#gmail.com';
INSERT INTO users (email, status, password) VALUES (acc, 1, '$2y$12$/Jc6ayqwr333Od/0bexYF.aT1h34mzmElG8SFozzrWjhokS6wEUA6');
SET i = i + 1;
END WHILE;
END
But problem is in line: acc = acc + i + '#gmail.com';
How to do that right in MySQL?
Also I have tried this:
CREATE PROCEDURE myproc()
BEGIN
DECLARE i INT;
DECLARE acc VARCHAR;
i = 16;
WHILE i <= 17 DO
acc = 'access' + i + '#gmail.com';
INSERT INTO users (email, status, password) VALUES (acc, 1, '$2y$12$/Jc6ayqwr333Od/0bexYF.aT1h34mzmElG8SFozzrWjhokS6wEUA6');
SET i = i + 1;
END WHILE;
END
You have not declared the type for acc and have not used a set clause to set it in the loop and you don't know how to concat in mysql and the concat you are attempting is logically wrong.
try this
drop procedure if exists p;
delimiter $$
CREATE PROCEDURE p()
BEGIN
DECLARE i int DEFAULT 30;
DECLARE acc varchar(10) default 'access';
declare vemail varchar(30);
WHILE i <= 35 DO
set vemail = concat(acc,i,'#gmail.com');
#INSERT INTO users (email, status, password) VALUES (vemail, 1, '$2y$12$/Jc6ayqwr333Od/0bexYF.aT1h34mzmElG8SFozzrWjhokS6wEUA6');
select vEmail;
SET i = i + 1;
END WHILE;
END $$
delimiter ;
call p()
When you are happy with the vemail format uncomment the insert and remove the select
Related
I need to convert this tSQL query into MySQL query version 5.7.14
I tried declare parameter with the following syntax
declare #commissionType INT;
but I reached a dead end and need help to convert this query
CREATE PROCEDURE MyProc
#chainId int
as
BEGIN
declare #commissionMethod int
declare #commissionType int
declare #value decimal(18,2)
set #commissionMethod = 1
set #value = 200000
set #commissionType = (select CommissionType from CommissionRules
where ChainId = #chainId )
IF(#commissionMethod = 1)
BEGIN
print('alert')
select (Fixed * #value) / 100 from CommissionRules where ChainId = #chainId
END
END
This is pretty much a literal conversion. But check your logic. It doesn't make any sense. v_commissionMethod is always 1 and v_commissionType is not even being used after selecting a value.
DELIMITER //
CREATE PROCEDURE MyProc (
p_chainId int)
BEGIN
declare v_commissionMethod int;
declare v_commissionType int;
declare v_value decimal(18,2);
set v_commissionMethod = 1;
set v_value = 200000;
select CommissionType into v_commissionType from CommissionRules
where ChainId = p_chainId ;
IF(v_commissionMethod = 1)
THEN
/* print('alert') */
select (Fixed * v_value) / 100 from CommissionRules where ChainId = p_chainId;
END IF;
END;
//
DELIMITER ;
Something like this:
DELIMITER $$
CREATE PROCEDURE MyProc (
in_chainId unsigned
) as
BEGIN
select v_CommissionType := CommissionType,
v_commissionMethod = 1,
v_value = 200000
from CommissionRules
where ChainId = in_chainId;
if v_commissionMethod = 1 then
select 'alert';
select (Fixed * v_value) / 100
from CommissionRules
where ChainId = in_chainId
end if;
END;$$
DELIMITER ;
I want to make a Stored Procedure that handles an order from our site. The problem is that when I run the script there is a MySQL syntax error. I'm very new with MySQL Stored Procedures. Can someone look at my code?
USE postbrood;
DELIMITER //
CREATE PROCEDURE MaakBestelling(IN KlantIDParam INT, IN ProductIDArray VARCHAR(255), IN AantalArray VARCHAR(255))
BEGIN
DECLARE BestelID INT DEFAULT 0;
DECLARE ArrayLenght INT DEFAULT 0;
DECLARE Counter INT DEFAULT 0;
SET ArrayLenght = LENGTH(ProductIDArray) - LENGTH(REPLACE(ProductIDArray, ',', '')) + 1;
INSERT INTO bestelling(klantID) VALUES (KlantIDParam);
SET BestelID = LAST_INSERT_ID();
WHILE Counter < ArrayLenght DO
INSERT INTO bestelregel VALUES (SUBSTRING_INDEX(ProductIDArray,',',Counter),BestelID,SUBSTRING_INDEX(AantalArray,',',Counter));
SET Counter = Counter + 1;
END WHILE;
END//
DELIMITER ;
Thanks in advance!
Nailed it! :)
USE postbrood;
DELIMITER //
CREATE PROCEDURE MaakBestelling(IN KlantIDParam INT, IN ProductIDArray VARCHAR(255), IN AantalArray VARCHAR(255))
BEGIN
DECLARE BestelID INT DEFAULT 0;
DECLARE ArrayLenght INT DEFAULT 0;
DECLARE Counter INT DEFAULT 0;
SET ArrayLenght = LENGTH(ProductIDArray) - LENGTH(REPLACE(ProductIDArray, ',', '')) + 1;
INSERT INTO bestelling(klantID) VALUES (KlantIDParam);
SET BestelID = LAST_INSERT_ID();
WHILE Counter < ArrayLenght DO
INSERT INTO bestelregel VALUES (SUBSTRING_INDEX(ProductIDArray,',',Counter),BestelID,SUBSTRING_INDEX(AantalArray,',',Counter),2.50);
SET Counter = Counter + 1;
END WHILE;
END//
DELIMITER ;
I think the comma is not being recognized at
SET ArrayLenght = LENGTH(ProductIDArray) - LENGTH(REPLACE(ProductIDArray, ',', '')) + 1;
You should try replacing the comma with the ascii entity number: ,
I have created mysql function to split the string in rows using temporary table, it gives correct in temp table, but not when i call the function it returns 1 as output.
My code is as below
DELIMITER $$
DROP FUNCTION IF EXISTS `split`$$
CREATE FUNCTION `split`(`String` VARCHAR(8000), `Delimiter` CHAR(1)) RETURNS VARCHAR(8000) CHARSET latin1
BEGIN
DECLARE idx INT;
DECLARE slice VARCHAR(8000);
DECLARE result INT DEFAULT 0;
DROP TEMPORARY TABLE IF EXISTS tmp;
CREATE TEMPORARY TABLE `tmp` (items VARCHAR(8000));
-- set #tmptab = tmp;
SET idx = 1;
label: WHILE LENGTH(`String`)<1 OR `String` IS NULL DO
LEAVE label;
END WHILE;
loop_lable: WHILE (idx<>0) DO
SET idx = LOCATE(`Delimiter`,`String`);
IF (idx<>0) THEN
SET slice = LEFT(`String`,idx - 1);
ELSE
SET slice = `String`;
END IF;
IF(LENGTH(slice)>0) THEN
INSERT INTO tmp(Items) VALUES(slice) ;
SET result = 1;
END IF;
SET `String` = RIGHT(`String`,LENGTH(`String`) - idx);
IF LENGTH(`String`) = 0 THEN
LEAVE loop_lable;
END IF;
END WHILE;
RETURN result;
END$$
DELIMITER ;
I am trying to create a procedure to update my database with multiple parameters. Here is my code:
DELIMITER //
CREATE PROCEDURE updateImages (IN stagingID INT, IN streetName VARCHAR(50), IN numberOfImages INT)
BEGIN
DECLARE count INT;
SET count = 1;
WHILE count < (numberOfImages + 1) DO
SET fileName = CONCAT(streetName, ' (mls) (', count, ').jpg');
INSERT INTO images_tbl VALUES
(NULL, stagingID, fileName, 0);
SET count = count + 1;
END WHILE;
END //
DELIMITER ;
PHPMyAdmin is giving me a blank #1193 error with no other information. I've tried to search and implement the resolutions I have found regarding this error, but have not been able to figure it out.
Any ideas would be very welcome. Thanks in advance.
As #Drew pointed out, I omitted a declaration for fileName. Final Code:
DELIMITER //
CREATE PROCEDURE updateImages (IN stagingID INT, IN streetName VARCHAR(50), IN numberOfImages INT)
BEGIN
DECLARE count INT;
DECLARE fileName VARCHAR(100);
SET count = 1;
WHILE count < (numberOfImages + 1) DO
SET fileName = CONCAT(streetName, ' (mls) (', count, ').jpg');
INSERT INTO images_tbl VALUES (NULL, stagingID, fileName, 0);
SET count = count + 1;
END WHILE;
END //
DELIMITER ;
I'm creating a a while loop until 30 and I want to get the x as value into the query. I can't run this because I cant use the x as value. How can I do this ?
I want to use the x as hour id.
Thanks
CREATE DEFINER=`root`#`localhost` PROCEDURE `add_day`(IN `d_id` INT, IN `date` VARCHAR(64))
BEGIN
DECLARE x INT;
SET x = 1;
WHILE x <= 30 DO
INSERT INTO `e-heal`.`scheduler` (`scheduler_id` ,`d_id` ,`hour_id` ,`date` ,`available`)
VALUES (NULL , '5', x, '2013-04-22', '0');
SET x = x + 1;
END WHILE;
END
It works for me as is. You can try adding # in front of x:
DROP PROCEDURE `add_day`;
DELIMITER $$
CREATE DEFINER=`root`#`localhost`
PROCEDURE `add_day`(IN `d_id` INT, IN `date` VARCHAR(64))
BEGIN
DECLARE x INT;
SET #x = 1;
WHILE #x <= 30 DO
INSERT INTO `e-heal`.`scheduler`
(`scheduler_id` ,`d_id` ,`hour_id` ,`date` ,`available`)
VALUES (NULL , '5', #x, '2013-04-22', '0');
SET #x = #x + 1;
END WHILE;
END $$
DELIMITER ;
But I'm wondering what you are trying to do with the two in-parameters that you never use, and that have same name as two of the columns.