Skip to Main Content

Berkeley DB Family

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

comparison function returns incorrect data

3333809Oct 21 2016 — edited Nov 1 2016

i have modified the examples/c/ex_heap.c to read from one db and insert/delete in another.  For some reason one of the keys that the comparison function receives is either truncated or doesn't match any records in the db.  This is my comparison function

int compare_cmid(dbp, a, b, lob)

        DB *dbp;

        const DBT *a, *b;

        size_t *lob;

{

       //char* temp_a = (char*)a->data;

       //char* temp_b = (char*)b->data;

       //printf("temp_a = %s\n", temp_a);

       //printf("temp_b = %s\n", temp_b);

        char key_a[25];

        char key_b[25];

        memset(key_a,'\0',sizeof(key_a));

        memset(key_b,'\0',sizeof(key_b));

       int ascii_a[256];

       int ascii_b[256];

       int i;

       memset(ascii_a, 0, sizeof(ascii_a));

       memset(ascii_b, 0, sizeof(ascii_b));

       strcpy(key_a, a->data);

       strcpy(key_b, b->data);

       printf("key_a = %s\n", key_a);

       printf("key_b = %s\n", key_b);

       int l_a;

       l_a =  strlen(key_a);

       int l_b;

       l_b = strlen(key_b);

       int value;

       for (i=0; i<l_a; i++) {

         value = key_a[i];

         ascii_a[value]++;

       }

       for(i=0; i<l_b; i++) {

         value = key_b[i];

         ascii_b[value]++;

        }

       for(i=0; i<256; i++) {

         if(ascii_a[i] > ascii_b[i]) {

             return 1;

         } else if (ascii_a[i] < ascii_b[i]) {

             return -1;

         }

       }

       return 0;

}

b is often messed up.  I'm setting the comparison function before opening the db and it gets called fine but just not with the expected keys.  Please assist.

This post has been answered by userBDBDMS-Oracle on Oct 21 2016
Jump to Answer

Comments

BluShadow
The majority of those commands are SQL*Plus commands. That means that they are specific to SQL*Plus and are not part of SQL or PL/SQL which is what you write your procedures/packages with.

Tell us more about what you are trying to achieve and we can advise the best way, because this may be that you put everything in a procedure and code the requirements to generate output or you put everything in a script and execute it via a job etc.
221158
You can include the select statement in a cursor within a database procedure and use dynamic SQL (execute immediate to execute the DDL).

Christopher Soza
Oracle BI DBA
Orix Consultancy Services Ltd
b: http://sozaman.blogspot.com
519811
The system setup is that from Oracle 10g database, data will be copied to locally deployed Oracle XE databases. In order to do that, tables in Oracle XE should be truncated. But before truncating the table, constraints should be disabled first.Then after inserting records from Oracle 10g, constraints shall be enabled again.

I hope you could help me with this one. Thanks in advance.
Marwim
Hello sidkaterin,

here is a package I use for development. Never give access to this package to a normal user!

Regards
Marcus
CREATE OR REPLACE PACKAGE  my_trunc
IS
PROCEDURE disable_triggers;
PROCEDURE enable_triggers;
PROCEDURE disable_constraints;
PROCEDURE enable_constraints;
PROCEDURE truncate_table(p_tabelle IN VARCHAR2);

END my_trunc;
/

CREATE OR REPLACE PACKAGE BODY my_trunc
IS

PROCEDURE disable_triggers
IS
    cur_name                INTEGER;
    rows_processed          INTEGER;
BEGIN
    cur_name := dbms_sql.open_cursor;
    FOR c_trg IN(
        SELECT  'ALTER TRIGGER ' || trigger_name || ' DISABLE ' stmnt
        FROM    user_triggers
        )
    LOOP
        dbms_sql.parse (
                 cur_name
                ,c_trg.stmnt
                ,dbms_sql.v7
                );
        rows_processed := dbms_sql.execute (cur_name);
    END LOOP;
    dbms_sql.close_cursor (cur_name);
EXCEPTION
    WHEN OTHERS THEN
        dbms_sql.close_cursor (cur_name);
        RAISE_APPLICATION_ERROR (
                 -20999
                ,'Disable Trigger: '||SUBSTR(SQLERRM, 1, 100));
END disable_triggers;

PROCEDURE enable_triggers
IS
    cur_name                INTEGER;
    rows_processed          INTEGER;
BEGIN
    cur_name := dbms_sql.open_cursor;
    FOR c_trg IN (
        SELECT  'ALTER TRIGGER ' || trigger_name || ' ENABLE ' stmnt
        FROM    user_triggers
        )
    LOOP
        dbms_sql.parse (
                 cur_name
                ,c_trg.stmnt
                ,dbms_sql.v7
                );
        rows_processed := dbms_sql.execute (cur_name);
    END LOOP;
    dbms_sql.close_cursor (cur_name);
EXCEPTION
    WHEN OTHERS THEN
        dbms_sql.close_cursor (cur_name);
        RAISE_APPLICATION_ERROR (
                 -20999
                ,'Enable Trigger: '||SUBSTR(SQLERRM, 1, 100)
                );
END enable_triggers;

PROCEDURE disable_constraints
IS
    cur_name                INTEGER;
    rows_processed          INTEGER;
