tags:

views:

39

answers:

3

I have one table and a stored procedure(sp). I have to update the table using the stored procedure. At present I am using sp with four parameters but in the future may increase parameter list so, how can I update tge table without knowing parameters name and parameters count from Ado.Net.

Note: I don't know even single parameter name but I have to update that table by using SP and Ado.net.

A: 

Use Optional Parameters in your stored procedures to accomplish this.

Here is one without parameters

Create Procedure MyProcedure
AS

Update Table Set Column = 0

GO

Here is one with parameters that cannot be ignored

Create Procedure MyProcedure
    @MyColumnValue INTEGER
AS

Update Table Set Column = @MyColumnValue

GO

Here is one with one parameter that cannot be ignored, and one that can be ignored. If you do not send a value for @MyColumn2Value, it will use the default value.

Create Procedure MyProcedure
    @MyColumn1Value INTEGER,
    @MyColumn2Value INTEGER = -3
AS

Update Table 
Set 
    Column1 = @MyColumn1Value
    Column2 = @MyColumn2Value

GO

Thus, if you add to your procedure in the future, and you do not want to break existing code, you can add optional parameters like the one above.

Raj More
A: 

Here is a (database) function that returns table of parameter information of a given @RoutineName (Stored procedure name).

Create Function [dbo].[ftRoutineSchema](@RoutineName varchar(200)) returns table as return 
--declare @routineName varchar(100);select @routineName='ftBom4Excel'
SELECT   ColumnName=Case Is_Result
                                    When 'YES' then '@RC'
                                  else Parameter_Name
                            end
            ,DataType= case Data_Type
                            When  'DECIMAL' then 'Decimal('+convert(varchar,Numeric_precision)+','+Convert(varchar,Numeric_scale)+')'
                            When  'numeric' then 'Decimal('+convert(varchar,Numeric_precision)+','+Convert(varchar,Numeric_scale)+')'
                            when 'varchar' then 'Varchar('+Convert(varchar,Character_maximum_length)+')'
                            ELSE dATA_TYPE      
                          end
            ,ColumnOrder=Ordinal_Position   
            ,Direction =Case Parameter_Mode
                                when 'INOUT' then 'Out'
                                else Parameter_Mode
                            end

 FROM 
         Information_schema.Parameters  
  Where Specific_name=@ROUTINEnAME
TonyP
A: 

You can call SP without knowing of parameters name, just knowing parameters order.

For example, you have to call next sp: "EXEC my_sp @a, @b, @c"

using (var connection = new SqlConnection("MyConnectionString")
using (var command = connection.CreateCommand())
{
    command.CommandText = String.Format("EXEC my_sp {0}, {1}, {2}", "a", "b", "c");
    connection.Open();
    command.ExecuteNonQuery();
}
abatishchev