如何将动态参数传递给存储过程

时间:2022-06-22 15:34:16

I have designed my layout completely dynamic based upon a table's columns. Suppose when the user adds a new column in a table, then without modifying any logic it will show that column on the page.

我根据表格的列设计了完全动态的布局。假设当用户在表中添加新列时,如果不修改任何逻辑,它将在页面上显示该列。

As far selection it's working fine. But I facing hurdle in saving data in the database.

到目前为止,它的工作正常。但我面临着在数据库中保存数据的障碍。

How I can save this data, because I do not specify the parameter in stored procedure that will fulfill my requirement. I want to pass it dynamically.

我如何保存这些数据,因为我没有在存储过程中指定满足我要求的参数。我想动态传递它。

1 个解决方案

#1


You can pass TABLE parameter to the stored procedure. So you can create a table type something like this

您可以将TABLE参数传递给存储过程。所以你可以创建一个像这样的表类型

 CREATE TYPE LIST_OF_PARAMS AS TABLE (id int, NAME VARCHAR) 

then you have to alter your procedure to accept a variable of type LIST_OF_PARAMS.

然后你必须改变你的程序以接受LIST_OF_PARAMS类型的变量。

Something like this:

像这样的东西:

CREATE TYPE LIST_OF_PARAMS AS TABLE (id int, NAME VARCHAR) 
go 
CREATE PROCEDURE Myproc @Mytable LIST_OF_PARAMS READONLY,
                       @id     INT
AS
  BEGIN

Also note that:

另请注意:

Note that in SQL Server 2008 table valued parameters are read only. But as you notice we actually require you to write READONLY. So that actually then means that at some point in the future maybe if you say please, please please often enough we might be able to actually make them writable as well at some point. But at the moment they are read only.

请注意,在SQL Server 2008中,表值参数是只读的。但是你注意到我们实际上要求你写READONLY。所以这实际上就意味着在未来的某个时刻,如果你说请,请经常请我们在某些时候能够真正使它们成为可写的。但目前他们只读。

#1


You can pass TABLE parameter to the stored procedure. So you can create a table type something like this

您可以将TABLE参数传递给存储过程。所以你可以创建一个像这样的表类型

 CREATE TYPE LIST_OF_PARAMS AS TABLE (id int, NAME VARCHAR) 

then you have to alter your procedure to accept a variable of type LIST_OF_PARAMS.

然后你必须改变你的程序以接受LIST_OF_PARAMS类型的变量。

Something like this:

像这样的东西:

CREATE TYPE LIST_OF_PARAMS AS TABLE (id int, NAME VARCHAR) 
go 
CREATE PROCEDURE Myproc @Mytable LIST_OF_PARAMS READONLY,
                       @id     INT
AS
  BEGIN

Also note that:

另请注意:

Note that in SQL Server 2008 table valued parameters are read only. But as you notice we actually require you to write READONLY. So that actually then means that at some point in the future maybe if you say please, please please often enough we might be able to actually make them writable as well at some point. But at the moment they are read only.

请注意,在SQL Server 2008中,表值参数是只读的。但是你注意到我们实际上要求你写READONLY。所以这实际上就意味着在未来的某个时刻,如果你说请,请经常请我们在某些时候能够真正使它们成为可写的。但目前他们只读。