我需要从一个excel工作表中的数据插入到一个Teradata表。 和我需要这使用宏来完成。

我有数据在Excel片状,点击

COL1  COL2 COL3 COL4
1      2    3     4
2      5    8     10
.
.
so上

和我需要保持在excel工作表一个按钮和一个宏分配给该按钮,以便当我点击该按钮在excel工作表中的行应被插入到数据库表中。

的要求是,我将空excel表发送给所述的人,他将填补在片材与所述数据和他点击在Excel中按钮,数据必须被插入到数据库表中。 我宁愿使用宏这样..

由于所有

有帮助吗?

解决方案

看一看链路使用VB / VBA代码(用于马科)从Excel到SQL Server移动数据。

其他提示

我已经创建功能,将改变了Excel表分成多个插入命令。

复制到这个模块,然后在式,设置为第一参数在待插入需要的细胞的值,所述第二范围应该列的名称(按F4设置此作为常数)和第三(可选)表的名称。如果没有指定的表名,然后在片材的名称将被用作默认值。

在你的情况是这样的表格看起来应该像:

+---+------+------+------+------+-----------------------------------------+
|   | A    | B    | C    | D    | E                                       |
+---+------+------+------+------+-----------------------------------------+
| 1 | COL1 | COL2 | COL3 | COL4 |                                         |
+---+------+------+------+------+-----------------------------------------+
| 2 | 1    | 2    | 3    | 4    | =Insert2DB(A2:D2,$A$1:$D$1,"TableName") |
+---+------+------+------+------+-----------------------------------------+
| 3 | 2    | 5    | 8    | 10   | =Insert2DB(A3:D3,$A$1:$D$1,"TableName") |
+---+------+------+------+------+-----------------------------------------+

这会为你生成这两个疑问:

INSERT INTO TableName ([COL1],[COL2],[COL3],[COL4]) VALUES (1,2,3,4)
INSERT INTO TableName ([COL1],[COL2],[COL3],[COL4]) VALUES (2,5,8,10)

