2012-09-19 216 views
1

我有一個存儲過程。在這個存儲過程中,我必須檢查一個特定的參數是否爲空。我怎樣才能做到這一點?我寫到:如何在sql server中檢查參數是否爲空?

ALTER PROCEDURE [dbo].[GetReelListings] 
    @locationUrlIdentifier VARCHAR(100) 
AS 
BEGIN 

SET NOCOUNT ON; 

declare @Sql varchar(max)='' 

SET @Sql = 'SELECT CategoryName, CategoryUrlIdentifier, LocationUrlIdentifier, Directory.* FROM (SELECT ROW_NUMBER() OVER (PARTITION BY Category.Name ORDER BY CASE WHEN '''+ @locationUrlIdentifier + ''' = Location.UrlIdentifier THEN 1 ELSE CASE WHEN ''' + @locationUrlIdentifier + ''' IS NULL AND Directory.LocationId IS NULL THEN 0 ELSE 2 END END, Directory.SortOrder) AS ''RowNo'', Category.Name AS CategoryName, Category.UrlIdentifier AS CategoryUrlIdentifier, dbo.Location.UrlIdentifier AS LocationUrlIdentifier, Directory.DirectoryId, CASE WHEN ''' + @locationUrlIdentifier + ''' = Location.UrlIdentifier THEN 1 ELSE CASE WHEN ''' + @locationUrlIdentifier + ''' IS NULL AND Directory.LocationId IS NULL THEN 0 ELSE 2 END END AS CategoryOrder FROM dbo.Directory INNER JOIN dbo.Category ON Directory.CategoryId = Category.CategoryId LEFT OUTER JOIN dbo.Location ON dbo.Directory.LocationId = location.Location_ID) AS content INNER JOIN dbo.Directory ON content.DirectoryId = Directory.DirectoryId WHERE content.RowNo =1 ' 

if (@locationUrlIdentifier is null) 
begin 
SET @Sql = @Sql + ' and 1=1' 
end 
else 
begin 
SET @Sql = @Sql + ' and CategoryOrder = 1 ' 
end 

print @SQl 
EXECUTE (@Sql) 
END 

這將在SQL中工作,但這將在Codebehind中返回一個空數據集。

+1

什麼是錯誤 – codingbiz

+0

這將在後面的數據集的代碼沒有什麼回報,從而爲null –

+2

更好,如果你能前給完整的腳本。然後很容易找到問題 – Prasanna

回答

3

只要你加入字符串和NULL s ^在一起,結果是NULL。通過你問的變量是否是NULL的時候,你已經做到了這一點:

' + @locationUrlIdentifier + ' 

幾次。如果是NULL@Sql也是。

你可能要考慮使用COALESCE更換NULL用合適的替代(例如一個空字符串):

' + COALESCE(@locationUrlIdentifier,'') + ' 

您也仍然有你的最終建設一個邏輯錯誤。如果變量是NULL,你就會有一個where子句說法:

WHERE content.RowNo =1 1=1 

這是無效的。我認爲你不應該追加任何東西。


我也不清楚爲什麼你這樣做的動態SQL。下面的似乎是可以直接執行的等效查詢:

SELECT 
    CategoryName, 
    CategoryUrlIdentifier, 
    LocationUrlIdentifier, 
    Directory.* 
FROM 
    (SELECT 
     ROW_NUMBER() OVER (
      PARTITION BY Category.Name ORDER BY 
       CASE 
        WHEN @locationUrlIdentifier = Location.UrlIdentifier THEN 1 
        WHEN @locationUrlIdentifier IS NULL AND Directory.LocationId IS NULL THEN 0 
        ELSE 2 
       END, 
       Directory.SortOrder 
     ) AS RowNo, 
     Category.Name AS CategoryName, 
     Category.UrlIdentifier AS CategoryUrlIdentifier, 
     dbo.Location.UrlIdentifier AS LocationUrlIdentifier, 
     Directory.DirectoryId, 
     CASE 
      WHEN @locationUrlIdentifier = Location.UrlIdentifier THEN 1 
      WHEN @locationUrlIdentifier IS NULL AND Directory.LocationId IS NULL THEN 0 
      ELSE 2 
     END AS CategoryOrder 
    FROM 
     dbo.Directory 
      INNER JOIN 
     dbo.Category 
      ON 
       Directory.CategoryId = Category.CategoryId 
      LEFT OUTER JOIN 
     dbo.Location 
      ON 
       dbo.Directory.LocationId = location.Location_ID 
    ) AS content 
     INNER JOIN 
    dbo.Directory 
     ON 
      content.DirectoryId = Directory.DirectoryId 
WHERE 
    content.RowNo =1 and 
    (@locationUrlIdentifier or CategoryOrder = 1) 
+0

對不起,我忘了寫和1 = 1 –

+0

@ABVyas - 沒有必要添加'和1 = 1'到一個否則形成良好的'WHERE'子句。 –

1

你可以這樣做只是在一個查詢:

Select Query ...where ... 
    and ((@locationUrlIdentifier is null) or (CategoryOrder = 1)) 
0

或者您可以使用ISNULL()檢查並將空值更改爲空字符串

IF (ISNULL(@locationUrlIdentifier,'') = '') 

甚至這個檢查可以使用ISNULL()NULL轉換爲空字符串,如果它繼續存在是一個問題

相關問題