今天同事问我一个问题,要求insert into select 语句但是呢,要求把values值显示到sql语句
这是他的原始sql,
insert into sys_dictionary_item (type_id,item_name,item_value,is_active,is_default,sequence)
select type_id,item_name,item_value,is_active,is_default,sequence from sys_dictionary_item where type_id=13;
对于个人本地执行这么写当然是可以的,但是要是发给dba执行,这么写会被喷的,因此要求换一种使用concat 这种拼装sql的形式实现。
SELECT
CONCAT( "insert into sys_dictionary_item(type_id,item_name,item_value,is_active,is_default,sequence)
values", "(", type_id, ",'", item_name, "',", item_value, ",", is_active, ",", is_default, ",", sequence, ");" ) as sqlStr
FROM
sys_dictionary_item
WHERE
type_id = 13;
看下最后的效果
insert into sys_dictionary_item(type_id,item_name,item_value,is_active,is_default,sequence)
values(13,'人员聚集',1,1,0,1);
insert into sys_dictionary_item(type_id,item_name,item_value,is_active,is_default,sequence)
values(13,'人员走失',5,1,0,1);
insert into sys_dictionary_item(type_id,item_name,item_value,is_active,is_default,sequence)
values(13,'人员倒地',4,1,0,0);
insert into sys_dictionary_item(type_id,item_name,item_value,is_active,is_default,sequence)
values(13,'打架斗殴',3,1,0,0);
insert into sys_dictionary_item(type_id,item_name,item_value,is_active,is_default,sequence)
values(13,'车辆拥堵入口',18,1,0,0);
insert into sys_dictionary_item(type_id,item_name,item_value,is_active,is_default,sequence)
values(13,'车辆拥堵出口',19,1,0,0);
insert into sys_dictionary_item(type_id,item_name,item_value,is_active,is_default,sequence)
values(13,'周界入侵',9,1,0,0);
是想要的结果,类似update 、delete都可以这样实现。