1

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

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

4

4 回答 4

3

每当您将字符串和NULLs 连接在一起时,结果就是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)
于 2012-09-19T12:48:30.330 回答
1

你可以在一个查询中做到这一点:

Select Query ...where ... 
  and ((@locationUrlIdentifier is null) or (CategoryOrder = 1))
于 2012-09-19T12:48:07.443 回答
0

您可以使用 NULLIF 而不是 IS NULL

参考:检查存储过程中的参数是否为空或为空

http://msdn.microsoft.com/en-us/library/ms177562.aspx

于 2012-09-19T12:50:56.460 回答
0

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

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

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

于 2012-09-19T12:54:32.450 回答