-
Notifications
You must be signed in to change notification settings - Fork 1
/
PRTools.xlam.SqlGen.bas
47 lines (42 loc) · 1.49 KB
/
PRTools.xlam.SqlGen.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Attribute VB_Name = "SqlGen"
Option Explicit
Public Function GenInsert(ByVal tablename As String, ByVal columns As Range, ByVal Values As Range) As String
GenInsert = GenInsertHead(tablename, columns)
End Function
Public Function GenInsertHead(ByVal tablename As String, ByVal columns As Range) As String
Dim i As Integer, cell As Range
For Each cell In columns.Cells
If i = 0 Then
i = 1
GenInsertHead = "insert into " & tablename & "("
Else
GenInsertHead = GenInsertHead & ", "
End If
GenInsertHead = GenInsertHead & cell.Value
Next cell
GenInsertHead = GenInsertHead & ")"
End Function
Public Function GenValues(lineSeparator As String, Values As Range) As String
Dim i As Integer, cell As Range
For Each cell In Values.Cells
If i = 0 Then
i = 1
GenValues = lineSeparator & "("
Else
GenValues = GenValues & ", "
End If
GenValues = GenValues & ToSqlLiteral(cell.Value)
Next cell
GenValues = GenValues & ")"
End Function
Public Function ToSqlLiteral(Value As Variant) As String
If IsEmpty(Value) Then
ToSqlLiteral = "null"
ElseIf TypeName(Value) = "Date" Then
ToSqlLiteral = Format(Value, IIf(Value = Int(Value), "yyyy-mm-dd", "yyyy-mm-dd hh:mm:ss"))
ElseIf IsNumeric(Value) Then
ToSqlLiteral = CStr(Value)
Else
ToSqlLiteral = "'" & Replace(CStr(Value), "'", "''") & "'"
End If
End Function