Skip to content

Commit 1d1d8f4

Browse files
authored
Create using VBA.sql
1 parent 287deba commit 1d1d8f4

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Sub GenerateSQL()
2+
Dim ws As Worksheet
3+
Dim rowCount As Long
4+
Dim sql As String
5+
Dim outputFile As String
6+
Dim i As Long
7+
Dim id As Variant, name As String, national_id As String, birth_date As String, enrollment_date As String, graduation_date As String, gpa As Variant
8+
9+
Set ws = ThisWorkbook.Sheets(1)
10+
rowCount = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
11+
outputFile = "C:\Users\USER\Downloads\InsertScripts.sql"
12+
Open outputFile For Output As #1
13+
14+
For i = 2 To rowCount
15+
id = ws.Cells(i, 1).Value
16+
name = ws.Cells(i, 2).Value
17+
national_id = ws.Cells(i, 3).Value
18+
birth_date = ws.Cells(i, 4).Value
19+
enrollment_date = ws.Cells(i, 5).Value
20+
graduation_date = ws.Cells(i, 6).Value
21+
gpa = ws.Cells(i, 7).Value
22+
23+
If IsEmpty(id) Or IsEmpty(name) Then GoTo SkipRow
24+
name = Replace(name, "'", "''")
25+
26+
sql = "INSERT INTO Student (id, name, national_id, birth_date, enrollment_date, graduation_date, gpa) VALUES (" & _ id & ", '" & name & "', '" & national_id & "', '" & birth_date & "', '" & enrollment_date & "', '" & graduation_date & "', " & gpa & ");"
27+
Print #1, sql
28+
29+
SkipRow:
30+
Next i
31+
Close #1
32+
MsgBox "SQL scripts generated successfully!"
33+
End Sub

0 commit comments

Comments
 (0)