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

当我尝试使用“like”手动从表中选择时

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

你设置了@TotalRecords

SELECT  @TotalRecords as N'@TotalRecords'

但使用@return_value

SELECT  'Return Value' = @return_value

也许你的意思是?

 SELECT  'Return Value' = @TotalRecords
于 2013-09-05T08:46:02.250 回答
0

我发现了问题

 @SearchStringName nchar(100)=null

当我将类型从nchar更改为nvarchar时,它可以正常工作:)

于 2013-09-05T09:54:23.057 回答