2009-10-15 467 views
0

我需要將Excel表格中的數據插入到Teradata表格中。 和我需要這個使用MACRO來完成。如何將Excel表格中的數據插入到數據庫表格中?

我有一個Excel工作表像

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

數據,我需要保持在Excel工作表的按鈕和宏指定到該按鈕,這樣,當我點擊按鈕,在該行Excel表應該插入到數據庫表中。

要求是我將發送空的excel表單給這個人,他會填寫表單中的數據,他點擊excel中的按鈕,數據必須插入到數據庫表中。 我寧願這樣做使用宏。

謝謝大家。

回答

2

看看this使用vb/vba代碼(用於marco)將數據從excel移動到sql server的鏈接。

+0

我得到的代碼連接到SQLServer的。但我想爲Teradata數據庫做同樣的事情.. – SrinivasR 2009-10-15 07:32:25

+0

好吧,那麼這個http://209.85.229.132/search?q=cache:LKwYRBskhUoJ:www.teradataforum.com/attachments/a040130a.rtf+Teradata+數據庫+的excel和CD = 6&HL = EN&CT = clnk&GL = ZA – 2009-10-15 07:55:34

2

我已經創建了將Excel錶轉換爲多個插入命令的函數。

將其複製到模塊中,然後在公式中將第一個參數設置爲需要插入的單元格的值,第二個範圍應該是列的名稱(按F4將其設置爲常量),並且第三(可選)表的名稱。如果未指定表名稱,則表格名稱將被用作默認值。

在你的情況是這樣的表格看起來應該像:

+---+------+------+------+------+-----------------------------------------+ 
| | 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 | =Insert2DB(B4:E4,$B$2:$E$2,"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) 

下面是函數(工作好與微軟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 

如果您有很多要插入的數據,則可能不需要使用IN SERT 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 
相關問題