存储过程仅返回2个参数的结果

时间:2013-08-11 03:42:47

标签: sql-server-2008 stored-procedures

下面的朋友是我的代码

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为null时,它会返回预期的结果,但是当我尝试在@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的参数传递时,它返回零 我的语法有问题吗?

3 个答案:

答案 0 :(得分:2)

您尚未指定varchar参数的大小。

  ,@fname varchar
  ,@lname varchar

不这样做会给你一个大小。

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

根据您的情况更改为适当的内容。

  ,@fname varchar(100)
  ,@lname varchar(100)

答案 1 :(得分: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 = '')

或两者兼而有之。

答案 2 :(得分: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。

希望它有所帮助。