如何确定参数值是否传递给存储过程
我想创建一个存储过程(在 SQL Server 2008 R2 中),它将根据表的 PK 更新表中的记录。
例如,存储过程将具有四个参数:
@ID int,
@Name nvarchar(50),
@Email nvarchar(80),
@Phone nvarchar(20)
如何确定存储过程的调用者是否为一个(或多个)参数传递了 NULL 值,以及调用者是否没有为一个(或多个)参数传递任何值。更多)参数?
C# 呼叫者示例:
呼叫者为 @Phone
指定 NULL
:
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "EditPerson";
cmd.Parameters.AddWithValue("@ID", id);
cmd.Parameters.AddWithValue("@Name", 'Frank');
cmd.Parameters.AddWithValue("@Email", '[email protected]');
cmd.Parameters.AddWithValue("@Phone", DBNull.Value);
DatabaseManager.instance.ExecuteScalarQuery(cmd);
}
呼叫者忽略 @Phone
参数:
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "EditPerson";
cmd.Parameters.AddWithValue("@ID", id);
cmd.Parameters.AddWithValue("@Name", 'Frank');
cmd.Parameters.AddWithValue("@Email", '[email protected]');
DatabaseManager.instance.ExecuteScalarQuery(cmd);
}
我在这里想要完成的是,如果调用者显式为参数指定 NULL 值,然后我将使用 NULL 值更新记录。但是,如果用户明确忽略传递参数,则 UPDATE
查询将保留已为特定记录设置的字段/列的值(即查询不会更新该特定列) 。
我想我可以指定默认值,可以安全地假设调用者永远不会使用 - 像这样:
@ID int,
@Name nvarchar(50) = 'NameIsUndefined',
@Email nvarchar(80) = 'EmailIsUndefined',
@Phone nvarchar(20) = 'PhoneIsUndefined'
然后,在存储过程中,我可以检查未定义的值 - 如果参数变量仍然设置为 NameIsUndefine
、EmailIsUndefine
和/或 PhoneIsUndefine
值,那么我可以安全地假设调用者没有显式定义这些参数的值。这是实现我的目标的唯一方法吗?
I want to create a stored procedure (in SQL Server 2008 R2) that will update a record in a table based on the table's PK.
The stored proc will have, for example, four parameters:
@ID int,
@Name nvarchar(50),
@Email nvarchar(80),
@Phone nvarchar(20)
How can I determine if the caller of the stored proc passes a NULL value for one (or more) of the parameters vs. if the caller didn't pass anything for one (or more) of the parameters?
C# caller example:
Caller specifies NULL
for @Phone
:
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "EditPerson";
cmd.Parameters.AddWithValue("@ID", id);
cmd.Parameters.AddWithValue("@Name", 'Frank');
cmd.Parameters.AddWithValue("@Email", '[email protected]');
cmd.Parameters.AddWithValue("@Phone", DBNull.Value);
DatabaseManager.instance.ExecuteScalarQuery(cmd);
}
Caller ignores the @Phone
parameter:
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "EditPerson";
cmd.Parameters.AddWithValue("@ID", id);
cmd.Parameters.AddWithValue("@Name", 'Frank');
cmd.Parameters.AddWithValue("@Email", '[email protected]');
DatabaseManager.instance.ExecuteScalarQuery(cmd);
}
What I'm trying to accomplish here is, if the caller explicitly specifies a NULL value for a parameter, then I will update the record with a NULL
value. However, if the user explicitly ignores passing a parameter, then the UPDATE
query will retain the value of the field/column that is already set for the particular record (i.e. the query will NOT update that particular column).
I suppose that I could specify default values that can be safely assumed that a caller will never use - something like this:
@ID int,
@Name nvarchar(50) = 'NameIsUndefined',
@Email nvarchar(80) = 'EmailIsUndefined',
@Phone nvarchar(20) = 'PhoneIsUndefined'
Then, in the stored proc, I can check for the undefined values - if the parameter vars are still set to the NameIsUndefined
, EmailIsUndefined
, and/or PhoneIsUndefined
values, then I can safely assume that the caller did not explicitly define values for those params. Is this the only way to accomplish my goal?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您像这样声明参数(没有默认值),则存储过程将需要所有四个参数,并且如果其中任何一个参数未传递给提供程序组成的 EXEC 语句,则存储过程将失败。
您可以将某些参数声明为可选,如下所示:
@Phone nvarchar(20) = NULL
但是,无法在存储过程内部判断它是否被省略或显式设置为
NULL
。If you declare your parameters like this (without default value), the stored proc will require all four of them and will just fail if any of them will not be passed to the
EXEC
statement composed by the provider.You may declare some of the parameters optional like this:
@Phone nvarchar(20) = NULL
however, there will be no way to tell inside the sproc if it was omitted or explicitly set to
NULL
.AFAIK,没有办法区分 SQL Server 中的 NULL 和 NULL。
在你的 C# 代码中,我会 A) 传递另一个值,例如空字符串,以指示传递了一个空值,然后在 SQL 中处理该值,如果传递了该值,则将 NULL 写入数据库,或者如果传递了该值,则保留先前的值变量为 NULL,或 B) 不传递 DBNull.Value,而是传递从数据库读取的先前值。
There's no way to tell the difference between NULL and NULL in SQL Server, AFAIK.
In your C# code, I would A) Pass another value, like an empty string, to indicate an empty value was passed, then process that in SQL to write NULL to the DB if the value was passed, or retain the previous value if the variable is NULL, or B) Instead of passing
DBNull.Value
, pass the previous value that was read from the DB.