Insertar el uso de una sola instrucción insert
-
12-09-2019 - |
Pregunta
Quiero insertar los datos en mi tabla tblSubscriptions y solo quiero usar una instrucción insert.Voy a insertar los datos de cada usuario en la Tabla de Usuario.El siguiente SQL doesnot trabajo.
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)
Cómo hacer esto utilizando una instrucción insert yo.e Sin cursores.
Solución
Consulta
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)
Otros consejos
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
La cláusula IN no funcionará con NULL
Basta con retirar los 'valores'
INSERT(...)
SELECT ...
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow