Background Image
응용개발
2008.11.21 17:31

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

조회 수 19007 추천 수 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

  1. CUBRID 에서 제공하는 ODBC 함수 목록

    Date2008.11.21 Category응용개발 Byadmin Views26050
    Read More
  2. CUBRID 사용시 방화벽 설정

    Date2008.11.21 Category운영관리 Byadmin Views30980
    Read More
  3. SElinux 환경에서 CUBRID 사용하기

    Date2008.11.21 Category운영관리 Byadmin Views25505
    Read More
  4. libjvm.so 찾지 못하여 서버 구동이 되지 않은 경우

    Date2008.11.21 Category운영관리 Byadmin Views26440
    Read More
  5. Attempt to access a closed ResultSet(PreparedStatement) 오류

    Date2008.11.21 Category응용개발 Byadmin Views28027
    Read More
  6. loadjava 사용 시 inner class 로딩은 어떻게?

    Date2008.11.21 Category응용개발 Byadmin Views27449
    Read More
  7. 64bit OS 에서 사용가능한가요?

    Date2008.11.21 Category운영관리 Byadmin Views17415
    Read More
  8. 데이터베이스를 다른 머신으로 옮기는 방법

    Date2008.11.21 Category운영관리 Byadmin Views27649
    Read More
  9. HP-ux에서 jvm loading이 실패하는 경우 조치 사항

    Date2008.11.21 Category운영관리 Byadmin Views22432
    Read More
  10. tomcat 5.5 버전 이상에서 dbcp(커넥션 풀링) 설정법

    Date2008.11.21 Category응용개발 Byadmin Views28526
    Read More
  11. 데이터 필드의 타입 변경하기

    Date2008.11.21 Category질의작성 Byadmin Views18418
    Read More
  12. 예약어를 테이블명이나 컬럼명으로 사용시

    Date2008.11.21 Category질의작성 Byadmin Views37304
    Read More
  13. embedded sql 에서 char 사용시 주의 사항

    Date2008.11.21 Category응용개발 Byadmin Views19007
    Read More
  14. MySQL의 limit 명령어 처리

    Date2008.11.21 Category마이그레이션 Byadmin Views32682
    Read More
  15. 응용프로그램에서 질의 처리시 commit/rollback 처리 문제

    Date2008.11.21 Category응용개발 Byadmin Views21589
    Read More
  16. 백업파일을 이용하여 데이터베이스가 없는 다른 서버에서 복구하는 방법

    Date2008.11.21 Category운영관리 Byadmin Views22034
    Read More
  17. 중복된 데이터에 대하여 일련번호를 붙이는 방법

    Date2008.11.21 Category질의작성 Byadmin Views17359
    Read More
  18. 중복된 데이터를 1개만 남기고 지우는 방법

    Date2008.11.21 Category질의작성 Byadmin Views17305
    Read More
  19. select 결과중 몇개만 가져오는 방법

    Date2008.11.21 Category질의작성 Byadmin Views19900
    Read More
Board Pagination Prev 1 ... 5 6 7 8 9 10 11 12 13 14 Next
/ 14

Contact Cubrid

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