我有一个存储过程。在这个存储过程中,我必须检查特定参数是否不为空。我怎样才能做到这一点?我写了这个:
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 中返回一个空数据集。