Stored procedure for creating Insert commands in MySql?

I need a stored procedure to get the table records and return the value as insert expressions for the selected records.

For an instance, a stored procedure must have three input parameters ...

1- Table Name

2- Column Name

Column value

If

1- Table Name = "EMP"

2- Column Name = "EMPID"

3- Column value = "15"

Then there should be an exit, select all EMP values, where EMPID is 15 After the values ​​are selected for the above condition, the stored procedure should return a script to insert the selected values.

The purpose of this is to backup the selected values. when the SP returns the value of {Insert statements}, C # simply writes them to the .sql file.

SP, . ..

0
4
DELIMITER $$

DROP PROCEDURE IF EXISTS `sample`.`InsGen` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `InsGen`(
in_db varchar(20),
in_table varchar(20),
in_ColumnName varchar(20),
in_ColumnValue varchar(20)
)
BEGIN

declare Whrs varchar(500);
declare Sels varchar(500);
declare Inserts varchar(200);
declare tablename varchar(20);
declare ColName varchar(20);


set tablename=in_table;


# Comma separated column names - used for Select
select group_concat(concat('concat(\'"\',','ifnull(',column_name,','''')',',\'"\')'))
INTO @Sels from information_schema.columns where table_schema=in_db and table_name=tablename;


# Comma separated column names - used for Group By
select group_concat('`',column_name,'`')
INTO @Whrs from information_schema.columns where table_schema=in_db and table_name=tablename;


#Main Select Statement for fetching comma separated table values

 set @Inserts=concat("select concat('insert IGNORE into ", in_db,".",tablename," values(',concat_ws(',',",@Sels,"),');')
 as MyColumn from ", in_db,".",tablename, " where ", in_ColumnName, " = " , in_ColumnValue, " group by ",@Whrs, ";");

 PREPARE Inserts FROM @Inserts;

EXECUTE Inserts;                    

END $$

DELIMITER ;
0

mysqldump:

mysqldump --no-create-info --skip-triggers 
  --where="$COLUMN_NAME='$COLUMN_VALUE'" --databases $DB --tables $TABLE_NAME
+4

Anuya ( http://kedar.nitty-witty.com/blog/mysql-stored-procedure-to-generate-extract-insert-statement) btw...

mysql:

/*
isNumeric - return 1/true if passed in string is numeric, false otherwise
Usage example: select isNumeric('2012-02-16'); => 0
*/
DROP FUNCTION IF EXISTS `bettermentdb`.`isNumeric`;
DELIMITER ;;
CREATE DEFINER=`betterment-web`@`localhost` FUNCTION `bettermentdb`.`isNumeric`(s varchar(255))
RETURNS TINYINT
DETERMINISTIC
BEGIN
   SET @match ='^(([0-9+-.$]{1})|([+-]?[$]?[0-9]*(([.]{1}[0-9]*)|([.]?[0-9]+))))$';
   RETURN IF(s regexp @match, 1, 0);
END;;
DELIMITER ;

/*
isNumeric - return an input wrapped in "'" if value is non-numeric, original otherwise.
Depends on isNumeric()
Usage example: select wrapNonNumeric(now()); => '2012-02-16'
           select wrapNonNumeric(NULL); => NULL
           select wrapNonNumeric(1); => 1
*/
DROP FUNCTION IF EXISTS `bettermentdb`.`wrapNonNumeric`;
DELIMITER ;;
CREATE DEFINER=`betterment-web`@`localhost` FUNCTION `bettermentdb`.`wrapNonNumeric`(s varchar(255))
RETURNS varchar(255)
DETERMINISTIC
BEGIN
   RETURN IF(isNumeric(s), s, concat("'", s, "'"));
END;;
DELIMITER ;

col , , db.table:

DELIMITER ;;
DROP PROCEDURE IF EXISTS GenerateInsertSQL;
CREATE DEFINER=`root`@`localhost` PROCEDURE GenerateInsertSQL(IN in_db varchar(20), IN in_table varchar(32), IN in_row BIGINT)
READS SQL DATA
BEGIN
   DECLARE nullableValues varchar(1000);
   DECLARE colNames varchar(1000);
   DECLARE insertStmnt varchar(2000);

   SELECT group_concat(concat('IFNULL(wrapNonNumeric(`',column_name,'`), "NULL")')) INTO @nullableValues from information_schema.columns where table_schema=in_db and table_name=in_table;
   SELECT group_concat(concat('`',column_name,'`')) INTO @colNames from information_schema.columns where table_schema=in_db and table_name=in_table;

   SET @insertStmnt=concat("select concat('INSERT INTO `", in_db, "`.`", in_table, "`(", @colNames, ") VALUES (', concat_ws(', ',",@nullableValues,"),');') from ", in_db, ".", in_table, " where id = ", in_row, " group by ", @colNames, ";");
   PREPARE insertStmnt FROM @insertStmnt;
   EXECUTE insertStmnt;
END
DELIMITER ;
+2

.

mysqldump is ok but not too friendly to modify the WHERE clause.

I ended up SQLyog , a MySQL client, it has a function in which you can export the result set of any sql SELECT statement to a bunch of INSERT statements.

0
source

Source: https://habr.com/ru/post/1770977/


All Articles