Duke578
Duke578

Reputation: 81

A program that outputs a report, as a CSV

Ho do I write code for a program that can accept three input parameters: x , y, and the filename to write to?

I should be able to call the program like this: run prog.p (input “1”, input 5, input “filename1.csv”).

so far my I have written the code below and not sure how to go around it.

OUTPUT TO xxxxxx\filename1.csv". 

DEFINE VARIABLE Profit AS DECIMAL  FORMAT "->>,>>9.99":U INITIAL 0 NO-UNDO.

EXPORT DELIMITER "," "Amount" "Customer Number" "Invoice Date" "Invoice Number" "Total_Paid" "Profit".

FOR EACH Invoice  WHERE Invoice.Ship-charge > 5.00
                  AND   Invoice.Total-Paid > 0.01
                  AND   Invoice.Invoice-Date GE 01/31/93 /* this is between  also can use < >*/
                  AND   Invoice.Invoice-Date LE TODAY NO-LOCK:

    Profit = (Invoice.Invoice-Num / Invoice.Total-Paid) * 100.

EXPORT DELIMITER "," Amount Cust-Num Invoice-Date Invoice-Num  Total-Paid Profit. 
END.

OUTPUT CLOSE.

Thank you.

Upvotes: 0

Views: 116

Answers (1)

Jensd
Jensd

Reputation: 8011

You're on the right track! OUTPUT TO VALUE(variable) is what might help you. Also you should possibly use a named stream.

It's not clear to me what parameters x and y should do so I just inserted them as dummies below.

Note: You're commenting about using <> instead of GE. That might work logically but could (will) effect your performance by forcing the database to scan entires tables instead of using an index.

Something like this:

DEFINE INPUT  PARAMETER pcX    AS CHARACTER NO-UNDO.
DEFINE INPUT  PARAMETER piY    AS INTEGER   NO-UNDO.
DEFINE INPUT  PARAMETER pcFile AS CHARACTER NO-UNDO.

/* Bogus temp-table to make the program run... */
/* Remove this unless just testing without database ...*/
DEFINE TEMP-TABLE Invoice NO-UNDO 
    FIELD Ship-Charge  AS DECIMAL 
    FIELD Total-Paid   AS DECIMAL 
    FIELD Invoice-Date AS DATE
    FIELD Invoice-Num  AS INTEGER
    FIELD Amount       AS INTEGER 
    FIELD Cust-Num     AS INTEGER.

DEFINE STREAM str.

DEFINE VARIABLE Profit AS DECIMAL  FORMAT "->>,>>9.99":U INITIAL 0 NO-UNDO.

OUTPUT STREAM str TO VALUE(pcFile). 

EXPORT STREAM str DELIMITER "," "Amount" "Customer Number" "Invoice Date" "Invoice Number" "Total_Paid" "Profit".
FOR EACH Invoice  WHERE Invoice.Ship-charge > 5.00
                  AND   Invoice.Total-Paid > 0.01
                  AND   Invoice.Invoice-Date GE 01/31/93 /* this is between  also can use < >*/
                  AND   Invoice.Invoice-Date LE TODAY NO-LOCK:

    Profit = (Invoice.Invoice-Num / Invoice.Total-Paid) * 100.
    EXPORT STREAM str DELIMITER "," Amount Cust-Num Invoice-Date Invoice-Num  Total-Paid Profit. 
END.

OUTPUT STREAM str CLOSE.

Now you can run this program, assuming it's named "program.p":

RUN program.p("1", 5, "c:\temp\file.txt").

or

RUN program.p(INPUT "1", INPUT 5, INPUT "c:\temp\file.txt").

(INPUT is the default direction for parameters).

EDIT: Run example + changed first input to CHARACTER instead of integer

Upvotes: 5

Related Questions