SQL Server 2008中的子选择查询错误

时间:2013-04-03 12:36:07

标签: java sql-server sql-server-2008 jdbc

考虑以下问题:

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

此查询在我的本地环境中正常工作,但在执行生产环境时会出现问题。

子选择查询是否需要在SQL-Server级别设置任何属性?

我使用Java - JDBC进行SQL事务。

请参阅下面的堆栈跟踪:

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.

1 个答案:

答案 0 :(得分:3)

我很惊讶您的版本适用于任何环境!尝试省略values

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

对于多个子查询,您可以:

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

或者,使用括号来强制标量上下文values

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