在我的项目中,我想搜索输入值,我想我应该使用动态Sql创建一个SP。当我在MVC中调用此SP时,它返回“int”。我希望它返回列为ProductName,ProductCat ....示例:
create SearchProduct
(@ProductId int =null,@ProductName nvarchar(50) = null,@ProductCatId int = null,.......)
as
declaredeclare @SqlStr nvarchar(max)
declare @ParamList nvarchar(2000)
set @SqlStr ='select p.ProductId,p.ProductName,p.ProductCatName,p.From,p.To from Product p where (1=1)'
-- p.from , p. to is warranty period
if @ProductName is not null
set @SqlStr = 'and p.ProductName like ''%' +@ProductName + '%'' )'
......
set @ParamList ='@ProductId2,@ProductName2,@ProductCat2,....'
execute sp_executesql @sqlStr,@ParamList,@ProductId,@ProductName,@ProductCat,....
在MVC中,我将SP称为:
var temp = db.SearchProduct(ProductId,ProductName,ProductCat,....)
// in here, var temp = int temp.