-1

考虑以下查询:

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.
4

1 回答 1

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=?))
于 2013-04-03T12:37:17.667 回答