当添加多行时,SQL插入到..选择+添加列

时间:2016-12-19 16:14:18

标签: sql insert-into

我正在尝试从TherapistRates表中向我的serviceLink表中插入多行。我需要在每一行上加therapistID。但是,SQL不允许我在插入多行时添加值。我该如何解决这个问题?

这是我的问题:

INSERT INTO TherapistRates (therapistID, serviceType)  
VALUES (@therapistID, (SELECT (serviceID) FROM serviceLink WHERE TherapistTypeID = @therapistID)

2 个答案:

答案 0 :(得分:2)

为什么不:

insert into TheRapistRates (TherapistID, serviceType)
select TherapistTypeID , serviceID
from serviceLink
where TherapistTypeID = @TherapistID

NEW VERSION(来自对其他答案的评论)

insert into TheRapistRates (TherapistID, serviceType)
select @TherapistID, serviceID
from serviceLink
where TherapistTypeID = @TherapistTypeID

答案 1 :(得分:0)

这是你要找的吗?

Insert  TherapistRates 
        (therapistID, serviceType)
Select  @TherapistId, ServiceId
From    ServiceLink
Where   TherapistTypeID = @TherapistTypeID