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: ,
Related
I have a table with following prototype:
CREATE TABLE `T2` (
`ID` int(11) DEFAULT NULL,
`NAME` varchar(100) DEFAULT NULL
)
I want to create procedure to insert random strings with a specific length and I wrote the following:
DELIMITER ;;
CREATE PROCEDURE STRGEN ( IN ITER INT) BEGIN
DECLARE COUNTER INT DEFAULT 0;
DECLARE _STR VARCHAR(1000) DEFAULT NULL;
WHILE COUNTER < #ITER DO
SET _STR = CONCAT(_STR , CHAR(RAND()*100+65));
SET COUNTER = COUNTER + 1;
END WHILE;
INSERT INTO T2 VALUES(RAND()*100 , _STR);
END;;
And called it this way:
CALL STRGEN(100);
The problem is that it inserts NULL as the NAME field.
My suspicion is that changes made to _STR goes out of scope when the while loop terminates.
I've also tried using a global variable like the following:
CREATE PROCEDURE STRGEN ( IN ITER INT) BEGIN
DECLARE COUNTER INT DEFAULT 0;
DECLARE _STR VARCHAR(1000) DEFAULT NULL;
SET #S = NULL;
WHILE (COUNTER < #ITER) DO
SET _STR = CONCAT(_STR , CHAR(RAND()*100+65));
SET COUNTER = COUNTER + 1;
SET #S = _STR;
END WHILE;
INSERT INTO T2 VALUES(RAND()*100 , #S);
END;;
And also declaring #S outside of the procedure , but nothing special happened.
Any help is appreciated.
Best regards.
When I tried running your procedure, I got the following error:
Error Code: 1366. Incorrect string value: '\x87' for column '_STR' at
row 1 0.000 sec
It seems that you are not generating valid chars... I tried a different char generator logic and it works as expected.
DELIMITER ;;
CREATE PROCEDURE STRGEN ( IN ITER INT) BEGIN
DECLARE COUNTER INT DEFAULT 0;
DECLARE _STR VARCHAR(1000) DEFAULT ''; -- change the initial value to ''
WHILE COUNTER < #ITER DO
-- SET _STR = CONCAT(_STR , CHAR(RAND()*100+65));
-- if I change the logic for generating random char, then I get expected result
SET _STR = CONCAT(_STR , LEFT(MD5(RAND()), 1));
SET COUNTER = COUNTER + 1;
END WHILE;
INSERT INTO T2 VALUES(RAND()*100 , _STR);
END;;
sorry for my english :)
I need (only with SQL) select all column values form table A, split its by separator and insert separated values to other table?
For example, i have table like this:
ID, NAME, MAGIC
1, Marty, ACD ACFX U128BH
and i need export "MAGIC" value to separate table like this:
ID, USERID, MAGIC
1, 1, ACD
2, 1, ACFX
3, 1, U128BH
How to do it? I found eg. SQL insert data to other table after split string, but this is MS SQL syntax (?) and im using MySQL.
Thanx for reply
EDIT THIS ANSWER TO MUCH YOUR WORK
#drop table magic_t;
create table magic_t(
id int,
name varchar(100),
magic varchar(100)
);
#drop table split_insert;
create table split_insert(
split_value varchar(100)
);
insert into magic_t values(1,'nam1','VAL1 VAL2 VAL3');
insert into magic_t values(1,'nam1','VAL4 VAL5 VAL3');
#drop function strSplit;
DELIMITER ;;
CREATE FUNCTION strSplit(x varchar(255), delim varchar(12), pos int) returns varchar(255)
return replace(substring(substring_index(x, delim, pos), length(substring_index(x, delim, pos - 1)) + 1), delim, '');;
DELIMITER ;
#drop procedure split_table;
DELIMITER ;;
CREATE procedure split_table ()
begin
DECLARE done INT DEFAULT 0;
DECLARE while_condition boolean default true;
DECLARE counter INT DEFAULT 0;
DECLARE magic_value varchar(100);
declare splited_value varchar(100);
DECLARE colum_list CURSOR FOR SELECT magic FROM magic_t;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1; -- to stop loop when fetch is over
OPEN colum_list;
REPEAT FETCH colum_list INTO magic_value;
if done =0 then
set counter =1;
set while_condition = true;
while(while_condition) do
set splited_value = strSplit(magic_value,' ',counter) ;
if splited_value <>'' then
insert into split_insert values(splited_value);
end if;
if splited_value='' then set while_condition=false; end if;
set counter =counter+1;
end while;
end if;
UNTIL done -- here stop repeat when done =1
END REPEAT;
end ;;
DELIMITER ;
call split_table ();
select * from split_insert;
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 trying to create a procedure with a loop inside, but when i call it just show the first record and dont interate.
delimiter //
create procedure load_foo_test_data()
begin
declare v_max int unsigned default 10;
declare v_counter int unsigned default 1;
select COUNT(*) into v_max FROM emp;
start transaction;
while v_counter < v_max do
select * from emp where emp_id = v_counter;
set v_counter=v_counter+1;
end while;
commit;
end
//
delimiter ;
when i call the procedure, it shows me just the first record (with ID 1).
I tried with "select v_counter" and only shows 1 too.
and tried with loop instead while and same thing.
i'm working with sequel pro in mysql 5.5
Thanks!
I think you should use
while ( ) {
// do this
}
Try to use
SET v_counter = 0;
REPEAT
IF v_counter < v_max do THEN
select * from emp where emp_id = v_counter;
set v_counter=v_counter+1;
END IF;
UNTIL v_counter END REPEAT;
I have a table that stores some comma separated strings. Is there a way to write a sql to
return me a separate rows for each token string obtained by splitting across commas.
You can simply write and call a stored procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS explode_table $$
CREATE PROCEDURE explode_table(bound VARCHAR(255))
BEGIN
DECLARE id INT DEFAULT 0;
DECLARE value TEXT;
DECLARE occurance INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
DECLARE splitted_value INT;
DECLARE done INT DEFAULT 0;
DECLARE cur1 CURSOR FOR SELECT table1.id, table1.value
FROM table1
WHERE table1.value != '';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
DROP TEMPORARY TABLE IF EXISTS table2;
CREATE TEMPORARY TABLE table2(
`id` INT NOT NULL,
`value` VARCHAR(255) NOT NULL
) ENGINE=Memory;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO id, value;
IF done THEN
LEAVE read_loop;
END IF;
SET occurance = (SELECT LENGTH(value)
- LENGTH(REPLACE(value, bound, ''))
+1);
SET i=1;
WHILE i <= occurance DO
SET splitted_value =
(SELECT REPLACE(SUBSTRING(SUBSTRING_INDEX(value, bound, i),
LENGTH(SUBSTRING_INDEX(value, bound, i - 1)) + 1), ',', ''));
INSERT INTO table2 VALUES (id, splitted_value);
SET i = i + 1;
END WHILE;
END LOOP;
SELECT * FROM table2;
CLOSE cur1;
END; $$
-------------------
CALL explode_table(',');