下面是与Microsoft SQL(TSQL的功能(工作好):

    Function Insert2DB(InputRange As Range, Optional ColumnsNames As Variant, Optional TableName As Variant)

      Dim rangeCell As Range
      Dim InsertValues As String
      Dim CellValue As String
      Dim C As Range

        Dim AllColls As String
        Dim SingleCell As Range
        Dim TableColls As String

    InsertValues = ""

    'Start Loop
    For Each rangeCell In InputRange.Cells

    'Recognize data type
    Set C = rangeCell
        If IsEmpty(C) Then
                'DataType is NULL then NULL
                CellValue = "NULL"
            ElseIf Application.IsText(C) Then
                'DataType is VARCHAR or CHAR
                CellValue = "'" & Trim(rangeCell.Value) & "'"
            ElseIf Application.IsLogical(C) Then
                'DataType is bit eg. TRUE / FALSE
                    If rangeCell.Value = True Then
                        CellValue = "1"
                    ElseIf rangeCell.Value = False Then
                        CellValue = "0"
                    End If
            ElseIf Application.IsErr(C) Then
                'If there is an ERROR in cell, the statment will return 0
                CellValue = "NULL"
            ElseIf IsDate(C) Then
                'DataType is DATE or DATETIME, in case it is DATE specifying HH:mm:ss would do no harm
                CellValue = "'" & VBA.Format(rangeCell.Value, "yyyymmdd hh:mm:ss") & "'"
            ElseIf InStr(1, C.Text, ":") <> 0 Then
                'DataType is TIME
                CellValue = "'" & VBA.Format(rangeCell.Value, "hh:mm:ss") & "'"
            ElseIf IsNumeric(C) Then
                'DataType is number
                CellValue = rangeCell.Value
        End If

    If (Len(InsertValues) > 0) Then
        InsertValues = InsertValues + "," + CellValue
    Else
        InsertValues = CellValue
    End If

    Next rangeCell
    'END Loop

    If IsMissing(ColumnsNames) Then
        TableColls = ""
        Else

        For Each SingleCell In ColumnsNames.Cells
            If Len(AllColls) > 0 Then
                     AllColls = AllColls + "," + "[" + Trim(Replace(SingleCell.Value, Chr(160), "")) + "]"
            Else
                    AllColls = "[" + Trim(Replace(SingleCell.Value, Chr(160), "")) + "]"
            End If
        Next SingleCell
        TableColls = " (" & AllColls & ")"
    End If


    'If TableName is not set, then take the name of a sheet
    If IsMissing(TableName) = True Then
        TableName = ActiveSheet.Name
    Else
    TableName = TableName
    End If

    'Set the return value
        Insert2DB = "INSERT INTO " & TableName & TableColls & " VALUES (" & InsertValues & ")"

    End Function

如果您有相当多的数据插入,则可能不需要使用INSERT INTO在每个命令,那么就使用第1行Insert2DB功能(每500次)和其余的使用只是Insert2DBValues:

+---+------+------+------+------+-----------------------------------------------+
|   | A    | B    | C    | D    | E                                             |
+---+------+------+------+------+-----------------------------------------------+
| 1 | COL1 | COL2 | COL3 | COL4 |                                               |
+---+------+------+------+------+-----------------------------------------------+
| 2 | 1    | 2    | 3    | 4    | =Insert2DB(B3:E3,$B$2:$E$2,"TableName")       |
+---+------+------+------+------+-----------------------------------------------+
| 3 | 2    | 5    | 8    | 10   | =Insert2DBValues(A3:D3,$A$1:$D$1,"TableName") |
+---+------+------+------+------+-----------------------------------------------+

这会给你以下命令:

INSERT INTO TableName ([COL1],[COL2],[COL3],[COL4]) VALUES (1,2,3,4)
,(2,5,8,10)


Function Insert2DBValues(InputRange As Range, Optional ColumnsNames As Variant, Optional TableName As Variant)

  Dim rangeCell As Range
  Dim InsertValues As String
  Dim CellValue As String
  Dim C As Range

    Dim AllColls As String
    Dim SingleCell As Range
    Dim TableColls As String

InsertValues = ""

'Start Loop
For Each rangeCell In InputRange.Cells

'Recognize data type
Set C = rangeCell
    If IsEmpty(C) Then
            'DataType is NULL then NULL
            CellValue = "NULL"
        ElseIf Application.IsText(C) Then
            'DataType is VARCHAR or CHAR
            CellValue = "'" & Trim(rangeCell.Value) & "'"
        ElseIf Application.IsLogical(C) Then
            'DataType is bit eg. TRUE / FALSE
                If rangeCell.Value = True Then
                    CellValue = "1"
                ElseIf rangeCell.Value = False Then
                    CellValue = "0"
                End If
        ElseIf Application.IsErr(C) Then
            'If there is an ERROR in cell, the statment will return 0
            CellValue = "NULL"
        ElseIf IsDate(C) Then
            'DataType is DATE or DATETIME, in case it is DATE specifying HH:mm:ss would do no harm
            CellValue = "'" & VBA.Format(rangeCell.Value, "yyyy-mm-dd hh:mm:ss") & "'"
        ElseIf InStr(1, C.Text, ":") <> 0 Then
            'DataType is TIME
            CellValue = "'" & VBA.Format(rangeCell.Value, "hh:mm:ss") & "'"
        ElseIf IsNumeric(C) Then
            'DataType is number
            CellValue = rangeCell.Value
    End If

If (Len(InsertValues) > 0) Then
    InsertValues = InsertValues + "," + CellValue
Else
    InsertValues = CellValue
End If

Next rangeCell
'END Loop

If IsMissing(ColumnsNames) Then
    TableColls = ""
    Else

    For Each SingleCell In ColumnsNames.Cells
        If Len(AllColls) > 0 Then
                 AllColls = AllColls + "," + "[" + Trim(Replace(SingleCell.Value, Chr(160), "")) + "]"
        Else
                AllColls = "[" + Trim(Replace(SingleCell.Value, Chr(160), "")) + "]"
        End If
    Next SingleCell
    TableColls = " (" & AllColls & ")"
End If


'If TableName is not set, then take the name of a sheet
If IsMissing(TableName) = True Then
    TableName = ActiveSheet.Name
Else
TableName = TableName
End If

'Set the return value
    Insert2DBValues = ",(" & InsertValues & ")"

End Function

最后,如果你正在使用MySQL,有字符串的不同逸出,所以在这样的情况下使用Insert2DBMySQL:

    Function Insert2DBMySQL(InputRange As Range, Optional ColumnsNames As Variant, Optional TableName As Variant)

      Dim rangeCell As Range
      Dim InsertValues As String
      Dim CellValue As String
      Dim C As Range

        Dim AllColls As String
        Dim SingleCell As Range
        Dim TableColls As String

    InsertValues = ""

    'Start Loop
    For Each rangeCell In InputRange.Cells

    'Recognize data type
    Set C = rangeCell
        If IsEmpty(C) Then
                'DataType is NULL then NULL
                CellValue = "NULL"
            ElseIf Application.IsText(C) Then
                'DataType is VARCHAR or CHAR
                CellValue = "'" & Trim(rangeCell.Value) & "'"
            ElseIf Application.IsLogical(C) Then
                'DataType is bit eg. TRUE / FALSE
                    If rangeCell.Value = True Then
                        CellValue = "1"
                    ElseIf rangeCell.Value = False Then
                        CellValue = "0"
                    End If
            ElseIf Application.IsErr(C) Then
                'If there is an ERROR in cell, the statment will return 0
                CellValue = "NULL"
            ElseIf IsDate(C) Then
                'DataType is DATE or DATETIME, in case it is DATE specifying HH:mm:ss would do no harm
                CellValue = "'" & VBA.Format(rangeCell.Value, "yyyy-mm-dd hh:mm:ss") & "'"
            ElseIf InStr(1, C.Text, ":") <> 0 Then
                'DataType is TIME
                CellValue = "'" & VBA.Format(rangeCell.Value, "hh:mm:ss") & "'"
            ElseIf IsNumeric(C) Then
                'DataType is number
                CellValue = rangeCell.Value
        End If

    If (Len(InsertValues) > 0) Then
        InsertValues = InsertValues + "," + CellValue
    Else
        InsertValues = CellValue
    End If

    Next rangeCell
    'END Loop

    If IsMissing(ColumnsNames) Then
        TableColls = ""
        Else

        For Each SingleCell In ColumnsNames.Cells
            If Len(AllColls) > 0 Then
                     AllColls = AllColls + "," + "" + Trim(Replace(SingleCell.Value, Chr(160), "")) + ""
            Else
                    AllColls = "" + Trim(Replace(SingleCell.Value, Chr(160), "")) + ""
            End If
        Next SingleCell
        TableColls = " (" & AllColls & ")"
    End If


    'If TableName is not set, then take the name of a sheet
    If IsMissing(TableName) = True Then
        TableName = ActiveSheet.Name
    Else
    TableName = TableName
    End If

    'Set the return value
        Insert2DBMySQL = "INSERT INTO " & TableName & TableColls & " VALUES (" & InsertValues & ");"

    End Function
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top