(1) 「Microsoft Visual Studio Express 2012 for Windows Desktop」を開き、新規で「Windowsフォームアプリケーション」を開く
(2) Form1に適当にDataGridViewをいれておく。
(3) Form1.vbに以下のコードを追記
Public Class Form1
Public Sub New()
' この呼び出しはデザイナーで必要です。 InitializeComponent()
' InitializeComponent() 呼び出しの後で初期化を追加します。 Construct() End Sub
Private Sub Construct() ' テーブル定義 Dim dtMain As New DataTable dtMain.Columns.Add(New DataColumn With {.DataType = System.Type.GetType("System.Int32"), .ColumnName = "ID"}) dtMain.Columns.Add(New DataColumn With {.DataType = System.Type.GetType("System.String"), .ColumnName = "NAME"})
Dim dtSub1 As DataTable = dtMain.Clone Dim dtSub2 As DataTable = dtMain.Clone Dim dtKekka As DataTable = dtMain.Clone Dim dtRow As DataRow = Nothing
' データインサート For i As Integer = 1 To 3 dtRow = dtMain.NewRow dtRow("ID") = i dtMain.Rows.Add(dtRow) If i = 1 Then dtRow("NAME") = "外結DT1" dtSub1.ImportRow(dtRow) ElseIf i = 3 Then dtRow("NAME") = "外結DT2" dtSub2.ImportRow(dtRow) End If Next
' 外結クエリ① Dim query = From main In dtMain.AsEnumerable _ From sub1 In dtSub1.AsEnumerable.Where(Function(m) m.Field(Of Integer)("ID") = main.Field(Of Integer)("ID")).DefaultIfEmpty() _ From sub2 In dtSub2.AsEnumerable.Where(Function(m) m.Field(Of Integer)("ID") = main.Field(Of Integer)("ID")).DefaultIfEmpty() _ Select New With _ { _ .id = main!ID, _ .name = If(sub1 Is Nothing, If(sub2 Is Nothing, main!NAME, sub2!NAME), sub1!NAME) _ } For Each row In query dtRow = dtKekka.NewRow dtRow("ID") = row.id dtRow("NAME") = row.name dtKekka.Rows.Add(dtRow) Next Me.DataGridView1.DataSource = dtKekka
SQL>grant read, write on directory DATA_PUMP_DIR to admin; SQL>grant execute on utl_file to admin;
■2.プロシージャの作成
次にadminスキーマにログインして、CSV出力するプロシージャ(csv_out)をコンパイル。
create or replace procedure csv_out (tbl_name in varchar2) is select_str varchar2(2000); procedure create_select(tbl in varchar2, record_str out varchar2) is type rec_type is record(column_name varchar2(30), data_type varchar2(30)); type tbl_rec_type is table of rec_type; tbl_rec tbl_rec_type; begin record_str := 'select '; execute immediate 'select column_name, data_type from user_tab_columns where table_name = :1 order by column_id' bulk collect into tbl_rec using tbl; for i in 1 .. tbl_rec.count loop record_str := record_str || case when tbl_rec(i).data_type in ('VARCHAR2', 'CHAR', 'CLOB') then ' ''"'' || ' else '' end || tbl_rec(i).column_name || case when tbl_rec(i).data_type in ('VARCHAR2', 'CHAR', 'CLOB') then ' || ''"'' ' else '' end || case when i=tbl_rec.last then '' else ' || '','' ||' end; end loop; record_str := record_str || ' as csv_data from ' || tbl; end;
procedure create_csv(tbl in varchar2, str in varchar2) is fout utl_file.file_type; type csv_data_type is table of varchar2(5000); csv_data_tbl csv_data_type; begin fout := utl_file.fopen('DATA_PUMP_DIR', tbl || '.csv', 'w'); execute immediate str bulk collect into csv_data_tbl; for i in 1 .. csv_data_tbl.count loop utl_file.put_line(fout, csv_data_tbl(i)); end loop; utl_file.fclose(fout); end; begin -- CSV出力Select文作成 create_select(upper(tbl_name), select_str); -- CSV出力 create_csv(upper(tbl_name), select_str); end; /