0

I’m working with a stored procedure from a vendor, so I can’t update the procedure. The call I’m making requires a few parameters be passed as NULL. I’m using:

$sqlConnection = OpenConnection
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure
$sqlCmd.Connection = $sqlConnection
### Stored Procedure Name
$SqlCmd.CommandText = "procedure.name"
### Stored Procedure Parameters
$sqlCmd.Parameters.AddWithValue("@Param1", "SomeValue") | Out-Null
$sqlCmd.Parameters.AddWithValue("@Param2", DBNull.Value) | Out-Null
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 2
    Null in PowerShell is represented by `$null` – Daniel Oct 23 '22 at 01:42
  • 1
    So what happens when you try this? do you get an error? Everything in this post uses `[System.DBNull]::Value` https://stackoverflow.com/questions/22285149/dealing-with-system-dbnull-in-powershell – Nick.Mc Oct 23 '22 at 02:38
  • @Daniel when I try $null i get an exception error saying the function expects parameter '@param2', which was not supplied. – Something_Amusing Oct 23 '22 at 16:04
  • @Nick.McDermaid using DBNull.value just gave me an error saying there was a missing expression after the comma. But using the [System.DBNull]::Value worked. Thank you so much. Can you post that as an answer so I can accept it? – Something_Amusing Oct 23 '22 at 16:08
  • I can't take credit for my googling. If you don't mind I will mark this as duplicate. – Nick.Mc Oct 23 '22 at 22:55
  • Does this answer your question? [Dealing with System.DBNull in PowerShell](https://stackoverflow.com/questions/22285149/dealing-with-system-dbnull-in-powershell) – Nick.Mc Oct 23 '22 at 22:55
  • Does this answer your question? [SQL Insert from Powershell: null values inserted as "blank"](/q/9507669/90527) – outis Oct 27 '22 at 18:42
  • @Nick.McDermaid that did fix it. Please go ahead and mark it as duplicate. Thank you. I thought I updated this a few days ago but it doesn't appear I saved the comment. – Something_Amusing Oct 29 '22 at 03:31

0 Answers0