views:

112

answers:

3

Hi, I want to insert the data in my table tblSubscriptions and I only want to use one insert statement. I am going to insert data for every userId in User Table. The following SQL doesnot work.

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)

How to do this using one insert statement i.e Without cursors.

A: 

Just remove 'Values'

INSERT(...)
SELECT ...
Dewfy
+6  A: 

Query

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)
Kirtan
+3  A: 
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

The IN clause will not work with NULL

CodeByMoonlight
I'd try both this and the accepted answer. I suspect that if there is a data set that is large, this one might perform better.
HLGEM