3

I'm completely new to Powershell so i'm a little confused about how to call a SQL procedure that takes parameters. I have opened a connection to my database successfully and i've managed to get a procedure that doesn't take parameters to work so I know that the connection is fine.

The code to add a parameter and run the query is below:

$dateToUse = Get-Date -f yyyy/MM/dd
$MysqlQuery.CommandText = "GetJourneyByDepartureDate"
$MysqlQuery.Parameters.AddWithValue("_departureDate", $dateToUse)
$queryOutput = $MysqlQuery.ExecuteReader()

Whenever I try and run my script I get an error saying

Incorrect number of arguments for PROCEDURE dbo.GetJourneyByDepartureDate; expected 1, got 0

I've had a look around trying to find a solution but I don't understand enough about Powershell to know what solutions might be correct.

Also I am unable to post the SQL query but I have managed to run the procedure many times by just running the query in HeidiSQL passing the arguement manually

EDIT:

I've now changed my code slightly, it now looks like this:

$MysqlQuery.CommandText = "GetJourneyByDepartureDate"
$MysqlQuery.Parameters.Add("@_departureDate", [System.Data.SqlDbType]::Date) | out-Null
$MysqlQuery.Parameters['@_departureDate'].Value = $dateToUse
$parameterValue = $MysqlQuery.Parameters['@_departureDate'].value

Write-Host -ForegroundColor Cyan -Object "$parameterValue";
$queryOutput = $MysqlQuery.ExecuteReader()

I'm getting the $dateToUse value output on the console in the Write-Host line but i'm still getting the same Incorrect number of arguments error as before. SP is declared as below:

CREATE PROCEDURE `GetJourneyByDepartureDate`(IN `_departureDate` DATE) READS SQL DATA
4
  • Are you using MySQL Connector/Net from PowerShell? Commented Dec 10, 2015 at 16:47
  • I don't think so, though tbh I don't really know Commented Dec 10, 2015 at 18:14
  • 1
    Try: $MysqlQuery.Parameters.AddWithValue("@departureDate", $dateToUse). Commented Dec 10, 2015 at 18:30
  • What does the '@' symbol mean?? Commented Dec 11, 2015 at 9:01

1 Answer 1

2

In the end I found that I needed to set the CommandType to be StoredProcedure and also I needed to add the parameter but I was missing the direction and I apparently had to add a space after the '@' but i'm not sure why. My solution is below:

    $MysqlCommand = New-Object MySql.Data.MySqlClient.MySqlCommand.Connection = $connMySQL               #Create SQL command
    $MysqlCommand.CommandType = [System.Data.CommandType]::StoredProcedure;                              #Set the command to be a stored procedure

    $MysqlCommand.CommandText = "GetJourneyByDepartureDate";                                         #Set the name of the Stored Procedure to use
    $MysqlCommand.Parameters.Add("@ _departureDate", [System.Data.SqlDbType]::Date) | out-Null;          #Set the input and output parameters
    $MysqlCommand.Parameters['@ _departureDate'].Direction = [system.data.ParameterDirection]::Input;    #Set the _departureDate parameter to be an input parameter
    $MysqlCommand.Parameters['@ _departureDate'].Value = $dateToUse;                                     #Set the _departureDate parameter value to be dateToUse   
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.