0

I want to run this query

INSERT INTO [tblPollLogs]
           ([lastModified]
           ,[ip]
           ,[a1]
           ,[a2]
         )
     VALUES
           (getdate()
           ,'aaa'
           ,(select top 1 header from [tblPollAnswer] where [pollAnswerId] = @param1)
           ,(select top  1 header from [tblPollAnswer] where [pollAnswerId] = @param2)         
         )

But I get the error

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

can i run this query in one query or i need get the values before running this query

thanks

1
  • 1
    In SQL Server 2008+ subqueries are fine there. BTW: Why are you using top 1 with no order by? If there is more than one matching row it is undeterministic what will be inserted and if there isn't more than one matching row you don't need the top 1. Commented Sep 10, 2013 at 9:39

1 Answer 1

5

Try to change it to insert...select as below

INSERT INTO [tblPollLogs]
           ([lastModified]
           ,[ip]
           ,[a1]
           ,[a2]
         )
select getdate(),
       'aaa',
      (select top 1 header from [tblPollAnswer] where [pollAnswerId] = @param1),
      (select top  1 header from [tblPollAnswer] where [pollAnswerId] = @param2)         
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.