2012-09-19 213 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转换为空字符串,如果它继续存在是一个问题

相关问题