본문 바로가기

낙서장/낙서장

Excel - Visual Basic SQL Query for loop Cod

Sub update()

Dim i, j As Long
Dim str As String

 
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    Dim targetFile As Object
    Dim myFilePath As String
    Dim myFileText As String

    myFilePath = "C:\org\sample_sql.txt"
    Set targetFile = fso.CreateTextFile(myFilePath, True)  ' this will overwrite any existing file
        
       
       targetFile.WriteLine "-- ============================================= Sample Insert ============================================="
        
       For j = 2 To 62
           

targetFile.WriteLine "INSERT INTO SAMPLE(SAMPLE_A, SAMPLE_B) VALUES('" & Sheets("sample_sheet").Cells(j, 1).Value & "', '" & Sheets("sample_sheet").Cells(j, 2).Value & "');"
               
       Next j
 
          
         targetFile.WriteLine "commit;"
    
    
    targetFile.Close ' close the file
End Sub