0

大家好,下面是我的代码

USE [arrestedpersonsdb]
GO
/****** Object:  StoredProcedure [dbo].[stnencodedtodisplay]    Script Date: 08/11/2013 11:18:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[stnencodedtodisplay]
(
      @PageIndex INT = 1
      ,@PageSize INT = 10
      ,@RecordCount INT OUTPUT 
      ,@id int
      ,@fname varchar
      ,@lname varchar
    )
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here

      SELECT ROW_NUMBER() OVER
      (
            ORDER BY [fname] ASC
      )AS RowNumber
      ,[pid]
      ,[fname]
      ,[mname]
      ,[lname]
      ,[qualifier]
      ,[alias]

      INTO #Results
      FROM [todisplay]
      where (stnid = @id) and (type = 'STN')  and (fname = @fname or @fname = '') and (          lname = @lname or @lname = '')
      SELECT @RecordCount = COUNT(*)
      FROM #Results

      SELECT * FROM #Results
      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1

      DROP TABLE #Results
END

奇怪的是,当@fname 和@lname 为空时,它返回预期的结果,但是当我尝试在@fname 或@lname 上传递参数时,它什么也不返回

下面的示例

USE [arrestedpersonsdb]
GO

DECLARE @return_value int,
        @RecordCount int

EXEC    @return_value = [dbo].[stnencodedtodisplay]
        @PageIndex = 1,
        @PageSize = 10,
        @RecordCount = @RecordCount OUTPUT,
        @id = 1599,
        @fname = 'ALDRIN',
        @lname = ''

SELECT  @RecordCount as N'@RecordCount'

SELECT  'Return Value' = @return_value

GO
enter code here

但是当我将“ALDRIN”作为@fname 的参数传递时,它返回零是我的语法有问题吗?

4

3 回答 3

2

You have not specified the size of the varchar parameters.

  ,@fname varchar
  ,@lname varchar

Not doing so will give you a size of one.

  ,@fname varchar(1)
  ,@lname varchar(1)

Change to whatever is appropriate in your case.

  ,@fname varchar(100)
  ,@lname varchar(100)
于 2013-08-11T04:41:59.517 回答
0

这可能是一个上限问题。如果是这样,那么以下将解决它:

(upper(fname) = upper(@fname) or @fname = '') and 
(upper(lname) = upper(@lname) or @lname = '')

或者它可能是一个空白问题,在这种情况下,以下将解决它

(rtrim(ltrim(fname)) = rtrim(ltrim(@fname)) or @fname = '') and 
(rtrim(ltrim(lname)) = rtrim(ltrim(@lname)) or @lname = '')

或两者。

于 2013-08-11T03:54:32.863 回答
0

我可能迟到了,但解决这个问题的另一个选择是试试这个:

SELECT ROW_NUMBER() OVER
  (
        ORDER BY [fname] ASC
  )AS RowNumber
  ,[pid]
  ,[fname]
  ,[mname]
  ,[lname]
  ,[qualifier]
  ,[alias]

  INTO #Results
  FROM [todisplay]
  where stnid = @id 
    and type = 'STN'  
    AND CASE WHEN  @fname =''
          THEN 'True' 
          ELSE fname 
            END  = CASE WHEN   @fname ='' 
                    THEN 'True' 
                    ELSE @fname 
            END 
    AND CASE WHEN  @lname =''
          THEN 'True' 
          ELSE lname 
            END  = CASE WHEN   @lname ='' 
                    THEN 'True' 
                    ELSE @lname 
            END  

在您当前尝试设置 SET ANSI_NULLS OFF。

希望能帮助到你。

于 2013-08-23T12:31:10.913 回答