1

When we try to execute a stored procedure without sending values to it's required parameter , it gives an error like below.

enter image description here

I wanna know, is there any way to problematically capture the all parameters for a given stored procedure ?

Eg: Parr[] string = GetSpParameters("StoredProcedureName); //I need to develop the "GetSpParameters" function.

Thanks.

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
tishantha
  • 487
  • 5
  • 13
  • 34
  • I wanna get a list of parameters from a given sp in C#, not in SQL server. – tishantha Dec 18 '14 at 02:52
  • 2
    The "duplicate" question covers how to get a list of parameters for a proc, but it doesn't cover the real need of this question, which was how to get a list of _**required**_ parameters (i.e., parameters without a default value) for a proc. For that, see this answer: https://stackoverflow.com/a/1676272 – Troy Gizzi Jun 22 '17 at 22:27

2 Answers2

4

In SQL Server terms, a required parameter is any parameter that does not have a default value, e.g.:

CREATE PROCEDURE Foo
(
  @Required INT,
  @NotRequired INT = NULL
)

In this case, you can call the procedure without the @NotRequired parameter, but you can't call the procedure without the @Required one -- you'll get the error in your question.

You can use SqlCommandBuilder.DeriveParameters on a SqlCommand object you've set up to populate the Parameters collection, but that will not tell you the difference between optional and required parameters. You could to just assume that all stored procedure parameters are required, but that's a terrible idea. Stored procedures very often allow you to pass various combinations of parameters, and behave appropriately. For example, a "search" stored procedure may take a big list of parameters but only require you to specify the ones you really want to search for; or a stored procedure may have default values for certain parameter and only expect you to specify explicit values if you want to change the behavior.

To actually tell the difference, you need to use the SQL Server SMO API. This should be installed as part of the SQL Server client tools setup, you'll need to add a reference to several assemblies: Microsoft.SqlServer.ConnectionInfo, Microsoft.SqlServer.Management.Sdk, and Microsoft.SqlServer.Smo.

From there, the basic idea is:

var server = new Server("instancename");
server.ConnectionContext.DatabaseName = "DatabaseName";
server.ConnectionContext.LoginSecure = true;
server.ConnectionContext.Connect();

var db = server.Databases["databasename"];
var proc = new StoredProcedure(db, "storedprocedurename");

foreach (StoredProcedureParameter parameter in sp.Parameters)
{
    if (parameter.DefaultValue != null)
    {
      // param is required.
    }
}

You can see more information in this answer and this question and this MSDN article. In particular, that second question talks about ways to speed up this process by telling SMO not to load some information you won't care about.

Community
  • 1
  • 1
Michael Edenfield
  • 28,070
  • 4
  • 86
  • 117
  • Michael, I am getting an error 'Microsoft.SqlServer.Management.Server' does not contain a definition for 'Connect', I also checked the [MSDN](https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.server.aspx) page for the 'Server' class, it does not have a method with that name, please explain. – Karim AG Mar 09 '15 at 12:55
  • yes, sorry, it was a transcription error. it's `ConnectionContext.Connect()`. – Michael Edenfield Mar 09 '15 at 18:35
3

You can use SqlCommandBuilder.DeriveParameters method to populate the Parameters collection of the specified SqlCommand object.

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
  • 1
    Thank you. it works. here is my code. ---------------------- SqlCommandBuilder.DeriveParameters(SqlCmd); foreach (SqlParameter parameter in SqlCmd.Parameters) { } ---------------------- thanks. – tishantha Dec 18 '14 at 03:11
  • note that this doesn't actually tell you if the parameters are required or optional; that information is significantly harder to get. I'll have an answer forthcoming on that. – Michael Edenfield Dec 18 '14 at 03:13