StoredProcedureが1つあります
Create PROCEDURE [dbo].[NewsSelect]
@Name nchar(40)=null,
@PublishDate date=null,
@SearchStringName nchar(100)=null,
@Sort char(1)='1',
@Order bit=1,
@startRowIndex int = 1,
@pageSize int = 25,
@TotalRecords int Output
AS
begin
set rowcount @pageSize;
With newsSelect(ID, Name, PublishDate, [Status],RowNumber) as(
select
ID,
Name,
PublishDate,
[Status],
RowNumber
from
(select
ROW_NUMBER() over(order by
case when @Sort = '1' and @Order=1 then News.Name end asc,
case when @Sort = '1' and @Order=0 then News.Name end desc,
case when @Sort = '2' and @Order=1 then News.PublishDate end asc,
case when @Sort = '2' and @Order=0 then News.PublishDate end desc,
case when @Sort = '3' and @Order=1 then News.[Status] end asc,
case when @Sort = '3' and @Order=0 then News.[Status] end desc)as RowNumber,
News.ID,
News.Name as Name,
News.PublishDate,
News.[Status]
from [News]
where (@SearchStringName is null or(@SearchStringName is not null and ( Name like '%'+@SearchStringName+'%' )))
group by News.ID, News.Name, News.PublishDate,News.[Status]) as temp)
select * from newsSelect where newsSelect.rowNumber> @startRowIndex;
With newsSelect1(ID, Name, PublishDate, [Status],RowNumber) as( select
ID,
Name,
PublishDate,
[Status],
RowNumber
from
(select
ROW_NUMBER() over(order by
case when @Sort = '1' and @Order=1 then News.Name end asc,
case when @Sort = '1' and @Order=0 then News.Name end desc,
case when @Sort = '2' and @Order=1 then News.PublishDate end asc,
case when @Sort = '2' and @Order=0 then News.PublishDate end desc,
case when @Sort = '3' and @Order=1 then News.[Status] end asc,
case when @Sort = '3' and @Order=0 then News.[Status] end desc)as RowNumber,
News.ID,
News.Name as Name,
News.PublishDate,
News.[Status]
from News
where (@SearchStringName is null or(@SearchStringName is not null and ( Name like '%'+@SearchStringName+'%' )))
group by News.ID, News.Name, News.PublishDate,News.[Status]) as temp)
select @TotalRecords = Count(*) from newsSelect1
END
「いいね」で手動でテーブルから選択しようとしたとき
USE [tableName]
GO
SELECT [ID]
,[Name]
FROM [dbo].[News]
where News.[Name] like '%'+ 'te' +'%'
GO
結果は 2 行返されます
21 Test 1
22 Test 3
しかし、ストアプロシージャをテストすると
USE [tableName]
GO
DECLARE @return_value int,
@TotalRecords int
EXEC @return_value = [dbo].[NewsSelect]
@Name = NULL,
@PublishDate = NULL,
@SearchStringName = N'te',
@Sort = NULL,
@Order = NULL,
@startRowIndex = 0,
@pageSize = 25,
@TotalRecords = @TotalRecords OUTPUT
SELECT @TotalRecords as N'@TotalRecords'
SELECT 'Return Value' = @return_value
GO
0 を返します どこに問題がありますか? なぜ2行を返さないのですか?