I have in my hands an old app on informix server and I'm migrating data into a different database. Both are informix databases. I have a particular problem with one table. Old app used it to support multiline text.

OldTable: HeaderID int LineNum int Descr nvarchar(50,1)

NewTable: HeaderID int Descr lvarchar(max)

So, for each HeaderID I have to read the descriptions ordered by line number and put them all together for insert into a new table. There has to be a newline character between each line for conversion to succeed.

Any tips on how to do this?

有帮助吗?

解决方案

If you need to do it from SQL then you can use procedure:

CREATE FUNCTION get_text(aHeaderID int)
RETURNING lvarchar;
DEFINE result lvarchar;
DEFINE vcfld lvarchar;
    LET result=NULL;
    EXECUTE PROCEDURE IFX_ALLOW_NEWLINE('T');
    FOREACH cur1
        FOR SELECT Descr INTO vcfld FROM OldTable WHERE HeaderID = aHeaderID ORDER BY LineNum
        IF result IS NULL THEN
            LET result = vcfld;
        ELSE
            LET result = result || '
' || vcfld;
        END IF;
    END FOREACH;
    RETURN result;
END FUNCTION;

(notice usage of IFX_ALLOW_NEWLINE and line breaking when updating result)

Then you can fill NewTable using:

UPDATE NewTable SET Descr=get_text(HeaderID);

其他提示

You can use PreparedStatement. This is example in Jython that uses JDBC Informix driver:

db = DriverManager.getConnection(db_url, usr, passwd)
pstm = db.prepareStatement("SELECT vc FROM src ORDER BY id")
rs = pstm.executeQuery()
lines = []
while (rs.next()):
    lines.append(rs.getString(1))
pstm = db.prepareStatement("INSERT INTO dest (lvc) VALUES (?)")
pstm.setString(1, '\n'.join(lines))
rs = pstm.execute()
db.close()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top