Skip to content

Commit 4cec791

Browse files
authored
New-DbaSqlParameter, check bound parameters correctly (#9584)
1 parent ea45d97 commit 4cec791

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

public/New-DbaSqlParameter.ps1

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -119,88 +119,90 @@ function New-DbaSqlParameter {
119119
[string]$SourceVersion,
120120
[ValidateSet("BigInt", "Binary", "Bit", "Char", "DateTime", "Decimal", "Float", "Image", "Int", "Money", "NChar", "NText", "NVarChar", "Real", "UniqueIdentifier", "SmallDateTime", "SmallInt", "SmallMoney", "Text", "Timestamp", "TinyInt", "VarBinary", "VarChar", "Variant", "Xml", "Udt", "Structured", "Date", "Time", "DateTime2", "DateTimeOffset")]
121121
[string]$SqlDbType,
122-
[string]$SqlValue,
122+
[object]$SqlValue,
123123
[string]$TypeName,
124124
[string]$UdtTypeName,
125125
[object]$Value,
126126
[switch]$EnableException
127127
)
128+
128129
$param = New-Object Microsoft.Data.SqlClient.SqlParameter
129130

130131
try {
131-
if ($PSBoundParameters.CompareInfo) {
132+
133+
if (Test-Bound -ParameterName CompareInfo) {
132134
$param.CompareInfo = $CompareInfo
133135
}
134136

135-
if ($PSBoundParameters.DbType) {
137+
if (Test-Bound -ParameterName DbType) {
136138
$param.DbType = $DbType
137139
}
138140

139-
if ($PSBoundParameters.Direction) {
141+
if (Test-Bound -ParameterName Direction) {
140142
$param.Direction = $Direction
141143
}
142144

143-
if ($PSBoundParameters.ForceColumnEncryption) {
145+
if (Test-Bound -ParameterName ForceColumnEncryption) {
144146
$param.ForceColumnEncryption = $ForceColumnEncryption
145147
}
146148

147-
if ($PSBoundParameters.IsNullable) {
149+
if (Test-Bound -ParameterName IsNullable) {
148150
$param.IsNullable = $IsNullable
149151
}
150152

151-
if ($PSBoundParameters.LocaleId) {
153+
if (Test-Bound -ParameterName LocaleId) {
152154
$param.LocaleId = $LocaleId
153155
}
154156

155-
if ($PSBoundParameters.Offset) {
157+
if (Test-Bound -ParameterName Offset) {
156158
$param.Offset = $Offset
157159
}
158160

159-
if ($PSBoundParameters.ParameterName) {
161+
if (Test-Bound -ParameterName ParameterName) {
160162
$param.ParameterName = $ParameterName
161163
}
162164

163-
if ($PSBoundParameters.Precision) {
165+
if (Test-Bound -ParameterName Precision) {
164166
$param.Precision = $Precision
165167
}
166168

167-
if ($PSBoundParameters.Scale) {
169+
if (Test-Bound -ParameterName Scale) {
168170
$param.Scale = $Scale
169171
}
170172

171-
if ($PSBoundParameters.Size) {
173+
if (Test-Bound -ParameterName Size) {
172174
$param.Size = $Size
173175
}
174176

175-
if ($PSBoundParameters.SourceColumn) {
177+
if (Test-Bound -ParameterName SourceColumn) {
176178
$param.SourceColumn = $SourceColumn
177179
}
178180

179-
if ($PSBoundParameters.SourceColumnNullMapping) {
181+
if (Test-Bound -ParameterName SourceColumnNullMapping) {
180182
$param.SourceColumnNullMapping = $SourceColumnNullMapping
181183
}
182184

183-
if ($PSBoundParameters.SourceVersion) {
185+
if (Test-Bound -ParameterName SourceVersion) {
184186
$param.SourceVersion = $SourceVersion
185187
}
186188

187-
if ($PSBoundParameters.SqlDbType) {
189+
if (Test-Bound -ParameterName SqlDbType) {
188190
$param.SqlDbType = $SqlDbType
189191
}
190192

191-
if ($PSBoundParameters.SqlValue) {
193+
if (Test-Bound -ParameterName SqlValue) {
192194
$param.SqlValue = $SqlValue
193195
}
194196

195-
if ($PSBoundParameters.TypeName) {
197+
if (Test-Bound -ParameterName TypeName) {
196198
$param.TypeName = $TypeName
197199
}
198200

199-
if ($PSBoundParameters.UdtTypeName) {
201+
if (Test-Bound -ParameterName UdtTypeName) {
200202
$param.UdtTypeName = $UdtTypeName
201203
}
202204

203-
if ($PSBoundParameters.Value) {
205+
if (Test-Bound -ParameterName Value) {
204206
$param.Value = $Value
205207
}
206208
$param

tests/New-DbaSqlParameter.Tests.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
3737
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Database tempdb -CommandType StoredProcedure -Query my_proc -SqlParameters $output
3838
$output.Value | Should -Be '{"example":"sample"}'
3939
}
40+
It "binds a 'falsy' value properly (see #9542)" {
41+
[int]$ZeroInt = 0
42+
$ZeroSqlParam = New-DbaSqlParameter -ParameterName ZeroInt -Value $ZeroInt -SqlDbType int
43+
$ZeroSqlParam.Value | Should -Be 0
44+
}
4045
}

0 commit comments

Comments
 (0)