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.
Related
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
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;
Please help me understand,
I need to make stored procedure for creating new user into db.
delimiter //
CREATE PROCEDURE add_user (x VARCHAR(25))
BEGIN
DECLARE x VARCHAR(25) DEFAULT 'mark';
DECLARE newname VARCHAR(25);
DECLARE xid INT;
SELECT x, id INTO newname, xid
FROM users WHERE x = x;
SELECT newname;
END;
delimiter ;
when i call add_user('peter');
it shows me:
newname/null
where do i go wrong ?
CREATE PROCEDURE add_user (x VARCHAR(25))
BEGIN
DECLARE x VARCHAR(25) DEFAULT 'mark';
This actually creates two variables named x. The first is the function parameter, and the second is a local variable which is in scope inside the BEGIN ... END block. According to MySQL's scoping rules, only one of these variables can be visible, which means that the parameter x is hidden and inaccessible inside the BEGIN ... END block.
The solution is simply to delete the line
DECLARE x VARCHAR(25) DEFAULT 'mark';
and, if you need to assign a default value, do it in an IF block:
IF x IS NULL THEN
SET x = 'mark';
END IF;
The complete function definition should be
delimiter //
CREATE PROCEDURE add_user (x VARCHAR(25))
BEGIN
DECLARE newname VARCHAR(25);
DECLARE xid INT;
IF x IS NULL THEN
SET x = 'mark';
END IF;
SELECT x, id INTO newname, xid
FROM users WHERE x = x;
SELECT newname;
END;
delimiter ;
I have the following stored procedure where I need to insert a set of data for the columns Category, Function & Status. Where category always should be '1' and Function 1 to 80, status always 'ACTIVE'.
BEGIN
DECLARE x INT;
SET x = 1;
WHILE x <= 80 DO
insert into functiontocategory (category,`function`,`status`) values ('1',x,'ACTIVE');
SET x = x+1;
END WHILE;
END
But it gives me 160 rows of inserted data, where 2 sets of 1 to 80s instead of one set. what is wrong with my procedure.
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `test`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `test`()
BEGIN
DECLARE X INT;
SET X = 1;
WHILE X <= 80 DO
INSERT INTO functiontocategory (category,`function`,`status`) VALUES ('1',X,'ACTIVE');
SET X = X+1;
END WHILE;
END$$
DELIMITER ;
I created a database as test and a table functiontocategory (category int(3),function text,status varchar(10)) and a procedure with a name test..for testing I used call test() and it inserted 80 rows exactly
I have a little problem. Looks like the procedure does not exist. Somehow it's dropped after the creation. I get different error each time i change something. I'm not really sure what's causing the error, maybe I'm not allowed to drop procedures and creating them in the same query.
I hope you guys can help me out.
drop procedure if exists refIntChk;
DELIMITER //
CREATE PROCEDURE refIntChk(IN district INT(11), OUT b INT(1))
BEGIN
DECLARE b INT(1);
IF district IN (select dist FROM t13)
THEN
SET b = 1;
ELSE
SET b = 0;
END IF;
END; //
DELIMITER ;
drop procedure gen if exists ;
DELIMITER //
CREATE PROCEDURE gen()
BEGIN
DECLARE rows INT(11) DEFAULT (SELECT COUNT(dist) FROM t13);
DECLARE district INT(11);
DECLARE custname VARCHAR(16);
DECLARE revenue FLOAT;
DECLARE x INT DEFAULT 10000;
DECLARE outvar INT(11);
WHILE x > 0
DO
SET district = FLOOR(RAND()*rows)+1;
CALL refIntChk(district, outvar);
IF outvar = 1
THEN
SET custname = substring(MD5(RAND()), -16);
SET revenue = (RAND() * 10);
INSERT INTO t14 VALUES(NULL, custname, district, revenue);
SET x = x - 1;
END IF;
END WHILE;
END;//
DELIMITER ;
CALL gen();
When you get errors, it's usually good to run each statement, one by one, and see which one is producing the error.
The second DROP procedure statement should be:
drop procedure if exists gen;