Background Image
응용개발
2008.11.21 17:31

embedded sql 에서 char 사용시 주의 사항

조회 수 19009 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
embedded sql 에서 host variable 을 사용할 경우 char type 에 대하여 where 절에 사용시 주의하여야 합니다.
비교하고자 하는 대상의 정확한 length(스키마상의 length)를 알아내고 host variable 상의 실제 데이터의 length(strlen) 이 비교대상의 스키마상의 length 보다 작은 경우 나머지는 공백으로 채워주어야만 정확한 비교 결과를 만들어 냅니다.
즉, name char(10) 일 경우 where name = :name 에서 strcpy(name, ‘1234’) 는 결과가 없지만 strcpy(name, ‘1234      ‘) 는 결과가 나올 수 있게 됩니다.

아래는 테스트를 위한 SQL 입니다.
create class t ( name char(10), addr varchar(10))
insert into t values(''''1234'''', ''''1234'''')
name                  addr
============================================
''''1234      ''''          ''''1234''''

아래는 예제 C source 입니다.
#include <stdio.h>

main()
{
        exec sql begin declare section;
                char    c[10];
                VARCHAR vc[10];
                char    rtn[10];
        exec sql end declare section;

        uci_startup("test");
        exec sql connect ''''demodb'''';

        strcpy(c, "1234");
        exec sql declare c0 cursor for
                select name from t where name = :c;
        exec sql open c0;
        exec sql fetch c0 into :rtn;
        printf("0. where char=(%s) : result count=%dn", c, SQLERRD[2]);
        exec sql close c0;

        strcpy(c, "1234      ");
        exec sql declare c1 cursor for
                select name from t where name = :c;
        exec sql open c1;
        exec sql fetch c1 into :rtn;
        printf("1. where char=(%s) : result count=%dn", c, SQLERRD[2]);
        exec sqlx close c1;

        strcpy(c, "12");
        exec sqlx declare c2 cursor for
                select name from t where substr(name,1,2) = :c;
        exec sqlx open c2;
        exec sqlx fetch c2 into :rtn;
        printf("2. where substr(char,1,2)=(%s) : result count=%dn", c, SQLERRD[2]);
        exec sql close c2;

        strcpy(c, "12        ");
        exec sql declare c3 cursor for
                select name from t where substr(name,1,2) = :c;
        exec sql open c3;
        exec sql fetch c3 into :rtn;
        printf("3. where substr(char,1,2)=(%s) : result count=%dn", c, SQLERRD[2]);
        exec sql close c3;

        strcpy(vc.array, "1234");
        exec sql declare c4 cursor for
                select name from t where addr = :vc;
        exec sql open c4;
        exec sql fetch c4 into :rtn;
        printf("4. (NO vc.length) where varchar=(%s) : result count=%dn", vc.array, SQLERRD[2]);
        exec sql close c4;

        strcpy(vc.array, "1234");
        vc.length = strlen(vc.array);
        exec sql declare c5 cursor for
                select name from t where addr = :vc;
        exec sql open c5;
        exec sql fetch c5 into :rtn;
        printf("5. where varchar=(%s) : result count=%dn", vc.array, SQLERRD[2]);
        exec sql close c5;

        strcpy(vc.array, "1234      ");
        vc.length = strlen(vc.array);
        exec sql declare c6 cursor for
                select name from t where addr = :vc;
        exec sql open c6;
        exec sql fetch c6 into :rtn;
        printf("6. where varchar=(%s) : result count=%dn", vc.array, SQLERRD[2]);
        exec sql close c6;

        strcpy(vc.array, "12");
        exec sql declare c7 cursor for
                select name from t where substr(addr,1,2) = :vc;
        exec sql open c7;
        exec sql fetch c7 into :rtn;
        printf("7. where substr(varchar,1,2)=(%s) : result count=%dn", vc.array, SQLERRD[2]);
        exec sql close c7;

        exec sql disconnect;
}


아래는 실행결과입니다.
0. where char=(1234) : result count=0
1. where char=(1234      ) : result count=1
2. where substr(char,1,2)=(12) : result count=0
3. where substr(char,1,2)=(12        ) : result count=1
4. (NO vc.length) where varchar=(1234) : result count=1
5. where varchar=(1234) : result count=1
6. where varchar=(1234      ) : result count=1
7. where substr(varchar,1,2)=(12) : result count=1

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
159 CUBRID 매니저 여러 버전의 CUBRID에 CUBRID Manager로 연결하는 방법 일동차렷? 2009.03.14 15355
158 질의작성 스키마에서 한글 사용하는 방법 file seongjoon 2009.04.14 15363
157 마이그레이션 CUBRID와 Oracle의 NULL과 '' (empty string)의 처리 차이점 1 권호일 2015.12.29 15441
156 운영관리 CUBRID Manager의 백업 자동화를 이용하여 요일 별로 백업을 하는 방법. file seongjoon 2009.12.16 15668
155 질의작성 CUBRID에서 응용프로그램 쿼리 binding 구문이 있을 경우 plan의 차이 cubebridge 2009.11.06 15701
154 운영관리 디비와 테이블 문자셋이 다른 경우 처리 방법 이용미 2015.06.03 15808
153 응용개발 jdbc에서 질의 플랜정보 보는 방법 손승일 2009.04.15 15843
152 운영관리 CUBRID제거 절차 janus 2009.12.12 15888
151 운영관리 CUBRID 2008 삭제시 제어판에서 삭제되지 않을시 해결법 file seongjoon 2009.04.13 15892
150 운영관리 CUBRID Database page크기 확인 방법 janus 2009.12.31 16018
149 기타 CUBRID의OLE DB 드라이버 설치 방법. file seongjoon 2009.08.24 16263
148 CUBRID 매니저 CUBRID에서 OID 확인하는 방법 file seongjoon 2010.01.02 16308
147 운영관리 CUBRID 쿼리 수행 중단 시 주의 사항 cubebridge 2010.03.04 16429
146 운영관리 Window서버 cmd창에서 CUBRID 구동 후 cmd창을 닫을 시 DB서비스 중단 조치방법 cubebridge 2009.10.29 16465
145 질의작성 CUBRID설치 후 default설정 상태에서 Query Plan보기(CSQL모드) cubebridge 2009.12.31 16507
144 CUBRID 매니저 테이블 명세서 한 시트로 합치기 file 성진 2015.12.24 16535
143 응용개발 PHP에 CUBRID 모듈 추가시 모듈이 로드되지 않는 문제에 대한 해결 방안 하나입니다 남재우 2010.07.01 16557
142 운영관리 브로커 상태를 확인할 때 status 의미 시난 2009.11.27 16562
141 응용개발 오라클 to CUBRID로 마이그레이션 수행 시 주의사항 cubebridge 2012.11.12 16644
140 기타 windows vista 환경의 csql에서 ;edit 실행시 오류발생하는 경우 file 손승일 2009.05.27 16686
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 11 ... 14 Next
/ 14

Contact Cubrid

대표전화 070-4077-2110 / 기술문의 070-4077-2113 / 영업문의 070-4077-2112 / Email. contact_at_cubrid.com
Contact Sales