过程或函数是否需要未提供的参数?

时间:2016-01-23 19:09:49

标签: sql-server stored-procedures

当我运行下面的代码时,我收到一条错误消息:

  

Msg 201,Procedure stp_DespatchedJob,Line 0 Procedure or Function   'stp_DespatchedJob'需要参数'@JobStatusId',而不是   供给

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


ALTER PROCEDURE [dbo].[stp_DespatchedJob]
(
  @jobId as bigint,
  @DriverId int,
  @HasPda bit,
  @enablePda bit,
  @IsAdvanceJob bit,
  @IsAutodespatchedJob bit,
  @Controller varchar(100),
  @JobStatusId int

)
AS
Begin

     if(@enablepda=1 and @haspda=1)
     BEGIN
         UPDATE BOOKING
            SET BookingStatusId=@JobStatusId,JobOfferDateTime=getdate()
                ,DriverId=@DriverId,despatchdatetime=getdate()
                ,Despatchby = @Controller
         where id=@jobId


     END

     ELSE
     BEGIN

       declare @fleetMasterId int
        select @fleetMasterId=fleetMasterId from fleet_DriverQueueList where driverId=@driverId and status=1


         UPDATE BOOKING
            SET BookingStatusId=2,DriverId=@DriverId,despatchdatetime=getdate(),FleetMasterId=@FleetMasterId,Despatchby = @Controller
           where id=@jobId

      END


     if(@IsAdvanceJob=1 and  @IsAutodespatchedJob=1) 
     BEGIN

         Update booking_autodespatch set  despatched=1 where bookingId=@jobId
     END

End

这是什么意思?为什么我收到此错误?如何摆脱它?

1 个答案:

答案 0 :(得分:0)

由于所有过程参数都是必需的,因此有两种方法可以正确调用它:

1)直接(不推荐)

EXEC dbo.stp_DespatchedJob 1,2,1,1,1,1,'ctrl',7

2)使用named parameters (推荐)

EXEC [dbo].[stp_DespatchedJob]
  @jobId = 1,
  @DriverId = 2,
  @HasPda = 1,
  @enablePda = 1,
  @IsAdvanceJob = 1,
  @IsAutodespatchedJob = 1,
  @Controller = 'ctrl',
  @JobStatusId = 7

未能为所有参数提供值将导致类似于您收到的错误。如果您有一些几乎总是相同的值(例如JobStatusId = 1),您可以在过程原型中使用默认值指定它:

ALTER PROCEDURE [dbo].[stp_DespatchedJob]
(
  @jobId as bigint,
  @DriverId int,
  @HasPda bit,
  @enablePda bit,
  @IsAdvanceJob bit,
  @IsAutodespatchedJob bit,
  @Controller varchar(100),
  @JobStatusId int = 1
)
AS

并且您在调用过程时不再需要为@JobStatusId提供值。