BEGIN

    cur_name := dbms_sql.open_cursor;
    FOR c_con IN (
        SELECT  'ALTER TABLE ' || table_name || ' DISABLE CONSTRAINT ' || constraint_name || ' ' stmnt
        FROM    user_constraints
        WHERE   constraint_type = 'R'
        )
    LOOP
        dbms_sql.parse (
                 cur_name
                ,c_con.stmnt
                ,dbms_sql.v7
                );
        rows_processed := dbms_sql.execute (cur_name);
    END LOOP;
    dbms_sql.close_cursor (cur_name);

    cur_name := dbms_sql.open_cursor;
    FOR c_con IN (
        SELECT  'ALTER TABLE ' || table_name || ' DISABLE CONSTRAINT ' || constraint_name || ' ' stmnt
        FROM    user_constraints
        WHERE   constraint_type != 'R'
        )
    LOOP
        dbms_sql.parse (
                 cur_name
                ,c_con.stmnt
                ,dbms_sql.v7
                );
        rows_processed := dbms_sql.execute (cur_name);
    END LOOP;
    dbms_sql.close_cursor (cur_name);

EXCEPTION
    WHEN OTHERS THEN
        dbms_sql.close_cursor (cur_name);
        RAISE_APPLICATION_ERROR (
                 -20999
                ,'Disable Constraints: '||
                 SUBSTR(SQLERRM, 1, 100)
                );
END disable_constraints;

PROCEDURE enable_constraints
IS
    cur_name                INTEGER;
    rows_processed          INTEGER;
BEGIN

    cur_name := dbms_sql.open_cursor;
    FOR c_con IN (
        SELECT  'ALTER TABLE ' || table_name || ' ENABLE CONSTRAINT ' || constraint_name || ' ' stmnt
        FROM    user_constraints
        WHERE   constraint_type != 'R'
        )
    LOOP
        dbms_sql.parse (
                 cur_name
                ,c_con.stmnt
                ,dbms_sql.v7
                );
        rows_processed := dbms_sql.execute (cur_name);
    END LOOP;
    dbms_sql.close_cursor (cur_name);

    cur_name := dbms_sql.open_cursor;
    FOR c_con IN (
        SELECT  'ALTER TABLE ' || table_name || ' ENABLE CONSTRAINT ' || constraint_name || ' ' stmnt
        FROM    user_constraints
        WHERE   constraint_type = 'R'
        )
    LOOP
        dbms_sql.parse (
                 cur_name
                ,c_con.stmnt
                ,dbms_sql.v7
                );
        rows_processed := dbms_sql.execute (cur_name);
    END LOOP;
    dbms_sql.close_cursor (cur_name);

EXCEPTION
    WHEN OTHERS THEN
        dbms_sql.close_cursor (cur_name);
        RAISE_APPLICATION_ERROR (
                 -20999
                ,'Enable Constraints: '||
                 SUBSTR(SQLERRM, 1, 100)
                );
END enable_constraints;

PROCEDURE truncate_table
    (
     p_tabelle              IN VARCHAR2
    )
IS
    cur_name                INTEGER;
    v_statement             VARCHAR2(500);
    rows_processed          INTEGER;
BEGIN
    v_statement     := 'TRUNCATE TABLE ' || p_tabelle || ' ';
    cur_name        := dbms_sql.open_cursor;
    dbms_sql.parse (
             cur_name
           ,v_statement
            ,dbms_sql.v7
            );
    rows_processed  := dbms_sql.execute (cur_name);
    dbms_sql.close_cursor (cur_name);
EXCEPTION
    WHEN OTHERS THEN
        dbms_sql.close_cursor (cur_name);
        RAISE_APPLICATION_ERROR (
                 -20999
                ,'Truncate Table ' || p_tabelle || ': ' ||
                SUBSTR(SQLERRM, 1, 100)
                );
END truncate_table;

END my_trunc;
/
SHO ERR
Edited by: Marwim on 18.09.2008 07:33
Replaced lessThan greaterThan with != because using code-markup does not hinder the forum software from changing the characters.
519811
Thank you. I can review this and maybe would revise a few statements to fit my requirements.
247514
sidkaterin wrote:
set feedback off
set verify off
set echo off
prompt Finding constraints to disable...
set termout off
set pages 80
set heading off
set linesize 120
spool tmp_disable.sql;
select 'spool igen_disable.log;' from dual;
select 'ALTER TABLE '||substr(c.table_name,1,35)||
' DISABLE CONSTRAINT '||constraint_name||' ;'
from user_constraints c, user_tables u
where c.table_name = u.table_name;
--and c.constraint_type IN ('P','R','U');
select 'exit;' from dual;
set termout on
prompt Disabling constraints now...
set termout off
@tmp_disable.sql;
Or run your procedure here.
exec myprocedure ;
exit
/

however, there are other procedures that shall be done after disabling the constraints. I want to put them in one package. How do I call this from a procedure? When I tried to simply enclose it in a PROCEDURE format, the spool file command is not recognized. It returned error message. Hope anyone can help me with this. Thanks in advance.
1 - 6
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 29 2016
Added on Oct 21 2016
23 comments
1,113 views