使用单个插入语句插入

时间:2009-08-11 09:15:55

标签: sql sql-server-2005 tsql insert-update

我想在我的表tblSubscriptions中插入数据,我只想使用一个insert语句。 我将为User Table中的每个userId插入数据。以下SQL不起作用。

Insert tblSubscriptions (UserID, ProductID, isACtive, SubscriptionDays, Price, MonthlyPrice, ProductType, CurrencyID)
Values ((Select userID From tblUser where SubscriptionType in(1, 0, null)), 7, 1, 0, 0, 0, 30, 1)

如何使用一个插入语句,即没有游标。

3 个答案:

答案 0 :(得分:6)

查询

INSERT INTO tblSubscriptions (UserID, ProductID, IsActive, SubscriptionDays, Price, MonthlyPrice, ProductType, CurrencyID)
SELECT UserID, 7, 1, 0, 0, 0, 30, 1 FROM tblUser WHERE ISNULL(SubscriptionType, 0) IN (1, 0)

答案 1 :(得分:3)

Insert tblSubscriptions (UserID, ProductID, isACtive, SubscriptionDays, Price, MonthlyPrice, ProductType, CurrencyID)
Select userID, 7, 1, 0, 0, 0, 30, 1
From tblUser where SubscriptionType in (1, 0) or SubScriptionType is null

IN子句不适用于NULL

答案 2 :(得分:0)

只需删除'值'

即可
INSERT(...)
SELECT ...