1
votes

I have a Powershell script which I want to execute a SQL Server stored procedure. The stored procedure has a parameter named @backupType which of a CHAR(1) type. I'm calling it like this:

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=(local);Database=master;Integrated Security=True"
$SqlConnection.Open()
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "sp_myStoredProcedure"
$SqlCmd.Connection = $SqlConnection

$SqlCmd.Parameters.Add("@backupType", [System.Data.SqlDbType]"Char", 1)
$SqlCmd.Parameters["@backupType"].Value = "F"

$result = $SqlCmd.ExecuteNonQuery()
$SqlConnection.Close()

The error I get is:

Exception calling "ExecuteNonQuery" with "0" argument(s): "Procedure or function 'sp_myStoredProcedure' expects parameter '@backupType', which was not supplied." At line:14 char:1

  • $result = $SqlCmd.ExecuteNonQuery()
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    • FullyQualifiedErrorId : SqlException

I can only assume I'm specifying the parameter wrong but a Google and search of SO has not surfaced anything helpful. Any ideas?

1
Is $SqlCmd.Parameters a hashtable/dictionary?Maximilian Burszley
The code looks OK to me, is this your actual code?DavidG
@TheIncorrigible1 I don't think so. I expect it's a collection of strongly typed parameters but I'm not 100% sure.Tom Troughton
@DavidG I removed two other parameters which are both string parameters and occur earlier in the SP. Since the error specified the char param I assumed the issue was with my syntax but maybe the error is misleading? I've triple-checked for typos etc.Tom Troughton

1 Answers

1
votes

try with

$SqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure
$SqlCmd.Parameters.Add("@backupType", [System.Data.SqlDbType]::Char, 1).Value="F"