Skip to content

Comments

Add support for named INPUT/OUTPUT parameters in stored procedures#43

Open
Mr-Kaos wants to merge 8 commits intoelvanto:masterfrom
Mr-Kaos:named-ouput-params
Open

Add support for named INPUT/OUTPUT parameters in stored procedures#43
Mr-Kaos wants to merge 8 commits intoelvanto:masterfrom
Mr-Kaos:named-ouput-params

Conversation

@Mr-Kaos
Copy link
Contributor

@Mr-Kaos Mr-Kaos commented Aug 7, 2025

Stored procedures have the ability to set a parameter as an output, essentially acting as a means to return a value from a procedure.
Additionally, some SQL flavours (e.g. MSSQL) allow for named parameter placement in stored procedure and do not require parameters to be placed in the order of their definition.

A method to execute a stored procedure using named parameters with/without an output parameter would allow for the retrieval of an output from a stored procedure.

The addition I have proposed introduces a new executeNamed() function, which allows the execution of a query using named parameters instead of positional ones (such as in the execute() function).

It can be used as follows (examples are using MSSQL T-SQL syntax):

// Create a simple stored procedure that multiples parameters 1 and 2 and sets the output to parameter 3 (output parameter)
$this->db->execute('CREATE OR ALTER PROCEDURE [dbo].[usp_testMultiply] @ParamA INT, @ParamB INT, @ParamC INT OUTPUT AS BEGIN SET @ParamC = @ParamA * @ParamB; END');

// Variable to store the output in. It must be of the same data data type the procedure accepts for its parameter, in this case `INT`.
$output = 0;
// Execute The stored procedure using named parameters. Notice how the output parameter is using a reference to `$output`.
$this->db->executeNamed('EXEC [usp_testMultiply] :ParamA, :ParamB, :ParamC', ['ParamA' => 5, 'ParamB' => 10], ['ParamC' => &$output]);

echo $output; // This should output 50 (5 * 10)

I have included unit tests for the MSSQL driver to verify this functionality works as intended.

The reason for this feature addition is to allow support for PDO OUT and INOUT parameters.
However, there is an issue exclusive to MySQL with this:

From (The only proper) PDO tutorial:

Calling a stored procedure is a rare case where bindParam() use is justified, as it's the only way to handle OUT and INOUT parameters. However, for mysql it doesn't work. You have to resort to an SQL variable and an extra call.

Mr-Kaos added 7 commits May 16, 2025 10:35
The MSSQL trust server certificate option now requires a boolean value.
- Added function to execute query with named parameters
- Implemented output parameter storage
* Add table tests for MSSQL driver
* Add stored procedure tests for MSSQL driver
- Updated MSSQL unit tests
Copy link
Member

@joshmcrae joshmcrae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mr-Kaos Thanks for implementing this, especially tests for MSSQL. Did you manage to run them during development? If so, could you perhaps update our docker-compose.yml to run the MSSQL preview container?

I think that since output is broken for MySQL we should throw an exception if an output param is bound when the driver is Mysql. Even an InvalidArgumentException would make sense to protect the use-case that we don't yet support.

@Mr-Kaos
Copy link
Contributor Author

Mr-Kaos commented Nov 1, 2025

@joshmcrae I have added SQL Server to the dockerfile and tested it to ensure that it can run the tests correctly. That being said, this is my first time playing with docker, but I believe everything should be in working order.

I have set it to use SQL Server 2019, but SQL Server 2017 can be used instead if you want a smaller container size.

I also noticed that there are some GitHub workflows defined for MySQL and PostgreSQL. I'm also not familiar with workflows and am unsure if one for SQL Server needs to be added or not.

@joshmcrae
Copy link
Member

Thanks for these changes! I've copied some of them into #45 and added Mssql to the GitHub workflow.

Once that PR is merged, you can merge down to your branch and I'll re-review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants