Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get stored procedure output parameter into variable using Powershell?

I have a stored procedure returning a string and I need the result as a powershell variable. I'm stuck on the output parameter syntax.

Powershell script:

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=myserver;Database=mydb;Integrated Security=True"
$SqlConnection.Open()
$SqlConnection.State

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "testsp3"
$SqlCmd.Connection = $SqlConnection


$op = new-object System.Data.SqlClient.SqlParameter;
$op.ParameterName = "@answer";
$op.Direction = [System.Data.ParameterDirection]'Output';
$op.DbType = [System.Data.DbType]'String';
$op.Size = 2500

$SqlCmd.Parameters.Add($op);

$what = $SqlCmd.ExecuteScalar();

$what

Error message:

Exception calling "ExecuteScalar" with "0" argument(s): "Procedure or function 'testsp3' expects parameter '@answer', which was not supplied."

Stored procedure if it matters:

CREATE PROCEDURE [dbo].[testsp3]
@answer nvarchar(max) output

AS
BEGIN
    SET NOCOUNT ON;

    SELECT @answer = 'blah blah blah'
    RETURN
END
GO
like image 985
scw Avatar asked Jun 17 '15 18:06

scw


People also ask

How do you access an output parameter from a stored procedure?

The easy way is to right-click on the procedure in Sql Server Management Studio (SSMS), select 'Execute stored procedure..." and add values for the input parameters as prompted. SSMS will then generate the code to run the procedure in a new query window, and execute it for you.

How do you call a stored procedure with parameters in PowerShell?

For mapping parameters that come from user input in a stored procedure to a stored procedure's parameters, we can use the SqlParameter object in PowerShell and call the add method to our command object. We can use the SqlParameter object in . NET with the sp_executesql function in SQL Server as well.

Can stored procedure have output parameter?

The Output Parameters in Stored Procedures are used to return some value or values. A Stored Procedure can have any number of output parameters. The simple logic is this — If you want to return 1 value then use 1 output parameter, for returning 5 values use 5 output parameters, for 10 use 10, and so on.


1 Answers

Modify your script to be like below

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=myserver;Database=mydb;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "testsp3"
$SqlCmd.Connection = $SqlConnection
$SqlCmd.CommandType = [System.Data.CommandType]'StoredProcedure'; <-- Missing
$outParameter = new-object System.Data.SqlClient.SqlParameter;
$outParameter.ParameterName = "@answer";
$outParameter.Direction = [System.Data.ParameterDirection]'Output';
$outParameter.DbType = [System.Data.DbType]'String';
$outParameter.Size = 2500;
$SqlCmd.Parameters.Add($outParameter) >> $null;
$SqlConnection.Open();
$result = $SqlCmd.ExecuteNonQuery();
$truth = $SqlCmd.Parameters["@answer"].Value;
$SqlConnection.Close();
$truth;
like image 142
Rahul Avatar answered Sep 18 '22 19:09

Rahul