简体   繁体   中英

Sub-Select query error in SQL Server 2008

Consider the below query:

insert into dbo.SubscriptionDetails (taxonomyid) 
values(select objectid from SubscriptionObjects where objectname=?)

This query works fine in my local environment and but gives issues while executed the production environment.

Does the sub-select query needs any properties to be set on the SQL-Server level?

I use Java - JDBC for SQL transactions.

See stack trace below:

2013.03.28 15:42:11 CDT ERROR Error while inserting records into SubscriptionDetails table..
java.sql.BatchUpdateException: Subqueries are not allowed in this context. Only scalar expressions are allowed.

I'm surprised your version works in any environment! Try to omit values :

insert  into dbo.SubscriptionDetails 
        (taxonomyid) 
select  objectid 
from    SubscriptionObjects 
where   objectname=?

For more than one subquery, you can:

insert  into dbo.SubscriptionDetails 
        (taxonomyid, contenttypeid) 
select  (select objectid from SubscriptionObjects where objectname=?)
,       (select objectid from SubscriptionObjects where objectname=?)

Alternatively, use parenthesis to force scalar context with values :

insert  into dbo.SubscriptionDetails 
        (taxonomyid, contenttypeid) 
values  ((select objectid from SubscriptionObjects where objectname=?),
         (select objectid from SubscriptionObjects where objectname=?))

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