存储过程以根据输入参数获取资产详细信息

时间:2012-10-22 10:20:56

标签: mysql

我正在编写一个存储过程来根据传递给它的CATEGORY ID获取详细信息,如果传递给它的CATEGORY ID不为null,我将基于CATEGORY ID获取,如果CATEGORY ID为空我获取CATEGORY ID除了3之外的所有详细信息,任何人都可以帮我解决此问题。

我的程序如下:

CREATE procedure [dbo].[SearchAssetdetails]
(
@assetCategory as int = null,
@assetType as int = null,
@assetDescription as nvarchar(200) = null,
@purchaseDate as datetime = null,
@validUpto as datetime = null)
as
begin
select ad.CategoryID,ad.AssetTypeId,ad.AssetDetailId,ad.AssetDescription,ad.Cost,ad.IsOwn,ad.LastModifiedby,ad.LastModifiedDatetime,ad.Location,ad.NoofLicences,ad.PurchaseDate,ad.SerialNumber,ad.ValidUpto,ad.VendorId,ad.Version,ad.WarrantyExpirationDate
 from AssetDetails ad where      
(ad.AssetDescription like (case when @assetDescription is not null then  '%'+@assetDescription+'%' else ad.AssetDescription end)        
and ad.CategoryID=(case when @assetCategory is not null then @assetCategory else ad.CategoryID   end)        
and ad.AssetTypeId=(case when @AssetType is not null then @AssetType else ad.AssetTypeId end)        
and ad.PurchaseDate=(case when @purchaseDate is not null then @purchaseDate else ad.PurchaseDate end)        
and ad.ValidUpto=(case when @validUpto is not null then @validUpto else ad.ValidUpto end)
 )  end;

1 个答案:

答案 0 :(得分:0)

由于where子句中的AND条件,您对其他参数的输入可能与category id输入null不匹配,但替换为3

首先,选择包含其他参数的记录,看看是否有任何参数提取类别为3的记录。 我建议你向我们展示类别ID为3的记录,以便更好地了解数据。

否则我相信没有错误。