Log in

View Full Version : SQL Commands..........?


Kingmen30264
08-28-2011, 04:29 PM
I was just wondering if someone could tell me of an easier way of making a command that will insert multiple values at one time (one line) instead of multiple lines.

Here is an example of what I am asking for:



insert into `tablename`(values) values (number, >= ### and <= ###);



Here is something that I did before and took me a GOOD minute:




INSERT INTO `goallists` (`listid`, `entry`) VALUES
(700, 219000),
(700, 219001),
(700, 219002),
(700, 219003),
(700, 219004),
(700, 219005),
(700, 219006),
(700, 219007),
(700, 219008),
(700, 219009),
(700, 219010),
(700, 219011),
(700, 219012),
(700, 219013),
(700, 219014),
(700, 219015),
(700, 219016),
(700, 219017),
(700, 219018),
(700, 219019),
(700, 219020),
(700, 219021),
(700, 219022),
(700, 219023),
(700, 219024),
(700, 219025),
(700, 219026),
(700, 219027),
(700, 219028),
(700, 219029),
(700, 219030),
(700, 219031),
(700, 219032),
(700, 219033),
(700, 219034),
(700, 219035),
(700, 219036),
(700, 219037),
(700, 219038),
(700, 219039),
(700, 219040),
(700, 219041),
(700, 219042),
(700, 219043),
(700, 219044),
(700, 219045),
(700, 219046),
(700, 219047),
(700, 219048),
(700, 219049),
(700, 219050),
(700, 219051),
(700, 219052),
(700, 219053);



I am not wanting to do something like that again.... specially since I have about 15 other zones that I am trying to do this to, and I don't really have a lot of time atm (school/college) to work with this.

Any and all help is greatly appreciated. (Yes, I have been searching on an answer on my own [when I can].)

Thanks,

Kingmen

Derision
08-28-2011, 05:48 PM
Assuming your values are all consecutive. then this seems to work:

set @listid = 700;
set @start = 219000;
set @end = 219053;
DELETE from goallists where listid = @listid;
DROP PROCEDURE IF EXISTS myProc;
delimiter $$
CREATE PROCEDURE myProc()
DETERMINISTIC
BEGIN
DECLARE counter INT DEFAULT @start;

simple_loop: LOOP
INSERT INTO `goallists` (`listid`, `entry`) VALUES (@listid, counter);
SET counter=counter+1;
IF counter>@end THEN
LEAVE simple_loop;
END IF;
END LOOP simple_loop;
SELECT * from goallists where listid = @listid;
END$$
delimiter ;
call myProc();

Just change the listid, start and end values at the top of the code to what you want.

PS: I had to google for how to do this (I'm not an SQL expert either :) ), code copied from here: http://www.java2s.com/Code/SQL/Procedure-Function/SimpleLOOP.htm

Kingmen30264
08-28-2011, 07:04 PM
VERY Nice. Thank you so much Derision... This worked like a charm :)