简体   繁体   中英

sql insert into with sub-query

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

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)         

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM