从select语句向表中插入数据

时间:2013-12-08 01:09:09

标签: sql-server-2008

我在两个表(人)和(付款)工作.payments表包含列(payment_id,payment_value,payment_class,日期,名称,person_id)现在我想通过从人员表中选择来插入数据到付款

insert into Payments(payment_id , payment_value , payment_class , date , name , person_id)
select person_id , person_name , person_status from Persons where person_status = 'sub'

如何向从人员表中选择的每个人插入付款..

1 个答案:

答案 0 :(得分:0)

根据您的要求,以下查询为每个人插入一张记录,其中当前日期和所有其他列在表格付款中为空

INSERT INTO Payments (payment_id,payment_value,payment_class,date,name,person_id)
SELECT NULL, NULL, NULL, GETDATE(), NULL, NULL
FROM Persons
WHERE person_status = 'sub';

如果你想直接从桌面人物中提取person_id,你可以这样做:

INSERT INTO Payments (payment_id,payment_value,payment_class,date,name,person_id)
SELECT NULL, NULL, NULL, GETDATE(), NULL, person_id
FROM Persons
WHERE person_status = 'sub';