views:

118

answers:

3

I want to execute this query :

INSERT INTO [Order] (FactorId, ProductId, Entity) 
VALUES((SELECT Top 1 FactorId FROM Factor WHERE UserId = @UserId AND Status = -1), @ProductId, @Entity)

but the following error occurs :

Subqueries are not allowed in this context. Only scalar expressions are allowed.

A: 
INSERT INTO [Order] (FactorId, ProductId, Entity) 
SELECT (select Top 1 FactorId FROM Factor WHERE UserId = @UserId AND Status = -1), 
@ProductId, @Entity
Preet Sangha
I executed this query, but the following error occurs:The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns.
Mehdi Dehghani
A: 

try with this

INSERT INTO [Order] (FactorId, ProductId, Entity) 
(SELECT Top 1 FactorId, @ProductId, @Entity FROM Factor 
WHERE UserId = @UserId AND Status = -1)

Note : insert query with select wont allow VALUES keyword :) .

anishmarokey
+4  A: 

Try

INSERT INTO [Order] (FactorId, ProductId, Entity) 
SELECT Top 1 FactorId, @ProductId, @Entity FROM Factor WHERE UserId = @UserId AND Status = -1
astander