Creating new table with same structure as existing one

Ask a Question related to Informix, Design and Development.

  1. #1

    Default Creating new table with same structure as existing one

    I need to create "archive" table that will have the same structure as
    "production" one.

    When I run my program it have to create table named "pronop_yyyymm" ( yyyymm is
    argument to my program representing date in format year(4)month(2)) if it
    doesn't exist. I will write rows from table "pronop" that are in given
    month/year to "pronop_yyyymm" and delete them from "pronop" (I know that DELETE
    is very slow but I don't have alternative).
    What I want is not to change program (or part of program where I create
    "pronop_yyyymm") when structure of table "pronop" is changed.
    I am using I4GL. I have an idea to do it with "dbschema" and some scripting.

    Is there any other way?

    nebojsa
    ------------------------------------
    Remove spam block (DELETE_) to reply
    Nebojsa Sevo Guest

  2. Similar Questions and Discussions

    1. Copy a table in Access structure only with CF query
      Hello, I need to know how to create a new table by copying an existing table with the table structure only in a query Don't know the Syntax but...
    2. Query w/bad table structure
      Hi, I am trying to query a column that has multiple values. When I export it, I need to have it so that each of those values is placed in its own...
    3. Creating A New Table In Existing Access Database
      Hi, I am trying to create a new table in an existing database and having some issues. When I use the attached code, it gives me an: Error...
    4. Creating Dynamic Structure
      Hi, I want to create a dynamic structure of structures. I wrote the following code but I am getting errors with that. I have three entities...
    5. query to return table structure
      hi, Is there any command or query that returns the table structure informix.
  3. #2

    Default RE: Creating new table with same structure as existing one


    How about altering the table to fragment it, putting the yyyymm rows into a
    separate fragment, and then detaching that fragment into the new table? The
    effectiveness of this approach probably depends on how big the original
    table is.

    HTH,
    Paul Mosser

    -----Original Message-----
    From: Nebojsa Sevo [mailto:DELETE_mips@zg.tel.hr]
    Sent: Tuesday, October 14, 2003 2:59 AM
    To: [email]informix-list@iiug.org[/email]
    Subject: Creating new table with same structure as existing one


    I need to create "archive" table that will have the same structure as
    "production" one.

    When I run my program it have to create table named "pronop_yyyymm" ( yyyymm
    is
    argument to my program representing date in format year(4)month(2)) if it
    doesn't exist. I will write rows from table "pronop" that are in given
    month/year to "pronop_yyyymm" and delete them from "pronop" (I know that
    DELETE
    is very slow but I don't have alternative).
    What I want is not to change program (or part of program where I create
    "pronop_yyyymm") when structure of table "pronop" is changed.
    I am using I4GL. I have an idea to do it with "dbschema" and some scripting.

    Is there any other way?

    nebojsa
    ------------------------------------
    Remove spam block (DELETE_) to reply
    sending to informix-list
    mosserp@wellsfargo.com Guest

  4. #3

    Default Re: Creating new table with same structure as existing one


    I have a small ec program, which will return the create table statement.

    It is written for data types mentioned in array . Call this from
    4gl with params (tabname,length(tabname)) returning l_create_stmt.

    This is bit crude, but u can refine it.



    ================================================== ===========
    #include<stdlib.h>

    #include<stdio.h>

    #include<sqltypes.h>

    #include<ifxtypes.h>

    #include<math.h>

    #include<datetime.h>



    char *myvals[24] = { " char\("," smallint"," int","",""," decimal\(","
    serial","
    date","",""," datetime","","","
    varchar\(","","","","","","","","","",""};
    char *dtimes[16] =
    {"YEAR","","MONTH","","DAY","","HOUR","","MINUTE", "","SECOND"
    ,"fraction\(1\)","fraction\(2\)","fraction\(3\)"," fraction\(4\)","fraction\(5\)"
    };

    int tabsel(int
    nargs)
    {

    exec sql begin declare
    section;
    char
    l_colname[50];
    int
    l_coltype;
    int
    l_colno;
    int
    l_collength;
    char
    l_mystmt[300];
    exec sql end declare
    section;
    int
    tmp;
    int
    tmp1;
    char
    tabname[50];
    int
    len;
    int
    i;
    char
    l_stmt[300];
    char
    selstmt[1000];

    popint(&len);
    popquote(tabname,(len + 1
    ));
    sprintf(l_mystmt,"select trim(colname),coltype,colno,collength
    from sys
    columns s,systables t where s.tabid=t.tabid and t.tabname= \"%s\" order
    by colno
    ",tabname);

    /*exec sql database
    euro50k;*/
    exec sql prepare prep_my from :l_mystmt
    ;
    if (sqlca.sqlcode != 0
    )

    {

    retquote("");

    retint(sqlca.sqlcode);

    return(2);

    }
    exec sql declare d_cur cursor for
    prep_my;
    if (sqlca.sqlcode != 0
    )

    {

    retquote("");

    retint(sqlca.sqlcode);

    return(2);

    }
    exec sql open
    d_cur;
    if (sqlca.sqlcode != 0
    )

    {

    retquote("");

    retint(sqlca.sqlcode);

    return(2);

    }
    sprintf(selstmt,"create table %s
    \(",tabname);
    while (sqlca.sqlcode ==
    0){


    exec sql fetch next d_cur into
    :l_colname,:l_coltype,:l_colno,:l
    _collength;

    if ( sqlca.sqlcode == 100
    )

    break;


    while ( l_coltype >= 256
    ){
    l_coltype = l_coltype -
    256;

    }
    if ( (2<l_coltype && l_coltype <5) || (7<l_coltype &&
    l_coltype<
    10) || (10< l_coltype && l_coltype<12 ) || (l_coltype >13)
    ){

    retquote("");

    retint(2);

    return(2);

    }



    for(i=0;i<strlen(l_colname);i++)

    {
    if ( l_colname[i]=='
    ')

    {

    l_colname[i]='\0';

    break;

    }

    }

    strcat(selstmt,l_colname);

    strcat(selstmt,myvals[l_coltype]);


    switch
    (l_coltype){
    case 0:
    sprintf(l_stmt,"%d\)\,",l_collength);

    break;
    case 1:
    sprintf(l_stmt,"\,");

    break;
    case 2:
    sprintf(l_stmt,"\,");

    break;
    case 5:
    tmp=l_collength/256;
    tmp1= l_collength -
    tmp*256;

    sprintf(l_stmt,"%d\,%d\)\,",tmp,tmp1);

    break;
    case 6:
    sprintf(l_stmt,"\,");

    break;
    case 7:
    sprintf(l_stmt,"\,");

    break;
    case 10: sprintf(l_stmt," %s to %s
    \,",dtimes[TU_START(l
    _collength)],dtimes[TU_END(l_collength)]);


    break;
    case 13:
    tmp=l_collength/256;

    tmp1=remainder(l_collength,256);

    sprintf(l_stmt,"%d\,%d\)\,",tmp1,tmp);

    break;

    }

    strcat(selstmt,l_stmt);

    }
    selstmt[strlen(selstmt) - 1] = ')';
    retquote(selstmt);
    retint(0);
    return(2);
    }
    ================================================== ========

    Hope this helps

    Rgds
    Preetinder


    Nebojsa Sevo wrote:
    >I need to create "archive" table that will have the same structure as
    >"production" one.
    >
    >When I run my program it have to create table named "pronop_yyyymm" ( yyyymm is
    >argument to my program representing date in format year(4)month(2)) if it
    >doesn't exist. I will write rows from table "pronop" that are in given
    >month/year to "pronop_yyyymm" and delete them from "pronop" (I know that DELETE
    >is very slow but I don't have alternative).
    >What I want is not to change program (or part of program where I create
    >"pronop_yyyymm") when structure of table "pronop" is changed.
    >I am using I4GL. I have an idea to do it with "dbschema" and some scripting.
    >
    >Is there any other way?
    >
    >nebojsa
    >------------------------------------
    >Remove spam block (DELETE_) to reply
    >
    >
    >
    sending to informix-list
    preetinder dhaliwal Guest

  5. #4

    Default Re: Creating new table with same structure as existing one

    On Tue, 14 Oct 2003 05:59:27 -0400, Nebojsa Sevo wrote:

    Actually you do have an alternative. Create a second fragment of the table
    containing the rows you want to archive using ALTER FRAGMENT ON TABLE pronop
    INIT FRAGMENT BY EXPRESSION (datecolumn < "2002-02" IN dbspace_1, REMAINDER IN
    current_dbspace;. Then detach that fragment to an independent table using ALTER
    FRAGMENT ON TABLE pronop DETACH dbspace_1 pronop_200202.


    Art S. Kagel
    > I need to create "archive" table that will have the same structure as
    > "production" one.
    >
    > When I run my program it have to create table named "pronop_yyyymm" ( yyyymm
    > is argument to my program representing date in format year(4)month(2)) if it
    > doesn't exist. I will write rows from table "pronop" that are in given
    > month/year to "pronop_yyyymm" and delete them from "pronop" (I know that
    > DELETE is very slow but I don't have alternative). What I want is not to
    > change program (or part of program where I create "pronop_yyyymm") when
    > structure of table "pronop" is changed. I am using I4GL. I have an idea to do
    > it with "dbschema" and some scripting.
    >
    > Is there any other way?
    >
    > nebojsa
    > ------------------------------------
    > Remove spam block (DELETE_) to reply
    Art S. Kagel Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139