生成insert指令碼的指令碼

husthxd發表於2005-03-03

1.透過sql/儲存過程

2.透過java程式


0.sql

-- created by yangtingkun

select 'insert into yourtable values (' || col_num || ', ''' || col_char || ''', to_date(' || to_char(col_date, 'yyyy-mm-dd hh24:mi:ss' ||, '''yyyy-mm-dd hh24:mi:ss'');' from yourtable;

1.透過儲存過程生成insert指令碼
drop table t_data
/
create table t_data
as
select object_id from dba_objects
where rownum < 100
/

set serveroutput on

declare
begin
  dbms_output.enable(100000);
  for c_1 in (select * from t_data where rownum < 5) loop
    dbms_output.put_line('insert into t_data values');
    dbms_output.put_line('('||c_1.object_id||');');
 end loop;
end;
/

2.使用java

    /**
     * 根據輸入的sql生成insert指令碼 created by husthxd 2005-03-03 v0.1
     * @param conn
     * @param sqlstmt
     * @param uri 輸出的檔名稱
     * @return
     * @throws GFPortalException
     */
    public boolean createSqlFile(Connection conn, String sqlstmt, String tablename,String uri) throws GFPortalException {
        //執行語句
        PreparedStatement pstmt;
        ResultSet rs = null;
        //連線
        try {
            //獲取sql語句
            pstmt =
                    conn.prepareStatement(sqlstmt,
                            ResultSet.TYPE_SCROLL_INSENSITIVE,
                            ResultSet.CONCUR_READ_ONLY);
        } catch (Exception e) {
            logger.debug("查詢初始化失敗!" + e);
            throw new GFPortalException("查詢初始化失敗!" + e);
        }
        //獲取結果集
        try {
            rs = pstmt.executeQuery(); //結果集
            ResultSetMetaData rsmd = rs.getMetaData(); //後設資料
            PrintWriter out = new PrintWriter(new FileWriter(uri), true);
            while (rs.next()) {
                StringBuffer row = new StringBuffer();
                row.append("insert into " + tablename + " values(");
                int count = rsmd.getColumnCount();
                //獲取其中的一條記錄
                for (int i = 1; i <= count; i++) {
                    //放入到檔案中
                    if (!CommonUtility.isNull(rs.getObject(i)))//判斷是否為null
                    {
                        switch (rsmd.getColumnType(i)) {
                            case Types.BIT:
                            case Types.INTEGER:
                            case Types.TINYINT:
                            case Types.BIGINT:
                            case Types.REAL:
                            case Types.FLOAT:
                            case Types.DOUBLE:
                            case Types.NUMERIC:
                            case Types.DECIMAL:
                            case Types.LONGVARBINARY:
                            case Types.VARBINARY:
                            case Types.BINARY:
                                row.append(rs.getObject(i));
                                break;

                            case Types.BLOB:
                            case Types.CLOB:
                                break;

                            case Types.DATE:
                            case Types.TIME:
                            case Types.TIMESTAMP:
                            case Types.NULL:
                                row.append("to_date('");
                                row.append(CommonUtility.getYMDHMM(rs.getDate(i)));
                                row.append("'");
                                row.append(",'yyyy-mm-dd hh24:mi:ss')");
                                break;

                            case Types.LONGVARCHAR:
                            case Types.CHAR:
                            case Types.VARCHAR:
                            case Types.OTHER:
                            default:
                                row.append("'");
                                row.append(rs.getString(i));
                                row.append("'");
                                break;
                        }//end switch
                    } else {
                        row.append("''");
                    }
                    if (i == count)//判斷是否最後一列
                    {
                        row.append(");");
                    } else {
                        row.append(",");
                    }
                }
                //完成一行
                out.println(row.toString());
                //釋放資源
                row = null;
            }
        } catch (Exception e) {
            logger.debug("查詢出錯!" + e);
            throw new GFPortalException("查詢出錯!" + e.getMessage());
        } finally {
            try {
                ConnMgr.closePreparedStatement(pstmt);
                rs.close();
                pstmt.close();
            } catch (Exception e2) {

            }
        }
        return true;
    }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/6906/viewspace-21715/,如需轉載,請註明出處,否則將追究法律責任。

相關文章