How can I change n-rows --> 1-row ? Getting constraint information.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tensi4u
    New Member
    • Nov 2007
    • 11

    How can I change n-rows --> 1-row ? Getting constraint information.

    Hi all,
    I've tried to get primary and unique constraint information via syscat.tabconst and syscat.keycolus e tables as one value for each constraint.
    For example, let's there is table called tblTest.
    tblTest ( Id, col1, col2, col3, col4 )
    - PK (P_PK_tblTest) : Id
    - UK (U_UK_tblTest) : col2, col3

    If I join two tables, syscat.tabconst (TC) and syscat.keycolus e (KC), it will show
    TC.constname TC.type KC.colnames
    -------------- -------- -------------
    P_PK_tblTest P Id
    U_UK_tblTest U col2
    U_UK_tblTest U col3

    But I'd like get them like below such as we can get columns information from syscat.indexes
    TC.constname TC.type KC.colnames
    -------------- -------- -------------
    P_PK_tblTest P +Id
    U_UK_tblTest U +col2+col3

    So I've tried to solve this using function but it's complaining something.
    How can I get the result like above (+col2+col3) ?

    In addition, I've posted the script that I tried to make function and error message. It would return only one result.

    create function getUPConstraint ( p_schema varchar(50), p_tableName varchar(50), p_type varchar(1) ) returns varchar(200)

    language sql

    begin atomic

    declare v_result varchar(200);
    declare v_columns varchar(50);

    declare c1 cursor with return for
    SELECT kc.colname
    FROM syscat.tabconst tc, syscat.keycolus e kc
    WHERE tc.tabschema = kc.tabschema
    AND tc.tabname = kc.tabname
    AND tc.constname = kc. constname
    and tc.tabschema = p_schema
    and tc.tabname = p_tableName
    AND tc.type = p_type
    order by kc.colseq;

    declare exit handler for not found
    set v_result = '';

    begin
    open c1;
    fetch_loop:
    loop
    fetch c1 into v_columns
    set v_result = v_result + '+' + v_columns;
    end loop fetch_loop;

    close c1;
    end;

    return v_result ;
    end@

    DB21034E The command was processed as an SQL statement because it was not a
    valid Command Line Processor command. During SQL processing it returned:
    SQL0104N An unexpected token "cursor with return for SELEC" was
    found following " declare c1". Expected tokens may include:
    "<space>". LINE NUMBER=11. SQLSTATE=42601
  • tensi4u
    New Member
    • Nov 2007
    • 11

    #2
    Actually I've solved this problem with the makeshift. I've posted what I had done. But I've been in stuck with creating function issue. Thank you.

    Code:
    select tblColSpan.constname, '+' || max(tblColSpan.col1) || '+' || max(tblColSpan.col2)
    from (
    	select tblInner.constname, ( case tblInner.colseq when 1 then tblInner.colname else '' end) col1, ( case tblInner.colseq when 2 then tblInner.colname else '' end) col2, ( case tblInner.colseq when 3 then tblInner.colname else '' end) col3 
    	from ( SELECT constname, colname, colseq FROM syscat.keycoluse WHERE tabschema = <schema name> AND tabname = <table name> and constname in ( <constraints name> {, <constraint name>}) order by constname, colseq ) tblInner
    ) tblColSpan
    group by tblColSpan.constname.
    Last edited by docdiesel; Jun 24 '08, 12:25 PM. Reason: Added code tags

    Comment

    Working...