|
1 |
| -# Export-and-Import-LOB-values |
2 |
| -Oracle PL/SQL solution to move LOB data with text files |
| 1 | + |
| 2 | +# Export and Import LOB values |
| 3 | + |
| 4 | +## Oracle PL/SQL solution to move LOB data with text files |
| 5 | + |
| 6 | +## Why? |
| 7 | + |
| 8 | +Because I did not find any useful and easy solution for exporting and importing LOB data. |
| 9 | + |
| 10 | +The **EXP_IMP_LOB** package can export and import **CLOB, NCLOB, BLOB** type column data using simple SQL (text) files. |
| 11 | + |
| 12 | +## How? |
| 13 | + |
| 14 | +First of all install the package onto both source and target schemas. |
| 15 | +To export run this select |
| 16 | + |
| 17 | + select * from table( EXP_IMP_LOB.EXPORT('table_name','lob_column_name','condition') ); |
| 18 | + |
| 19 | +where the *Table_Name* and *LOB_Column_Name* define the data column and the optional Condition defines the row or rows. |
| 20 | +If there is no condition, then every row data will be exported row by row. |
| 21 | + |
| 22 | +Example: |
| 23 | + |
| 24 | + select * from table( EXP_IMP_LOB.EXPORT('person','image','id=103' ) ); |
| 25 | + |
| 26 | +Result: |
| 27 | + |
| 28 | + / ****************************************************** |
| 29 | + TABLE :PERSON |
| 30 | + COLUMN :IMAGE |
| 31 | + ROW :103 |
| 32 | + ****************************************************** / |
| 33 | + BEGIN |
| 34 | + EXP_IMP_LOB.IMPORT_NEW; |
| 35 | + EXP_IMP_LOB.IMPORT_APPEND ( 'FFD8FFE000104A464....23232323232'); |
| 36 | + EXP_IMP_LOB.IMPORT_APPEND ( '32323232323232323....798999AA2A3'); |
| 37 | + ......... |
| 38 | + EXP_IMP_LOB.IMPORT_APPEND ( 'B2316524267279AA9....51401FFFD9'); |
| 39 | + EXP_IMP_LOB.IMPORT_UPDATE ( 'PERSON','IMAGE','103' ); |
| 40 | + COMMIT; |
| 41 | + END; |
| 42 | + / |
| 43 | + |
| 44 | +So, the export converts the binary data to 400 char length hexa strings and creates a script from it. |
| 45 | +I used ..... to symbolize many chars, because that is only a sample above. |
| 46 | +DO NOT ORDER THE RESULT! |
| 47 | +To import, you only have to install the package onto the target schema too and run this script above in the target schema. |
| 48 | +That's all. |
| 49 | + |
| 50 | +...more: |
| 51 | + |
| 52 | +* The source and target *table name, column name* must be the same! |
| 53 | +* The Table (both source and target) must have Primary key and they must be identical. |
| 54 | +* The EXPORT function can detect the primary key automatically. Theoretically it can manage composed keys too... |
| 55 | +* The size of a hexa string is defined in G_LENGTH global variable. 200 chars means 400 hexa chars. |
| 56 | +* The additional procedures: |
| 57 | +* **IMPORT_NEW**: resets the package variables to prepare it to accept a new LOB |
| 58 | +* **IMPORT_APPEND** : converts the hexa string to a binary data and append it the package variable |
| 59 | +* **IMPORT_UPDATE** : updates the given table, row, column with the package variable |
| 60 | +* **DIRECT_SQL** : executes the given SQL using the global LOB variable as a parameter. eg: |
| 61 | + |
| 62 | + EXP_IMP_LOB.DIRECT_SQL( 'insert into ANY_TABLE ( ID, IMAGE ) values ( 123, :1 )' ); |
| 63 | + |
0 commit comments