1

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行を返さないのですか?

4

2 に答える 2

0

問題が見つかりました

 @SearchStringName nchar(100)=null

タイプをncharからnvarcharに変更すると、問題なく動作します:)

于 2013-09-05T09:54:23.057 に答える
0

@TotalRecords を設定します

SELECT  @TotalRecords as N'@TotalRecords'

ただし、@return_value を使用します

SELECT  'Return Value' = @return_value

おそらくあなたが意味したのですか?

 SELECT  'Return Value' = @TotalRecords
于 2013-09-05T08:46:02.250 に答える