본문 바로가기

Backend/DB 실무

[Mybatis] The column index is out of range & COALESCE(A, B)

반응형

MybatisError

org.postgresql.util.PSQLException: The column index is out of range: 2, number of columns: 1.

 

주로 오타 때문에 column갯수가 안맞게 인식되는경우에 발생한다고 한다.

이번 케이스는 주석을 <!-- -->를 사용해야 하는데 /* */를 사용해서 발생했다.

 

COALESCE(A,B)

A,B를 비교해서 Null이 아닌 값을 유지하면서 둘다 NotNull이라면 A의 값을 반환

A B COALESCE(A,B)
1 2 1
Null 3 3
4 1 4
Null Null Null

 

<foreach collection="list" item="item" index="index" separator=";">
	INSERT INTO public.data
	(
		data_seq
		, var_version
	)
	VALUES
	(
		#{item.dataSeq}
		<!--, COALESCE((SELECT max(var_version) + 1 이렇게 하면 list 입력시 마다 커짐-->
        <!--, COALESCE((SELECT max(var_version) + 1 - #{index} 이렇게 하면 마지막 list에서 값이 작아짐-->
        , COALESCE((SELECT max(var_version)
			FROM cdp_data_variable
			WHERE group_seq = #{item.groupSeq}), 1))
</foreach>

 

 

반응형