' ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
' brief : INSERT or UPDATE or DELETE SQL文を作成して実行(1行)
' note : 引数
' テーブル名、ワークシート、SQLタイプ指定行、データベースカラム名指定行
' データ格納開始行、データ格納開始列
' ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
Public Sub createAndExcuteOracleSql(table_name As String, sheet As Worksheet, sql_type_defined_row As Long, db_col_name_defined_row As Long _
, db_data_start_row As Long, db_data_start_col As Long)
Dim sql As String
' SQL作成
sql = createOracleSql(table_name, sheet, sql_type_defined_row, db_col_name_defined_row, db_data_start_row, db_data_start_col)
' SQL実行
excuteSql sql
End Sub
' ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
' brief : INSERT or UPDATE or DELETE SQL文を作成して実行(複数行)
' note : 引数
' テーブル名、ワークシート、SQLタイプ指定行、データベースカラム名指定行
' データ格納開始行、データ格納開始列
' ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
Public Sub createAndExcuteOracleSqls(table_name As String, sheet As Worksheet, sql_type_defined_row As Long, db_col_name_defined_row As Long _
, db_data_start_row As Long, db_data_start_col As Long)
Dim i As Long
Dim sql As String
Dim val
Dim db_data_end_row As Long
' データの最終行を検索
i = 1
Do While sheet.Cells(db_data_start_row + i, db_data_start_col) <> ""
i = i + 1
Loop
db_data_end_row = db_data_start_row + i - 1
For i = db_data_start_row To db_data_end_row
createAndExcuteOracleSql table_name, sheet, sql_type_defined_row, db_col_name_defined_row, i, db_data_start_col
Next
End Sub
' ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
' brief : INSERT or UPDATE or DELETE SQL文を作成
' note : 引数
' テーブル名、ワークシート、SQLタイプ指定行、データベースカラム名指定行
' データ格納開始行、データ格納開始列
' ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
Public Function createOracleSql(table_name As String, sheet As Worksheet, sql_type_defined_row As Long, db_col_name_defined_row As Long _
, db_data_start_row As Long, db_data_start_col As Long) As String
Dim sql As String
sql = ""
Select Case checkSqlType(sheet, sql_type_defined_row, db_data_start_col)
Case "INSERT"
sql = createInsertOracleSql(table_name, sheet, sql_type_defined_row, db_col_name_defined_row, db_data_start_row, db_data_start_col)
createOracleSql = sql
Exit Function
Case "UPDATE"
sql = createUpdateOracleSql(table_name, sheet, sql_type_defined_row, db_col_name_defined_row, db_data_start_row, db_data_start_col)
createOracleSql = sql
Exit Function
Case "DELETE"
sql = createDeleteOracleSql(table_name, sheet, sql_type_defined_row, db_col_name_defined_row, db_data_start_row, db_data_start_col)
createOracleSql = sql
Exit Function
End Select
createOracleSql = sql
End Function
' ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
' brief : Excelから作成するSQLタイプがINSERT,UPDATE,DELTEのどれかなのか確認
' note :
' ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
Private Function checkSqlType(sheet As Worksheet, type_start_row As Long, type_start_col As Long) As String
Dim start_col As Long, end_col As Long
Dim i As Long
checkSqlType = ""
start_col = type_start_col
end_col = getMaxColRight(sheet, type_start_row, type_start_col)
For i = start_col To end_col
Select Case sheet.Cells(type_start_row, i)
Case "INSERT"
checkSqlType = "INSERT"
Exit Function
Case "UPDATE"
checkSqlType = "UPDATE"
Exit Function
Case "DELETE"
checkSqlType = "DELETE"
Exit Function
End Select
Next
MsgBox "DBManager.checkSqlType:SQLタイプの設定が間違っています。INSERT,UPDATE,DELETEが含まれていません。"
End
End Function
' ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
' brief : INSERT SQL文を作成
' note : 引数
' テーブル名、ワークシート、SQLタイプ指定行、データベースカラム名指定行
' データ格納開始行、データ格納開始列
' ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
Private Function createInsertOracleSql(table_name As String, sheet As Worksheet, sql_type_defined_row As Long, db_col_name_defined_row As Long _
, db_data_start_row As Long, db_data_start_col As Long) As String
Dim i As Long, j As Long
Dim start_row As Long, start_col As Long, end_col As Long
Dim sqltype As String
Dim sql As String, sql_1 As String, sql_2 As String
start_row = db_data_start_row
start_col = db_data_start_col
end_col = getMaxColRight(sheet, start_row, start_col)
sqltype = sheet.Cells(sql_type_defined_row, start_col).Value
i = start_row
' INSERT文の生成
sql = ""
sql_1 = "INSERT INTO " & table_name & " ("
sql_2 = "VALUES ("
For j = start_col To end_col
' DBカラム名を追加
sql_1 = sql_1 & sheet.Cells(db_col_name_defined_row, j).Value
If j <> end_col Then
sql_1 = sql_1 & ", "
Else
sql_1 = sql_1 & ") "
End If
' DBカラム名に対する値を追加
sql_2 = sql_2 & "'" & sheet.Cells(i, j).Value & "'"
If j <> end_col Then
sql_2 = sql_2 & ", "
Else
sql_2 = sql_2 & ") "
End If
Next
sql = sql_1 + sql_2
sql = Replace(sql_1 + sql_2, ", WHERE", " WHERE")
If Right(sql, 1) = "," Then sql = Mid(sql, 1, Len(sql) - 1)
If Right(sql, 5) = " AND " Then sql = Mid(sql, 1, Len(sql) - 5)
createInsertOracleSql = sql
End Function
' ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
' brief : UPDATE SQL文を作成
' note : 引数
' テーブル名、ワークシート、SQLタイプ指定行、データベースカラム名指定行
' データ格納開始行、データ格納開始列
' ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
Private Function createUpdateOracleSql(table_name As String, sheet As Worksheet, sql_type_defined_row As Long, db_col_name_defined_row As Long _
, db_data_start_row As Long, db_data_start_col As Long) As String
Dim i As Long, j As Long
Dim start_row As Long, start_col As Long, end_col As Long
Dim sqltype As String
Dim sql As String, sql_1 As String, sql_2 As String
start_row = db_data_start_row
start_col = db_data_start_col
end_col = getMaxColRight(sheet, start_row, start_col)
sqltype = sheet.Cells(sql_type_defined_row, start_col).Value
i = start_row
' UPDATE文の生成
sql = ""
sql_1 = "UPDATE " & table_name & " SET "
sql_2 = "WHERE "
For j = start_col To end_col
If sheet.Cells(sql_type_defined_row, j).Value = "UPDATE" Then
' UPDATEするDBカラム名と値を追加
sql_1 = sql_1 & sheet.Cells(db_col_name_defined_row, j).Value & " = '" _
& sheet.Cells(i, j).Value & "'"
If j <> end_col Then
sql_1 = sql_1 & ", "
Else
sql_1 = sql_1 & " "
End If
ElseIf sheet.Cells(sql_type_defined_row, j).Value = "WHERE" Then
' WHEREに対するDBカラム名と値を追加
sql_2 = sql_2 & sheet.Cells(db_col_name_defined_row, j).Value & " = '" _
& sheet.Cells(i, j).Value & "'"
If j <> end_col Then
sql_2 = sql_2 & " AND "
Else
sql_2 = sql_2 & " "
End If
Else
Debug.Print ("UPDATE文に対して、SQLタイプ設定が正しくありません。設定を見直して下さい。")
Exit Function
End If
Next
sql = sql_1 + sql_2
sql = Replace(sql_1 + sql_2, ", WHERE", " WHERE")
If Right(sql, 1) = "," Then sql = Mid(sql, 1, Len(sql) - 1)
If Right(sql, 5) = " AND " Then sql = Mid(sql, 1, Len(sql) - 5)
createUpdateOracleSql = sql
End Function
' ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
' brief : DELETE SQL文を作成
' note : 引数
' テーブル名、ワークシート、SQLタイプ指定行、データベースカラム名指定行
' データ格納開始行、データ格納開始列
' ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
Private Function createDeleteOracleSql(table_name As String, sheet As Worksheet, sql_type_defined_row As Long, db_col_name_defined_row As Long _
, db_data_start_row As Long, db_data_start_col As Long) As String
Dim i As Long, j As Long
Dim start_row As Long, start_col As Long, end_col As Long
Dim sqltype As String
Dim sql As String, sql_1 As String, sql_2 As String
start_row = db_data_start_row
start_col = db_data_start_col
end_col = getMaxColRight(sheet, start_row, start_col)
sqltype = sheet.Cells(sql_type_defined_row, start_col).Value
i = start_row
' DELETE文の生成
sql = ""
sql_1 = "DELETE FROM " & table_name & " "
sql_2 = "WHERE "
For j = start_col To end_col
If sheet.Cells(sql_type_defined_row, j).Value = "DELETE" Then
' WHEREに対するDBカラム名と値を追加
sql_2 = sql_2 & sheet.Cells(db_col_name_defined_row, j).Value & " = '" _
& sheet.Cells(i, j).Value & "'"
If j <> end_col Then
sql_2 = sql_2 & " AND "
Else
sql_2 = sql_2 & " "
End If
Else
Debug.Print ("DELETE文に対して、SQLタイプ設定が正しくありません。設定を見直して下さい。")
Exit Function
End If
Next
sql = sql_1 + sql_2
sql = Replace(sql_1 + sql_2, ", WHERE", " WHERE")
If Right(sql, 1) = "," Then sql = Mid(sql, 1, Len(sql) - 1)
If Right(sql, 5) = " AND " Then sql = Mid(sql, 1, Len(sql) - 5)
createDeleteOracleSql = sql
End Function