2012-10-22 23 views
0

我寫一個存儲過程來獲取基於CATEGORY ID傳遞給它的細節,如果傳遞給它的CATEGORY ID不是空的,我基於CATEGORY ID取,如果CATEGORY ID是null我提取CATEGORY ID3之外的所有細節,誰能幫我解決這個問題。存儲過程來獲取資產詳細信息基於輸入參數

我的程序是這樣的:

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 '%'[email protected]+'%' 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; 
+0

什麼DBMS你使用mysql,oracle,sql-server? – Parado

+0

我正在使用MY Sql –

回答

0

由於在where子句中,你投入到其他參數可以不匹配輸入category id作爲null3取代AND條件。

首先,選擇具有其他參數的記錄,看看它們中的任何一個是否獲取了具有類別標識3的記錄。 我建議您向我們展示具有類別標識爲3的記錄,以便更好地瞭解數據。

否則我相信沒有錯誤。

+0

雅我發現解決方案後,從數據庫中提取的細節我過濾了使用LINQ的基於類別ID的結果,如var res = from assetDetails其中asset.categoryID!= 3選擇資產。 –