2009-10-15 4 views
0

엑셀 시트의 데이터를 테라 데이타 테이블에 삽입해야합니다. 및 MACRO를 사용하여이 작업을 수행해야합니다.엑셀 시트의 데이터를 데이터베이스 테이블에 삽입하는 방법은 무엇입니까?

나는

에 이렇게

 
COL1 COL2 COL3 COL4 
1  2 3  4 
2  5 8  10 
. 
. 
같은 엑셀 시트에 데이터가 나는 버튼에게의 행을 클릭하면 있도록 내가 엑셀 ​​시트에 버튼을 유지하고 그 버튼에 매크로를 지정해야 Excel 시트를 데이터베이스 테이블에 삽입해야합니다.

요구 사항은 필자가 빈 Excel 시트를 사람에게 보내고 데이터 시트를 채우고 Excel의 단추를 클릭하여 데이터를 데이터베이스 테이블에 삽입해야한다는 것입니다. 매크로를 사용하여 이렇게하는 것이 더 좋을 것입니다.

감사합니다.

답변

2

엑셀에서 SQL Server로 데이터를 이동하기 위해 vb/vba 코드 (marco의 경우)를 사용하는 링크를 보시려면 this을보십시오.

+0

SQLServer에 연결하는 코드가 있습니다. 하지만 Teradata 데이터베이스에 대해 동일한 작업을 수행하려고합니다. – SrinivasR

+0

ok,이 방법에 대해 http://209.85.229.132/search?q=cache:LKwYRBskhUoJ:www.teradataforum.com/attachments/a040130a.rtf+Teradata+ database + excel & cd = 6 & hl = ko & ct = clnk & gl = za –

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 각 명령에, 그럼 그냥 첫 번째 행에서 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