0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

insert生成マクロ

Last updated at Posted at 2024-11-13
Sub GenerateInsertStatementsToFile()
    Dim ws As Worksheet
    Dim tableName As String
    Dim colNames As Variant
    Dim dataTypes As Variant
    Dim lastRow As Long
    Dim lastCol As Long
    Dim i As Long, j As Long
    Dim sql As String
    Dim outputFileName As String
    Dim fso As Object
    Dim ts As Object
    Dim filePath As String
    
    Set ws = ThisWorkbook.Sheets("Sheet1") ' データがあるシート名を指定してください

    tableName = ws.Range("B2").Value

    ' 最終列を取得
    lastCol = ws.Cells(3, ws.Columns.Count).End(xlToLeft).Column

    ' カラム名とデータ型を取得
    colNames = ws.Range(ws.Cells(3, 1), ws.Cells(3, lastCol)).Value
    dataTypes = ws.Range(ws.Cells(4, 1), ws.Cells(4, lastCol)).Value

    ' 最終行を取得
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row

    ' 出力ファイル名を設定
    outputFileName = "insert_" & tableName & ".sql"
    ' ファイルパスを設定(この例では、現在のブックと同じディレクトリ)
    filePath = ThisWorkbook.Path & "\" & outputFileName

    ' ファイルシステムオブジェクトを作成
    Set fso = CreateObject("Scripting.FileSystemObject")
    ' 既存のファイルがある場合は削除
    If fso.FileExists(filePath) Then
        fso.DeleteFile filePath
    End If
    ' テキストストリームを作成
    Set ts = fso.CreateTextFile(filePath, True, False) ' 第三引数はUnicodeを指定(FalseでANSI)

    ' データ行ごとにINSERT文を生成
    For i = 5 To lastRow
        sql = "INSERT INTO " & tableName & " ("
        ' カラム名を追加
        For j = 1 To UBound(colNames, 2)
            sql = sql & colNames(1, j)
            If j < UBound(colNames, 2) Then
                sql = sql & ", "
            End If
        Next j
        sql = sql & ") VALUES ("
        ' 値を追加
        For j = 1 To UBound(dataTypes, 2)
            Dim value As Variant
            value = ws.Cells(i, j).Value
            Select Case LCase(dataTypes(1, j))
                Case "int", "integer", "float", "double", "decimal"
                    If IsNumeric(value) Then
                        sql = sql & value
                    Else
                        sql = sql & "NULL"
                    End If
                Case "date", "datetime", "timestamp"
                    If IsDate(value) Then
                        sql = sql & "'" & Format(value, "yyyy-mm-dd hh:nn:ss") & "'"
                    Else
                        sql = sql & "NULL"
                    End If
                Case Else ' 文字列型
                    sql = sql & "'" & Replace(value, "'", "''") & "'"
            End Select
            If j < UBound(dataTypes, 2) Then
                sql = sql & ", "
            End If
        Next j
        sql = sql & ");"
        ' SQL文をファイルに書き込む
        ts.WriteLine sql
    Next i

    ' テキストストリームを閉じる
    ts.Close

    MsgBox "SQL文の生成が完了しました。ファイル名:" & outputFileName
End Sub
Sub GenerateInsertStatementsToFile()
    Dim ws As Worksheet
    Dim tableName As String
    Dim colNames As Variant
    Dim dataTypes As Variant
    Dim lastRow As Long
    Dim lastCol As Long
    Dim i As Long, j As Long
    Dim sql As String
    Dim outputFileName As String
    Dim fso As Object
    Dim ts As Object
    Dim filePath As String
    
    Set ws = ThisWorkbook.Sheets("Sheet1") ' データがあるシート名を指定してください

    tableName = ws.Range("B2").Value

    ' 最終列を取得
    lastCol = ws.Cells(3, ws.Columns.Count).End(xlToLeft).Column

    ' カラム名とデータ型を取得
    colNames = ws.Range(ws.Cells(3, 1), ws.Cells(3, lastCol)).Value
    dataTypes = ws.Range(ws.Cells(4, 1), ws.Cells(4, lastCol)).Value

    ' 最終行を取得
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row

    ' 出力ファイル名を設定
    outputFileName = "insert_" & tableName & ".sql"
    ' ファイルパスを設定(この例では、現在のブックと同じディレクトリ)
    filePath = ThisWorkbook.Path & "\" & outputFileName

    ' ファイルシステムオブジェクトを作成
    Set fso = CreateObject("Scripting.FileSystemObject")
    ' 既存のファイルがある場合は削除
    If fso.FileExists(filePath) Then
        fso.DeleteFile filePath
    End If
    ' テキストストリームを作成
    Set ts = fso.CreateTextFile(filePath, True, False) ' 第三引数はUnicodeを指定(FalseでANSI)

    ' データ行ごとにINSERT文を生成
    For i = 5 To lastRow
        sql = "INSERT INTO " & tableName & " ("
        ' カラム名を追加
        For j = 1 To UBound(colNames, 2)
            sql = sql & colNames(1, j)
            If j < UBound(colNames, 2) Then
                sql = sql & ", "
            End If
        Next j
        sql = sql & ") VALUES ("
        ' 値を追加
        For j = 1 To UBound(dataTypes, 2)
            Dim value As Variant
            value = ws.Cells(i, j).Value
            Select Case LCase(dataTypes(1, j))
                Case "int", "integer", "float", "double", "decimal"
                    If IsNumeric(value) Then
                        sql = sql & value
                    Else
                        sql = sql & "NULL"
                    End If
                Case "boolean", "bool"
                    ' Boolean型の処理
                    If LCase(CStr(value)) = "true" Or value = 1 Then
                        sql = sql & "1"
                    ElseIf LCase(CStr(value)) = "false" Or value = 0 Then
                        sql = sql & "0"
                    Else
                        sql = sql & "NULL"
                    End If
                Case "date", "datetime", "timestamp"
                    If IsDate(value) Then
                        sql = sql & "'" & Format(value, "yyyy-mm-dd hh:nn:ss") & "'"
                    Else
                        sql = sql & "NULL"
                    End If
                Case Else ' 文字列型
                    sql = sql & "'" & Replace(value, "'", "''") & "'"
            End Select
            If j < UBound(dataTypes, 2) Then
                sql = sql & ", "
            End If
        Next j
        sql = sql & ");"
        ' SQL文をファイルに書き込む
        ts.WriteLine sql
    Next i

    ' テキストストリームを閉じる
    ts.Close

    MsgBox "SQL文の生成が完了しました。ファイル名:" & outputFileName
End Sub
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?