chris.botma
09-21-2011, 08:52 AM
I have the following coding, it generates a 6 digit number, now what I would like to know is, how am I gonna select a single number out of that 6 digits? Say if i want to select the second digit out of the 6? The reason for doing so, I want to add a check digit in MySQL.
-- Delete the old version of the procedure so we start fresh
DROP PROCEDURE IF EXISTS mwc_account_batch_generator;
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Routine: mwc_account_batch_generator
-- Project: mwc account management
-- Author: Christiaan Botma
-- Date: 2011-09-20
--
-- [Short Description of the routine goes here]
--
-- [Define all input parameters here]
--
-- [Define what is returned here]
--
-- --------------------------------------------------------------------------------
-- change the delimeter to cause MySQL to see the following block
-- of statements as a single command
DELIMITER $$
-- Create the procedure, give it a name and define the input parameters
CREATE PROCEDURE `mwc_account_batch_generator` (
IN in_total_accounts BIGINT ,
IN in_initial_balance float(10,2)
-- CONSTRAINT loc_un UNIQUE(location)
)
BEGIN
DECLARE i INT DEFAULT 1;
drop table if exists tmp_account_generator;
create temporary table tmp_account_generator
(
acc_number BIGINT not null,
balance float(10,2) not null,
UNIQUE KEY (acc_number)
);
-- Stored procedure code goes here
-- insert new accounts into temp table with the balance specified by initial balance.
-- random account number generation rules:
-- Number must contain 6 digits
-- Number must not exist in the table, has to be unique
WHILE i <= in_total_accounts DO
insert into tmp_account_generator(acc_number, balance) values (100000 + (RAND() * 899999), in_initial_balance);
SET i = i + 1;
END WHILE;
-- select all new generated accounts
select * from tmp_account_generator;
END$$
call mwc_account_batch_generator(10, 10.02);
-- Delete the old version of the procedure so we start fresh
DROP PROCEDURE IF EXISTS mwc_account_batch_generator;
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Routine: mwc_account_batch_generator
-- Project: mwc account management
-- Author: Christiaan Botma
-- Date: 2011-09-20
--
-- [Short Description of the routine goes here]
--
-- [Define all input parameters here]
--
-- [Define what is returned here]
--
-- --------------------------------------------------------------------------------
-- change the delimeter to cause MySQL to see the following block
-- of statements as a single command
DELIMITER $$
-- Create the procedure, give it a name and define the input parameters
CREATE PROCEDURE `mwc_account_batch_generator` (
IN in_total_accounts BIGINT ,
IN in_initial_balance float(10,2)
-- CONSTRAINT loc_un UNIQUE(location)
)
BEGIN
DECLARE i INT DEFAULT 1;
drop table if exists tmp_account_generator;
create temporary table tmp_account_generator
(
acc_number BIGINT not null,
balance float(10,2) not null,
UNIQUE KEY (acc_number)
);
-- Stored procedure code goes here
-- insert new accounts into temp table with the balance specified by initial balance.
-- random account number generation rules:
-- Number must contain 6 digits
-- Number must not exist in the table, has to be unique
WHILE i <= in_total_accounts DO
insert into tmp_account_generator(acc_number, balance) values (100000 + (RAND() * 899999), in_initial_balance);
SET i = i + 1;
END WHILE;
-- select all new generated accounts
select * from tmp_account_generator;
END$$
call mwc_account_batch_generator(10, 10.02);