我如何使用2行插入SQL?

时间:2016-01-11 08:40:59

标签: sql-server

第一个表格如下: - Car_idCar_ModelCar_type car_idPK

第二个表格如下: - car_idjob_numberdatecurrent_distPrev_dist  car_idFK

我希望使用car_idjob_number

在第二个表格中插入新值

3 个答案:

答案 0 :(得分:1)

在选择查询中代替job_numberdatecurrent_distPrev_dist你应该给出要插入表格的实际值

INSERT INTO Test1 (car_id, job_number, [date], current_dist, Prev_dist)
SELECT car_id,
       job_number,
       [date],
       current_dist,
       prev_dist
FROM Test
WHERE car_id = value;

答案 1 :(得分:0)

查询将类似于

INSERT INTO second_Table (car_id, job_number, date, current_dist, Prev_dist)
SELECT car_id,
       job_number,
       [date],
       current_dist,
       prev_dist
FROM first_Table
WHERE car_id = value

在此查询job_number中,date,current_dist,prev_dist将具有实际值,car_id将来自first_Table

例如:假设job_number = 123,date =' 2015-12-08 05:52:53.717',current_dist = 123,prev_dist = 234.然后查询就像

INSERT INTO second_Table (car_id, job_number, date, current_dist, Prev_dist)
SELECT car_id,
       123,
       '2015-12-08 05:52:53.717',
       123,
       234
FROM first_Table
WHERE car_id = value

答案 2 :(得分:0)

在oracle中正常工作

在select query中代替job_number,date,current_dist,Prev_dist你应该给出要在表中插入的实际值

INSERT INTO Test1 (car_id, job_number, [date], current_dist, Prev_dist)
SELECT car_id,
       job_number,
       [date],
       current_dist,
       prev_dist
FROM Test
WHERE car_id = value;