Insert into select 语法
语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1
或者:Insert into Table2 select * from Table1
注意:(1)要求目标表Table2必须存在,并且字段field,field2...也必须存在
(2)注意Table2的主键约束,如果Table2有主键而且不为空,则 field1, field2...中必须包括主键
(3)注意语法,不要加values,和插入一条数据的sql混了,不要写成:
Insert into Table2(field1,field2,...) values (select value1,value2,... from Table1)
使用示例
insert into daily_gravida (person_id,person_name,depart_id,holidaystart,holidayend,reporttime,starttime,endtime,remarks,state,apply_user_id,apply_user_name) select v.vacation_user_id as person_id, v.vacation_user_name as person_name, v.vacation_dept_id as depart_id, CONVERT(CHAR(10), v.start_time, 23) as holidaystart, CONVERT(CHAR(10), v.end_time, 23) as holidayend, CONVERT(CHAR(10), getdate(), 120) as reporttime, CONVERT(CHAR(10), getdate(), 120) as starttime, CONVERT(CHAR(10), getdate(), 120) as endtime, '' as remarks, '2' as state, v.apply_user_id as apply_user_id, v.apply_user_name as apply_user_name from vacation_info v where v.vacation_id='3';
效果
源表:
目标表:
发表评论