Generate insert SQL statements from a CSV file

后端 未结 12 2097
轮回少年
轮回少年 2020-12-04 16:07

I need to import a csv file into Firebird and I\'ve spent a couple of hours trying out some tools and none fit my needs.

The main problem is that al

12条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 16:58

    Just finished this VBA script which might be handy for this purpose. All should need to do is change the Insert statement to include the table in question and the list of columns (obviously in the same sequence they appear on the Excel file).

    Function CreateInsertStatement()
        'Output file location and start of the insert statement
        SQLScript = "C:\Inserts.sql"
        cStart = "Insert Into Holidays (HOLIDAY_ID, NAT_HOLDAY_DESC, NAT_HOLDAY_DTE) Values ("
    
        'Open file for output
        Open SQLScript For Output As #1
    
        Dim LoopThruRows As Boolean
        Dim LoopThruCols As Boolean
    
    
        nCommit = 1 'Commit Count
        nCommitCount = 100 'The number of rows after which a commit is performed
    
        LoopThruRows = True
        nRow = 1 'Current row
    
        While LoopThruRows
    
            nRow = nRow + 1 'Start at second row - presuming there are headers
            nCol = 1 'Reset the columns
            If Cells(nRow, nCol).Value = Empty Then
                Print #1, "Commit;"
                LoopThruRows = False
            Else
                If nCommit = nCommitCount Then
                    Print #1, "Commit;"
                    nCommit = 1
                Else
                    nCommit = nCommit + 1
                End If
    
                cLine = cStart
                LoopThruCols = True
    
                While LoopThruCols
                    If Cells(nRow, nCol).Value = Empty Then
                        cLine = cLine & ");"                    'Close the SQL statement
                        Print #1, cLine                         'Write the line
                        LoopThruCols = False                    'Exit the cols loop
                    Else
                        If nCol > 1 Then                        'add a preceeding comma for all bar the first column
                            cLine = cLine & ", "
                        End If
                        If Right(Left(Cells(nRow, nCol).Value, 3), 1) = "/" Then 'Format for dates
                            cLine = cLine & "TO_DATE('" & Cells(nRow, nCol).Value & "', 'dd/mm/yyyy')"
                        ElseIf IsNumeric(Left(Cells(nRow, nCol).Value, 1)) Then 'Format for numbers
                            cLine = cLine & Cells(nRow, nCol).Value
                        Else 'Format for text, including apostrophes
                            cLine = cLine & "'" & Replace(Cells(nRow, nCol).Value, "'", "''") & "'"
                        End If
    
                        nCol = nCol + 1
                    End If
                Wend
            End If
        Wend
    
        Close #1
    
    End Function
    

提交回复
热议问题