如何检查参数在sql server中不为空?

时间:2012-09-19 12:39:23

标签: sql sql-server stored-procedures dataset

我有一个存储过程。在此存储过程中,我必须检查特定参数是否为空。我怎样才能做到这一点?我写了这个:

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中返回一个null数据集。

4 个答案:

答案 0 :(得分:3)

每当您将字符串和NULL连接在一起时,结果为NULL。当你询问变量是否为NULL时,你已经完成了这个:

' + @locationUrlIdentifier + '

好几次。如果是NULL,那么@Sql也是。

您可能需要考虑使用COALESCENULL替换为合适的替代品(例如空字符串):

' + 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)

答案 1 :(得分:1)

您可以在一个查询中执行此操作:

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

答案 2 :(得分:0)

答案 3 :(得分:0)

或者,您可以使用ISNULL()检查,然后将null更改为空字符串

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

或者甚至在此检查之前,如果问题仍然存在,您可以使用ISNULL()NULL转换为空字符串