The following are the sample Stored Procedure for reading the data from CSV file and insert into database table.
DELIMITER //
DROP PROCEDURE IF EXISTS `CHECKSERIALNUMBER`;
DROP TABLE IF EXISTS `SERIAL_NUMBER_TEMP_TABLE`;
CREATE TABLE `SP_TEST`.`SERIAL_NUMBER_TEMP_TABLE`(`ID` INT(5) NOT NULL ,`SERIAL_NUMBER` VARCHAR(255));
LOAD DATA LOCAL INFILE '/home/eswar/file.csv' INTO TABLE `SP_TEST`.`SERIAL_NUMBER_TEMP_TABLE`
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n' IGNORE 1 LINES;
CREATE PROCEDURE `CHECKSERIALNUMBER`()
BEGIN
DECLARE _COUNT INT;
DECLARE _I INT DEFAULT 1;
DECLARE _SNO VARCHAR(255);
DECLARE _SNOEXIST VARCHAR(255);
SELECT COUNT(*) INTO _COUNT FROM `SERIAL_NUMBER_TEMP_TABLE`;
WHILE _COUNT >= _I DO
SELECT `SERIAL_NUMBER` INTO _SNO FROM `SERIAL_NUMBER_TEMP_TABLE` WHERE ID=_I;
SELECT `SERIAL_NUMBER` INTO _SNOEXIST FROM `RT_TEST__TABLE` WHERE `SERIAL_NUMBER`=_SNO;
IF _SNOEXIST IS NULL THEN
SELECT _SNO,'Not Exist';
ELSE
SELECT _SNO,'Exist';
END IF;
SET _I=_I+1;
SET _SNOEXIST = null;
END WHILE;
END //
DELIMITER ;
DROP PROCEDURE IF EXISTS `CHECKSERIALNUMBER`;
DROP TABLE IF EXISTS `SERIAL_NUMBER_TEMP_TABLE`;
CREATE TABLE `SP_TEST`.`SERIAL_NUMBER_TEMP_TABLE`(`ID` INT(5) NOT NULL ,`SERIAL_NUMBER` VARCHAR(255));
LOAD DATA LOCAL INFILE '/home/eswar/file.csv' INTO TABLE `SP_TEST`.`SERIAL_NUMBER_TEMP_TABLE`
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n' IGNORE 1 LINES;
CREATE PROCEDURE `CHECKSERIALNUMBER`()
BEGIN
DECLARE _COUNT INT;
DECLARE _I INT DEFAULT 1;
DECLARE _SNO VARCHAR(255);
DECLARE _SNOEXIST VARCHAR(255);
SELECT COUNT(*) INTO _COUNT FROM `SERIAL_NUMBER_TEMP_TABLE`;
WHILE _COUNT >= _I DO
SELECT `SERIAL_NUMBER` INTO _SNO FROM `SERIAL_NUMBER_TEMP_TABLE` WHERE ID=_I;
SELECT `SERIAL_NUMBER` INTO _SNOEXIST FROM `RT_TEST__TABLE` WHERE `SERIAL_NUMBER`=_SNO;
IF _SNOEXIST IS NULL THEN
SELECT _SNO,'Not Exist';
ELSE
SELECT _SNO,'Exist';
END IF;
SET _I=_I+1;
SET _SNOEXIST = null;
END WHILE;
END //
DELIMITER ;
No comments:
Post a Comment