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

本文介绍了一种常见的SQL编程问题,即如何正确地从一个表中获取最大值并将其插入到另一个表中。通过对比错误与正确的SQL语句,解释了为何原语句会触发子查询不允许在此上下文的错误,并给出了有效的解决方案。

我想获取表中某一列的最大值,然后插入到另外一个表中,报错的Sql如下:

insert into AA(AA_ID,AA_YWFSRQ) 
values
(NewID(),(  SELECT   CONVERT(varchar, DATEADD(day,- 1, CAST(SUBSTRING(MAX(YEB_RQ), 1, 4) 
                                    + SUBSTRING(MAX(YEB_RQ), 5, 2) + SUBSTRING(MAX(YEB_RQ), 7, 2) 
                                     AS datetime)), 112) FROM      YEB ));
修正:

insert into AA(AA_ID,AA_YWFSRQ) 
select
NewID(),(  SELECT   CONVERT(varchar, DATEADD(day,- 1, CAST(SUBSTRING(MAX(YEB_RQ), 1, 4) 
                                    + SUBSTRING(MAX(YEB_RQ), 5, 2) + SUBSTRING(MAX(YEB_RQ), 7, 2) 
                                     AS datetime)), 112) FROM      YEB )
错误提示信息:
Subqueries are not allowed in this context. Only scalar expressions are allowed.

==》在此上下文中不允许使用子查询。只允许使用标量表达式。

public List<OaCoopApprove> findDoneList(String userName) { Criteria criteria = this.getSession().createCriteria(OaCoopApprove.class, "a"); criteria.createAlias("oaWorkflowNode", "b", Criteria.INNER_JOIN); criteria.add(Restrictions.ne("b.type", ScpConstants.NUMBER_3)); criteria.add(Restrictions.eq("a.userName", userName)); DetachedCriteria subQuery = DetachedCriteria.forClass(OaCoopApprove.class, "c"); subQuery.setProjection(Projections.max("c.workDate")); subQuery.add(Restrictions.eqProperty("c.oaCoopId", "a.oaCoopId")); subQuery.add(Restrictions.eq("c.userName", userName)); criteria.add(Subqueries.propertyEq("a.workDate", subQuery)); criteria.setFirstResult(0); criteria.setMaxResults(4); List<OaCoopApprove> list = criteria.list(); if (list != null && list.size() > 0) { return list; } return new ArrayList<OaCoopApprove>(); } Hibernate: select top 5 this_.id as id147_0_, this_.chengWenClass as chengWen2_147_0_, this_.contentId as contentId147_0_, this_.createDate as createDate147_0_, this_.createUserId as createUs5_147_0_, this_.createUserName as createUs6_147_0_, this_.state as state147_0_, this_.updateTime as updateTime147_0_, this_.updateUserId as updateUs9_147_0_, this_.updateUserName as updateU10_147_0_ from KY_CHENG_WEN_LOG this_ where this_.state=? order by this_.updateTime asc Hibernate: select this_.id as id536_0_, this_.affirmDate as affirmDate536_0_, this_.assessMess as assessMess536_0_, this_.assessedObject as assessed4_536_0_, this_.basisStandard as basisSta5_536_0_, this_.chapterNumber as chapterN6_536_0_, this_.checkListCode as checkLis7_536_0_, this_.checkListId as checkLis8_536_0_, this_.checkListProblemNo as checkLis9_536_0_, this_.checkListVersion as checkLi10_536_0_, this_.checkProject as checkPr11_536_0_, this_.correctiveMeasures as correct12_536_0_, this_.createDate as createDate536_0_, this_.createId as createId536_0_, this_.depId as depId536_0_, this_.depName as depName536_0_, this_.documents as documents536_0_, this_.fileCode as fileCode536_0_, this_.fileName as fileName536_0_, this_.fileSort as fileSort536_0_, this_.findItem as findItem536_0_, this_.flowState as flowState536_0_, this_.folderName as folderName536_0_, this_.lastUpdateDate as lastUpd24_536_0_, this_.lastUpdateId as lastUpd25_536_0_, this_.page as page536_0_, this_.problemPersonId as problem27_536_0_, this_.problemPersonName as problem28_536_0_, this_.proportionOfProblems as proport29_536_0_, this_.rank as rank536_0_, this_.remark as remark536_0_, this_.reportDate as reportDate536_0_, this_.stage as stage536_0_, this_.status as status536_0_, this_.technicalOrManagementIssues as technic35_536_0_, this_.versionCode as version36_536_0_ from NB_PROBLEM_REPORT this_ where this_.id NOT IN(select contentId from OA_COOP) 2025-07-23 15:25:40,538 - 500.jsp -2001219 [http-bio-8080-exec-5] ERROR 500.jsp - Unknown entity: null org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:693) org.hibernate.loader.criteria.CriteriaQueryTranslator.getPropertyMapping(CriteriaQueryTranslator.java:596) org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:483) org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:498) org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:503) org.hibernate.criterion.PropertyExpression.toSqlString(PropertyExpression.java:52) org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:380) org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:102) org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:72) org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:380) org.hibernate.loader.cr
最新发布
07-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值