如果参数为空,如何 return 所有值
How to return all values if Param is null
我想自定义此存储过程,以防万一@TownId 参数值为空 returns table 中的所有数据。否则 returns 正确的数据。
在我的 table 中,TownId 列中没有空值。有 nvarchar 值。
如果我从客户端发送@TownId=null,我没有成功获取所有数据
ALTER PROCEDURE dbo.GetCustomersPageWiseWithTown
@PageIndex INT = 1
,@PageSize INT = 10
,@PageCount INT OUTPUT
,@TownId int
AS
SET NOCOUNT ON;
SELECT ROW_NUMBER() OVER
(
order by IsPaid desc
)AS RowNumber
,b.Id
,b.Name
,b.Phone1
,b.Town
,b.Addess
,b.IsPaid
,b.DefaultImage
,t.TownName
INTO #Results
FROM BusinessFullData b
Left JOIN Towns t ON b.Town = t.Id
where ((b.IsVisable=1) and (b.Town=@TownId))
DECLARE @RecordCount INT
SELECT @RecordCount = COUNT(*) FROM #Results
SET @PageCount = CEILING(CAST(@RecordCount AS DECIMAL(10, 2)) / CAST(@PageSize AS DECIMAL(10, 2)))
PRINT @PageCount
SELECT * FROM #Results
WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1
DROP TABLE #Results
RETURN
非常感谢您的建议,
只需将该条件添加到您的 WHERE
子句中:
V-------------V
where ((b.IsVisable=1) and (@TownId IS NULL OR b.Town=@TownId))
另请注意 IsVisible
拼写错误。
处理可选参数的更好方法是这样的...
ALTER PROCEDURE dbo.GetCustomersPageWiseWithTown
@PageIndex INT = 1
,@PageSize INT = 10
,@PageCount INT OUTPUT
,@TownId INT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Sql NVARCHAR(MAX);
DECLARE @RecordCount INT;
SET @Sql = N'
SELECT
ROW_NUMBER() OVER (order by IsPaid desc)AS RowNumber
,b.Id
,b.Name
,b.Phone1
,b.Town
,b.Addess
,b.IsPaid
,b.DefaultImage
,t.TownName
INTO #Results
FROM BusinessFullData b
Left JOIN Towns t ON b.Town = t.Id
where b.IsVisable = 1 '
+ CASE WHEN @TownId IS NOT NULL
THEN N' and b.Town = @TownId ' ELSE N'' END
+ N' SELECT @RecordCount = COUNT(*) FROM #Results
SET @PageCount = CEILING(CAST(@RecordCount AS DECIMAL(10, 2)) / CAST(@PageSize AS DECIMAL(10, 2)))
SELECT * FROM #Results
WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1'
Exec sp_executesql @Sql
,N'@PageIndex INT , @PageSize INT, @TownId int,
@RecordCount INT, @PageCount INT OUTPUT'
,@PageIndex
,@PageSize
,@TownId
,@PageCount OUTPUT
END
我想自定义此存储过程,以防万一@TownId 参数值为空 returns table 中的所有数据。否则 returns 正确的数据。 在我的 table 中,TownId 列中没有空值。有 nvarchar 值。 如果我从客户端发送@TownId=null,我没有成功获取所有数据
ALTER PROCEDURE dbo.GetCustomersPageWiseWithTown
@PageIndex INT = 1
,@PageSize INT = 10
,@PageCount INT OUTPUT
,@TownId int
AS
SET NOCOUNT ON;
SELECT ROW_NUMBER() OVER
(
order by IsPaid desc
)AS RowNumber
,b.Id
,b.Name
,b.Phone1
,b.Town
,b.Addess
,b.IsPaid
,b.DefaultImage
,t.TownName
INTO #Results
FROM BusinessFullData b
Left JOIN Towns t ON b.Town = t.Id
where ((b.IsVisable=1) and (b.Town=@TownId))
DECLARE @RecordCount INT
SELECT @RecordCount = COUNT(*) FROM #Results
SET @PageCount = CEILING(CAST(@RecordCount AS DECIMAL(10, 2)) / CAST(@PageSize AS DECIMAL(10, 2)))
PRINT @PageCount
SELECT * FROM #Results
WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1
DROP TABLE #Results
RETURN
非常感谢您的建议,
只需将该条件添加到您的 WHERE
子句中:
V-------------V
where ((b.IsVisable=1) and (@TownId IS NULL OR b.Town=@TownId))
另请注意 IsVisible
拼写错误。
处理可选参数的更好方法是这样的...
ALTER PROCEDURE dbo.GetCustomersPageWiseWithTown
@PageIndex INT = 1
,@PageSize INT = 10
,@PageCount INT OUTPUT
,@TownId INT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Sql NVARCHAR(MAX);
DECLARE @RecordCount INT;
SET @Sql = N'
SELECT
ROW_NUMBER() OVER (order by IsPaid desc)AS RowNumber
,b.Id
,b.Name
,b.Phone1
,b.Town
,b.Addess
,b.IsPaid
,b.DefaultImage
,t.TownName
INTO #Results
FROM BusinessFullData b
Left JOIN Towns t ON b.Town = t.Id
where b.IsVisable = 1 '
+ CASE WHEN @TownId IS NOT NULL
THEN N' and b.Town = @TownId ' ELSE N'' END
+ N' SELECT @RecordCount = COUNT(*) FROM #Results
SET @PageCount = CEILING(CAST(@RecordCount AS DECIMAL(10, 2)) / CAST(@PageSize AS DECIMAL(10, 2)))
SELECT * FROM #Results
WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1'
Exec sp_executesql @Sql
,N'@PageIndex INT , @PageSize INT, @TownId int,
@RecordCount INT, @PageCount INT OUTPUT'
,@PageIndex
,@PageSize
,@TownId
,@PageCount OUTPUT
END