본문 바로가기

개발/DATABASE

[SQL] 동적 쿼리 제어

<choose>

 

1. 조건과 조건에 맞지 않을 경우 동적 제어

<choose>
    <when test="type != '' and type != NULL">
    -- 조건에 해당하면
    </when>
    <otherwise>
    -- 그렇지 않으면
    </otherwise>
</choose>

 

2. 조건이 무엇인지에 따라 동적 제어

<choose>
    <when test="type.equals('day') ">
    -- type이 day일 경우
    </when>
    <when test="type.equals('month') ">
    -- type이 month일 경우
    </when>
</choose>

 

3. AND(A OR B)

<choose>
    <when test='type1 == "N"'>
    	AND (TYPE_A IS NULL
    </when>                    
    <when test='type1 != null and type1 != "" and type1 !="N"'>
    	AND (TYPE_A = #{type1}
    </when>
    <otherwise>
    	-- 항상 참 (열기 위해 사용)
    	AND ( 1=1 
    </otherwise>
</choose>

<choose>
    <when test='type2 == "N"'>
    	AND (TYPE_B IS NULL
    </when>                    
    <when test='type2 != null and type2 != "" and type2 !="N"'>
    	AND (TYPE_B = #{type2}
    </when>
    <otherwise>
    	-- 항상 참 (닫기 위해 사용)
    	OR 1=1 )
    </otherwise>
</choose>