我需要一个storedprocedure来获取Table的记录并将值作为Insert Statements返回 选定的记录。
对于Instance,存储过程应该有三个输入参数......
1-表名
2-列名称
3-列值
如果
1-表名=“EMP”
2-列名=“EMPID”
3-列值=“15”
然后输出应该是,选择EMPID为15的EMP的所有值 为上述条件选择值后,存储过程必须 返回插入所选值的脚本。
这样做的目的是备份选定的值。当SP返回时 值{Insert statements},c#只会将它们写入.sql文件。
我不知道编写此SP,任何代码示例都是适用的。 感谢..
答案 0 :(得分:4)
您可以使用mysqldump执行此操作:
mysqldump --no-create-info --skip-triggers
--where="$COLUMN_NAME='$COLUMN_VALUE'" --databases $DB --tables $TABLE_NAME
答案 1 :(得分:2)
扩展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 :(得分:0)
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 ;
答案 3 :(得分:0)
今天不得不处理这个问题。
mysqldump没有问题,但不太友好,无法更改WHERE子句。
我结束了SQLyog,一个MySQL客户端,它有一个功能,你可以将任何sql SELECT语句的结果集导出到一堆INSERT语句中。