如何在where之后的存储过程中使用if条件
How to use if condition in stored procedure after where
我写了这个读取数据库数据的方法
string query = @"SELECT
IDproduct, name_product, first_price,
final_price, max_registered_price,
date_record_shamsi, final_date_view_shamsi,
count_views, image_1, collection_1, city, model
FROM
Table_khodro
WHERE
active = 0 ";
public DataSet ViewProductKhodro(string name, int firstPrice, int finalPrice, string subCollection, byte state,Int16 model)
{
if (name !="no name")
query += @" AND (name_product LIKE '%'+(@name_product)+'%')";
if (finalPrice != 0)
query += @" AND (first_price BETWEEN @first_price AND @final_price )";
if (subCollection !="انتخاب کنید")
query += @" AND (collection_1 = @collection_1 )";
if (state != 0)
query += @" AND (id_state = @id_state )";
if(model != 0)
query += @" AND (model = @model )";
SqlCommand cmd = new SqlCommand(query);
if (name != "no name")
{
cmd.Parameters.AddWithValue("@name_product", name);
}
if (finalPrice !=0)
{
cmd.Parameters.AddWithValue("@first_price", firstPrice);
cmd.Parameters.AddWithValue("@final_price", finalPrice);
}
if (subCollection != "انتخاب کنید")
cmd.Parameters.AddWithValue("@collection_1", subCollection);
if (state !=0)
cmd.Parameters.AddWithValue("@id_state", state);
if (model != 0)
cmd.Parameters.Add("@model",model );
// Fill the dataset
return FillDataSet(cmd, "Table_khodro");
}
现在我想为这个方法写一个存储过程。
一个存储过程可以吗?
怎么办?
请帮忙
您可以在查询中包含所有这些参数。
SELECT IDproduct ,
name_product ,
first_price ,
final_price ,
max_registered_price ,
date_record_shamsi ,
final_date_view_shamsi ,
count_views ,
image_1 ,
collection_1 ,
city ,
model
FROM Table_khodro
WHERE active = 0 AND (@NAME IS NULL OR NAME_PRODUCT LIKE @NAME)
AND (@FIRSTPRICE IS NULL OR FIRST_PRICE BETWEEN @FIRSTPRICE AND @FINALPRICE)
因此您的存储过程将具有所有必需的参数。您还必须传入那些您认为可能为 null 或可能不为 null 的参数。例如,如果@firstprice 为空,您的 sql 查询将在不考虑 First_price 列的情况下执行,否则它将使用中间价格并带来适当的结果集
希望对您有所帮助。
感谢您的回答
我这样更改我的方法:
public DataSet ViewProductKhodro(string name, int firstPrice, int finalPrice, string subCollection, byte state,Int16 model)
{ SqlCommand cmd = new SqlCommand();
cmd.CommandText="search_khodro";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name_product", name);
cmd.Parameters.AddWithValue("@first_price",firstPrice);
cmd.Parameters.AddWithValue("@final_price",finalPrice);
cmd.Parameters.AddWithValue("@collection_1",subCollection);
cmd.Parameters.AddWithValue("@id_state", state);
cmd.Parameters.Add("@model", model);
return FillDataSet(cmd, "Table_khodro");
}
然后像这样写一个存储过程:
ALTER PROCEDURE dbo.search_khodro
@name_product nvarchar(50),
@first_price int,
@final_price int,
@collection_1 nvarchar(30),
@id_state tinyint ,
@model smallint
AS begin SELECT IDproduct,name_product,first_price,final_price,max_registered_price,date_record_shamsi,final_date_view_shamsi, count_views,image_1,collection_1,city,model
from
Table_khodro
where
active=0 And ( @name_product IS NULL OR NAME_PRODUCT LIKE @name_product)
And (@first_price IS NULL OR first_price between @first_price AND @final_price )
And( @collection_1 IS NULL OR collection_1= @collection_1) And(@id_state IS NULL OR id_state=@id_state ) And (@model IS NULL OR model=@model)
end
此方法没有错误,但没有结果。请检查是否有问题?
我写了这个读取数据库数据的方法
string query = @"SELECT
IDproduct, name_product, first_price,
final_price, max_registered_price,
date_record_shamsi, final_date_view_shamsi,
count_views, image_1, collection_1, city, model
FROM
Table_khodro
WHERE
active = 0 ";
public DataSet ViewProductKhodro(string name, int firstPrice, int finalPrice, string subCollection, byte state,Int16 model)
{
if (name !="no name")
query += @" AND (name_product LIKE '%'+(@name_product)+'%')";
if (finalPrice != 0)
query += @" AND (first_price BETWEEN @first_price AND @final_price )";
if (subCollection !="انتخاب کنید")
query += @" AND (collection_1 = @collection_1 )";
if (state != 0)
query += @" AND (id_state = @id_state )";
if(model != 0)
query += @" AND (model = @model )";
SqlCommand cmd = new SqlCommand(query);
if (name != "no name")
{
cmd.Parameters.AddWithValue("@name_product", name);
}
if (finalPrice !=0)
{
cmd.Parameters.AddWithValue("@first_price", firstPrice);
cmd.Parameters.AddWithValue("@final_price", finalPrice);
}
if (subCollection != "انتخاب کنید")
cmd.Parameters.AddWithValue("@collection_1", subCollection);
if (state !=0)
cmd.Parameters.AddWithValue("@id_state", state);
if (model != 0)
cmd.Parameters.Add("@model",model );
// Fill the dataset
return FillDataSet(cmd, "Table_khodro");
}
现在我想为这个方法写一个存储过程。
一个存储过程可以吗?
怎么办?
请帮忙
您可以在查询中包含所有这些参数。
SELECT IDproduct ,
name_product ,
first_price ,
final_price ,
max_registered_price ,
date_record_shamsi ,
final_date_view_shamsi ,
count_views ,
image_1 ,
collection_1 ,
city ,
model
FROM Table_khodro
WHERE active = 0 AND (@NAME IS NULL OR NAME_PRODUCT LIKE @NAME)
AND (@FIRSTPRICE IS NULL OR FIRST_PRICE BETWEEN @FIRSTPRICE AND @FINALPRICE)
因此您的存储过程将具有所有必需的参数。您还必须传入那些您认为可能为 null 或可能不为 null 的参数。例如,如果@firstprice 为空,您的 sql 查询将在不考虑 First_price 列的情况下执行,否则它将使用中间价格并带来适当的结果集
希望对您有所帮助。
感谢您的回答 我这样更改我的方法:
public DataSet ViewProductKhodro(string name, int firstPrice, int finalPrice, string subCollection, byte state,Int16 model)
{ SqlCommand cmd = new SqlCommand();
cmd.CommandText="search_khodro";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name_product", name);
cmd.Parameters.AddWithValue("@first_price",firstPrice);
cmd.Parameters.AddWithValue("@final_price",finalPrice);
cmd.Parameters.AddWithValue("@collection_1",subCollection);
cmd.Parameters.AddWithValue("@id_state", state);
cmd.Parameters.Add("@model", model);
return FillDataSet(cmd, "Table_khodro");
}
然后像这样写一个存储过程:
ALTER PROCEDURE dbo.search_khodro
@name_product nvarchar(50),
@first_price int,
@final_price int,
@collection_1 nvarchar(30),
@id_state tinyint ,
@model smallint
AS begin SELECT IDproduct,name_product,first_price,final_price,max_registered_price,date_record_shamsi,final_date_view_shamsi, count_views,image_1,collection_1,city,model
from
Table_khodro
where
active=0 And ( @name_product IS NULL OR NAME_PRODUCT LIKE @name_product)
And (@first_price IS NULL OR first_price between @first_price AND @final_price )
And( @collection_1 IS NULL OR collection_1= @collection_1) And(@id_state IS NULL OR id_state=@id_state ) And (@model IS NULL OR model=@model)
end
此方法没有错误,但没有结果。请检查是否有问题?