简体   繁体   中英

How to Update database through stored procedure without knowing parameter names?

I have a table and a stored procedure. I have to update the table using the stored procedure. At present I am using stored procedure with four parameters but in the future the parameter list might grow. How can I update the 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 stored procedure and Ado.net.

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.

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

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM