准备
两张表,表a、表b
实现将a表中指定的字段添加到b表中**
现在book2中没有数据,但book有
book2
book
执行脚本
结果
脚本如下
DROP PROCEDURE IF EXISTS synchrodata;
CREATE PROCEDURE synchrodata(
IN ins_tab_name longtext,IN ins_field longtext,
IN sel_field longtext,IN sel_tab_name longtext,
IN whe_field longtext,IN groupby_field longtext,
IN having_field longtext,IN orderby_field longtext,
IN limit_field longtext
)
BEGIN
SET @sqlcmd = concat(
'INSERT INTO ',ins_tab_name,' (',ins_field,')
SELECT ',sel_field,' FROM ',sel_tab_name,' ',whe_field,
' ',groupby_field,' ',having_field,' ',orderby_field,' ',limit_field);
select @sqlcmd;
prepare stmt from @sqlcmd;
execute stmt;
deallocate prepare stmt;
END;
脚本如下-可以获取执行时间(建议使用2)
DROP PROCEDURE IF EXISTS synchrodata;
CREATE PROCEDURE synchrodata(
IN ins_tab_name longtext,IN ins_field longtext,
IN sel_field longtext,IN sel_tab_name longtext,
IN whe_field longtext,IN groupby_field longtext,
IN having_field longtext,IN orderby_field longtext,
IN limit_field longtext
)
BEGIN
set @begintime=now();
select @begintime;
SET @sqlcmd = concat(
'INSERT INTO ',ins_tab_name,' (',ins_field,')
SELECT ',sel_field,' FROM ',sel_tab_name,' ',whe_field,
' ',groupby_field,' ',having_field,' ',orderby_field,' ',limit_field);
prepare stmt from @sqlcmd;
execute stmt;
deallocate prepare stmt;
set @endtime=now();
select @endtime;
SELECT TIMESTAMPDIFF(SECOND,@begintime,@endtime);
END;
DROP PROCEDURE IF EXISTS synchrodata;
CREATE PROCEDURE synchrodata(
IN ins_tab_name longtext,IN ins_field longtext,
IN sel_field longtext,IN sel_tab_name longtext,
IN whe_field longtext,IN groupby_field longtext,
IN having_field longtext,IN orderby_field longtext,
IN limit_field longtext
)
BEGIN
declare begintime datetime default now();
declare endtime datetime ;
SET @sqlcmd = concat(
'INSERT INTO ',ins_tab_name,' (',ins_field,')
SELECT ',sel_field,' FROM ',sel_tab_name,' ',whe_field,
' ',groupby_field,' ',having_field,' ',orderby_field,' ',limit_field);
prepare stmt from @sqlcmd;
execute stmt;
deallocate prepare stmt;
set endtime=now();
SELECT TIMESTAMPDIFF(SECOND,begintime,endtime);
END;