日期:2014-05-16  浏览次数:20453 次

Ibatis insert语句中包含子查询问题

insert语句中包含子查询问题

Ibatis中的Insert语句,将values采用select代替

<insert id="insertFaqContinue">
        INSERT INTO FAQ ( 
        <dynamic prepend="">
            <isNotNull prepend="," property="topicID"> topicID </isNotNull>
            <isNotNull prepend="," property="listID"> listID </isNotNull>
            <isNotNull prepend="," property="topicID"> faqfloor </isNotNull>
        </dynamic>
        )
        SELECT 
        <dynamic prepend="">
            <isNotNull prepend="," property="topicID"> #topicID# </isNotNull>
            <isNotNull prepend="," property="listID"> #listID# </isNotNull>
            <isNotNull prepend="," property="title"> #title# </isNotNull>
            <isNotNull prepend="," property="topicID"> (SELECT count(faqID)+1 FROM FAQ WHERE topicID = #topicID#) </isNotNull>
        </dynamic>
        
        <selectKey resultClass="int" keyProperty="faqID" >
            SELECT @@IDENTITY AS faqID
        </selectKey>
    </insert>


分析:整体结构为Insert into select 语句,select 语句中包含子查询

 

<insert id="insertSubjectRelDemand" parameterClass="subjectreldemand">
    insert into dsp_subject_reldemand (no,subject_no,demand_no,version)
     select dsp_subject_reldemand_seq.nextval,#subject_no#,#demand_no#,(select version from dsp_subject where no = #subject_no#) from dual
 </insert>