问题
I'm writing an Informix procedure. I created a temp table, but I want to export this data in a text document. I can't use an UNLOAD statement in SPL; can somebody help me?
回答1:
The UNLOAD statement is not recognized by the server. The programs such as DB-Access, ISQL and I4GL recognize it and simulate it as a statement (basically, as a SELECT, of course, reading the data from the server and writing it to a file on the client). Consequently, the statement won't work in SPL — Stored Procedure Language — because that is run by the server on the server. (One reason for the disconnect is that the server can't write direct to a file on the client.)
So, you'll need to do things differently. The solution suggested by Ricardo Henriques in his answer is entirely sensible, as long as the file being created on the server machine is OK.
回答2:
Will you execute it on the Informix Server or a client? Which is the IDS version?
Here is a way using external tables, the file is stored on the server host:
CREATE PROCEDURE sp_unload()
CREATE TEMP TABLE temp1(
col1 INT
) WITH NO LOG;
INSERT INTO temp1 VALUES (1);
INSERT INTO temp1 VALUES (2);
INSERT INTO temp1 VALUES (3);
CREATE EXTERNAL TABLE temp1_ext
SAMEAS temp1
USING (
DATAFILES ("DISK:/home/informix/temp.dat")
);
INSERT INTO temp1_ext SELECT * FROM temp1;
DROP TABLE temp1_ext;
DROP TABLE temp1;
END PROCEDURE;
If what you're looking is to store on a client server you'll have to provide what kind of client you use (OS and the way you connect to the IDS).
If your using the dbaccess you can use the SP to create the temporary table and after submit the unload
of the data.
If using other clients probably your best option is to rewrite your SP as a function that returns more than one value from more than one row..
Then, iterate it and write to a file.
来源:https://stackoverflow.com/questions/31992388/using-an-unload-statement-in-an-informix-stored-procedure