There are better ways to do SQL inserts but sometimes you just want to generate SQL. Maybe? Either way, it shows the use of classes in PowerShell.
PowerShell
x
122
122
1
class BuildSQLArray {
2
3
<#
4
.SYNOPSIS
5
Class to build SQL 'insert' value rows easily.
6
7
.METHODS
8
AddRow()
9
GetRows()
10
GenSQL()
11
12
.PARAMETER
13
AddRow([System.Collections.ArrayList]$Row)
14
GenSQL([string]$Table, [System.Collections.ArrayList]$Columns)
15
16
.EXAMPLE
17
$SQLBuilder = New-Object BuildSQLArray
18
19
[System.Collections.ArrayList]$ColValues = @("'a'","'b'","'c'","'d'")
20
$SQLBuilder.AddRow($ColValues)
21
22
[System.Collections.ArrayList]$ColValues = @("'e'","'f'","'g'","'h'")
23
$SQLBuilder.AddRow($ColValues)
24
25
[System.Collections.ArrayList]$ColValues = @("'i'","'j'","'k'","'l'")
26
$SQLBuilder.AddRow($ColValues)
27
28
[System.Collections.ArrayList]$ColValues = @("'m'","'n'","'o'","'p'")
29
$SQLBuilder.AddRow($ColValues)
30
31
ForEach ($List in $SQLBuilder.GetRows()) {
32
Write-Host $List
33
}
34
35
$InsertSQL = $SQLBuilder.GenSQL("DBO.TheTable", @("1"; "2"; "3"; "4"))
36
Write-Host $InsertSQL
37
38
.RETURN
39
[System.Collections.ArrayList]$Rows = GetRows()
40
[String]$Sql = GenSQL()
41
#>
42
43
[System.Collections.ArrayList]$ValueList = @()
44
[System.Collections.ArrayList]$ValueLists = @()
45
[int]$LimitRows = 800
46
[int]$RowCount = 0
47
48
[void]AddRow([System.Collections.ArrayList]$Row) {
49
50
$this.RowCount += 1
51
52
if ($this.RowCount -le $this.LimitRows) {
53
$StrRow = $Row -Join ","
54
$StrRow = "({0})" -f $StrRow
55
$this.ValueList.Add($StrRow)
56
} else {
57
$this.UpdateRows()
58
$StrRow = $Row -Join ","
59
$StrRow = "({0})" -f $StrRow
60
$this.ValueList.Add($StrRow)
61
}
62
}
63
64
[void]UpdateRows() {
65
66
if ($this.ValueList) {
67
$StrRows = $this.ValueList -join ","
68
$StrRows = "{0}" -f $StrRows
69
$this.ValueLists += $StrRows
70
[System.Collections.ArrayList]$this.ValueList = @()
71
$this.RowCount = 0
72
}
73
}
74
75
[array]GenSQL([String]$Table, [Array]$Columns) {
76
$SQL = @()
77
$Vals = $this.GetRows()
78
$Columns = $Columns -Join ','
79
80
if ($Vals) {
81
ForEach ($Grp in $Vals) {
82
$SQL += "INSERT INTO $Table ($Columns) VALUES $Grp;"
83
}
84
}
85
return $SQL #-Join "`n"
86
}
87
88
[System.Collections.ArrayList]GetRows() {
89
$this.UpdateRows()
90
return $this.ValueLists
91
}
92
}
93
94
<# MAIN: Test #>
95
cls
96
97
# Instantiate the class.
98
$SQLBuilder = New-Object BuildSQLArray
99
100
# Limit values per insert command. Best to keep this even, and I do not check for odd.
101
$SQLBuilder.RowCount = 2
102
103
# Add some row values.
104
[System.Collections.ArrayList]$ColValues = @("'a'","'b'","'c'","'d'")
105
$SQLBuilder.AddRow($ColValues)
106
107
[System.Collections.ArrayList]$ColValues = @("'e'","'f'","'g'","'h'")
108
$SQLBuilder.AddRow($ColValues)
109
110
[System.Collections.ArrayList]$ColValues = @("'i'","'j'","'k'","'l'")
111
$SQLBuilder.AddRow($ColValues)
112
113
[System.Collections.ArrayList]$ColValues = @("'m'","'n'","'o'","'p'")
114
$SQLBuilder.AddRow($ColValues)
115
116
# Add the table and columns.
117
$InsertSQL = $SQLBuilder.GenSQL("DBO.TheTable", @("1"; "2"; "3"; "4"))
118
119
# Check the result.
120
ForEach ($SQL in $InsertSQL) {
121
Write-Host $SQL
122
}