Without going too much into the situation that has caused the need for this to happen... See towards end of question.
I am calling a stored procedure sp_that_I_dont_controll which may or may not (depending upon the day of the week) accept @p_some_parameter. Is there a way to tell the SQL Parameter object that a given parameter is optional and if the stored procedure doesn't accept a parameter with the given name, just ignore it?
(I'm trying to have @p_some_parameter added as a permenant optional parameter to the query, but I'm looking for a stop gap til then.)
Seems like I will have to go into the background as to why this is needed.
There are three software products that use this stored procedure.
A, B, and C.
A uses it and only works when it has the following parameters.
PROCEDURE [dbo].[sp_example]
@p_a varchar(8),
@p_b varchar(8)
OR
PROCEDURE [dbo].[sp_example]
@p_a varchar(8),
@p_b varchar(8) = NULL
B uses it and only works when it has the following parameter.
PROCEDURE [dbo].[sp_example]
@p_a varchar(8)
B does not work when the following is used:
PROCEDURE [dbo].[sp_example]
@p_a varchar(8),
@p_b varchar(8) = NULL
This is a problem is product B, but I don't have control over it.
On days when A is being used, the DBA changes it so the stored procedure accepts @p_a and @p_b. On days when B is being used, the DBA changes it so the stored procedure only accepts @p_a.
I want to make C work regardless of which stored procedure (it doesn't need to pass b in to work).
Possible solution:
New stored procedure: rejected, DBA doesn't want me adding more.
Try catch that calls with @p_a and @p_b and if it fails calls only with @p_a: works, but it is an ugly solution I'm trying to avoid.
Check what paramters the current stored procedure accepts and use that to control if I add @p_b or not: works, looks better, but means I'm not using the 'official' data access libraries (honestly, I would love to drop the entire data access library we are using, as it offers 30+ ways to call a stored procedure, the majority of which break when optional sql parameters are added).
Edit 1: Added description of the problem resulting in this need. Edit 2: Added a suggested solution from the comments.