How to create a table and populate data by using .txt file .

Code:
DEFINE VARIABLE lcInfo       as character no-undo.
DEFINE VARIABLE lcTableName  AS CHARACTER NO-UNDO.
DEFINE variable htt          as handle    no-undo.
DEFINE variable hBuf_TT_Empl as handle.

input from "C:\Users\rrebari\Desktop\input3.txt".

IMPORT UNFORMATTED lcInfo.
IF TRIM(lcInfo) > "" THEN
DO:
    ASSIGN
        lcTableName = TRIM(lcInfo).
    CREATE TEMP-TABLE htt.
END.

IF lcTableName > "" THEN
DO:
    repeat:
        import unformatted lcInfo.
        IF TRIM(lcInfo) > "" THEN
        DO:
            htt:add-new-field ( TRIM(lcInfo), "character").
        END.
    end.
/*DISP lcInfo.
*/
END.                        
htt:temp-table-prepare(lcTableName).
hBuf_TT_Empl = htt:default-buffer-handle.

do transaction:
    hBuf_TT_Empl:buffer-create().
    hBuf_TT_Empl:buffer-field ("Ename"):buffer-value= "Ramesh".
    hBuf_TT_Empl:BUFFER-FIELD ("Eid"):buffer-value= "123".
    hBuf_TT_Empl:BUFFER-FIELD ("Ecity"):buffer-value= "Jaipur".
end.
DEFINE VARIABLE qh4  AS HANDLE.
DEFINE VARIABLE i    AS INTEGER NO-UNDO.
DEFINE VARIABLE fldh AS HANDLE  EXTENT 3.

/* Create Query */
CREATE QUERY qh4.
qh4:SET-BUFFERS(hBuf_TT_Empl).
qh4:QUERY-PREPARE("for each abc").
qh4:QUERY-OPEN.

REPEAT WITH FRAME zz DOWN:
    qh4:GET-NEXT.
    IF qh4:QUERY-OFF-END THEN LEAVE.
    REPEAT i = 1 TO 3:
        fldh = hBuf_TT_Empl:BUFFER-FIELD(i).
        DISPLAY fldh:NAME FORMAT "x(15)"
            fldh:BUFFER-VALUE FORMAT "x(20)".
    END.
END.

hBuf_TT_Empl:BUFFER-RELEASE.
DELETE OBJECT qh4.
input close.
 

Attachments

  • 15_fetch_table.p
    1.5 KB · Views: 6
Last edited by a moderator:

TomBascom

Curmudgeon
If there is a question buried somewhere in that post I can't find it.

If you are posting your code because it is a wonderful example of "How to create..." that's nice but [ C O D E ] tags are a beautiful thing that go a long ways towards making a code focused post readable.
 

ForEachInvoiceDelete

Active Member
I have double checked.

I cannot find a question.

But heres how to create a temp-table from a text file in less lines.

Code:
DEFINE TEMP-TABLE ttSomeTable
FIELD SomeField      AS CHAR
FIELD SomeOtherField AS CHAR.

INPUT FROM VALUE("Link to my desktop where i store all my .txt files").

REPEAT:

    CREATE ttSomeTable.

    IMPORT DELIMITER "," ttSomeTable.

END.
 
I have double checked.

I cannot find a question.

But heres how to create a temp-table from a text file in less lines.

Code:
DEFINE TEMP-TABLE ttSomeTable
FIELD SomeField      AS CHAR
FIELD SomeOtherField AS CHAR.

INPUT FROM VALUE("Link to my desktop where i store all my .txt files").

REPEAT:

    CREATE ttSomeTable.

    IMPORT DELIMITER "," ttSomeTable.

END.
I am happy with you but in this My temp table name also coming from text file ,all fields also coming form text file ,you only assign the values to the fields.
 

ForEachInvoiceDelete

Active Member
Can i ask why?

It looks like a mess of code that isn't really doing anything useful.

You're hard coding the table name in your query-prep. And hard coding all your fields/data inside the code block anyway. (Where you're assigning buffer-value)

Just like completely removing any benefit of writing dynamic code lol

Something like this would be more suitable. We store field headers/data types in a database table so if someone wants an additional field etc added to an upload script we dont have to do any code changes.

Code:
    INPUT FROM VALUE(chrPath + chrFileName).
    
    REPEAT:
        
        IMPORT UNFORMATTED chrRecord.
                
        bpbttHandle:BUFFER-CREATE.
        
        REPEAT intCounter = 1 TO NUM-ENTRIES(chrRecord, CHR(254)) 
               ON ERROR UNDO, RETURN ERROR:
            
            ASSIGN hdlFieldBuffer              = bpbttHandle:BUFFER-FIELD(intCounter)
                   hdlFieldBuffer:BUFFER-VALUE = ENTRY(intCounter, chrRecord, CHR(254)).
        
        END.
        
    END.
 
My intention is not heart you my thread is for Questions : -create a text file which contains <tablename>,<fieldslist>. Write an ABL program which reads the text file and
creates a table and fields with the names given in the text file and populate the data and display them.
Thank you

Ramesh Chand Rebari
 

Cecil

19+ years progress programming and still learning.
My 2 Cents:

Guys, he just showing us a plausible method of being able to dynamically import a CVS file into a Temp-table where the developer does not know the field/column ordering of the file (It could happen).

Of cause, the other issue with dynamically creating a temp table from an external source is the fact the column name could begin with a non-alphabetical character or contain spaces i.e. 1_Street Address, 2_Street Address, 3_Street Address.

Need to sanitize the column names before using htt:add-new-field ( TRIM(lcInfo), "character").
 
My 2 Cents:

Guys, he just showing us a plausible method of being able to dynamically import a CVS file into a Temp-table where the developer does not know the field/column ordering of the file (It could happen).

Of cause, the other issue with dynamically creating a temp table from an external source is the fact the column name could begin with a non-alphabetical character or contain spaces i.e. 1_Street Address, 2_Street Address, 3_Street Address.

Need to sanitize the column names before using htt:add-new-field ( TRIM(lcInfo), "character").


exactly you catch my point
 
Top