简体   繁体   English

我在存储过程的脚本 sql 中收到返回值错误

[英]i get an error with return value in my script sql of stored proc

I get 'A TOP or FETCH clause contains an invalid value' this error我收到“A TOP 或 FETCH 子句包含无效值”这个错误

" Msg 1014, Niveau 15, État 1, Procédure dbo.sp_contacts_getcontacts, Ligne 24 [Ligne de départ du lot 0] A TOP or FETCH clause contains an invalid value. " " Msg 1014, Niveau 15, État 1, Procédure dbo.sp_contacts_getcontacts, Ligne 24 [Ligne de départ du lot 0] TOP 或 FETCH 子句包含无效值。"

I want to get contacts using stored procedure , this is the code of the SP我想使用存储过程获取联系人,这是SP的代码

( @Id     uniqueidentifier  = NULL,
    @role   nvarchar(max)     = NULL,
    @IdCompany    uniqueidentifier  = NULL,
    @Active   bit     = 0, 
    @Page  int    = NULL,
    @PerPage   int  = NULL,


)
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 * 
     FROM [dbo].[Contacts] 
     LEFT JOIN [dbo].[Companies]    
     ON Contacts.[IdCompany] =  Companies.[IdCompany]
     WHERE
          (@Id is null or @Id = [IdContact])
    AND   (@role is null  or @role=contacts.[Role])
    AND   (@IdCompany  is null or @IdCompany  = [IdCompany])
    AND   (@Active = 0 or @Active = [Active])

    ORDER BY [IdContact] OFFSET ((@Page - 1) * @PerPage) ROWS FETCH NEXT @PerPage ROWS ONLY

END

I could be misunderstanding your question.我可能误解了你的问题。 If you want to grab the results from that select, you'll need a table variable that matches the output of the sproc to Insert Into.如果您想从该选择中获取结果,您将需要一个与插入到的 sproc 输出匹配的表变量。

DECLARE @someTable TABLE(
    /* Whatever the output is of the sproc */
)

INSERT INTO @someTable
EXEC @return_value = dbo.nameOfYourSproc

You could also try creating a temp table from the results您也可以尝试从结果中创建一个临时表

INSERT INTO #someTable
EXEC @return_value = dbo.nameOfYourSproc

Although, my recommendation is to turn that SPROC into a table-valued function.不过,我的建议是将该 SPROC 转换为表值函数。

代码是正确的,我只是没有以这种方式插入页面和 perpage 的值,它显示错误“FETCH ...”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM