在存储过程中传递2个参数并一次使用一个参数

时间:2017-06-16 12:23:48

标签: c# sql

我有一个存储过程,可以提供我需要的结果。

Create Procedure [dbo].[usp_MTACardStatus]
(
@UID Varchar(50)
)
As
SELECT     mta.OrderNbr,mta.SerialNbr,mta.BatchNbr,sch.schoolnumber as  SchNbr,sch.schoolname as SchName,mti.shipmentdate as ShipmentDt,mti.ReceivedDate as ReceivedDt,s.studentid as StuID, i.IssueDT,mtst.MTACardStatusDesc as CardStatus,mtt.MTACardTypeDesc as CardType,p.firstname as FName,p.lastname as LName
FROM       MTACardInventory mta
      LEFT JOIN MTACardShipmentBlock mts on mts.BatchNbr = mta.BatchNbr and mts.OrderNbr = mta.OrderNbr
      LEFT JOIN MTACardShipment mti on mti.MTACardShipmentID = mts.MTACardShipmentID
      LEFT JOIN MTACardStatus mtst on mtst.MTACardStatusID = mta.MTACardStatusID
      LEFT JOIN MTACardType mtt on mtt.MTACardTypeID = mta.MTACardTypeID
      LEFT JOIN IDCard i on i.IDCardID = mta.IssuedTo_IDCardID
      LEFT JOIN Person p on p.PersonID = i.PersonID
      LEFT JOIN student s on s.PersonID = p.PersonID
      LEFT JOIN School sch on sch.schoolid = p.SchoolID
WHERE UID=@UID

我是否可以向同一个存储过程声明另一个参数'serialno',允许用户传递uid或序列号并获得相同的结果。

有办法吗?

3 个答案:

答案 0 :(得分:2)

如Lews所示,您需要传递序列号的另一个可选参数。然后在WHERE子句中,您需要引用附加的序列号参数。 WHERE子句必须检查任一参数是否为null,以便在提供UID时返回任何序列号,并在提供序列号时返回ANY UID。

Create Procedure [dbo].[usp_MTACardStatus] ( @UID Varchar(50) = NULL, @SerialNbr varchar(50) = NULL ) As SELECT mta.OrderNbr,mta.SerialNbr,mta.BatchNbr,sch.schoolnumber as SchNbr,sch.schoolname as SchName,mti.shipmentdate as ShipmentDt,mti.ReceivedDate as ReceivedDt,s.studentid as StuID, i.IssueDT,mtst.MTACardStatusDesc as CardStatus,mtt.MTACardTypeDesc as CardType,p.firstname as FName,p.lastname as LName FROM MTACardInventory mta LEFT JOIN MTACardShipmentBlock mts on mts.BatchNbr = mta.BatchNbr and mts.OrderNbr = mta.OrderNbr LEFT JOIN MTACardShipment mti on mti.MTACardShipmentID = mts.MTACardShipmentID LEFT JOIN MTACardStatus mtst on mtst.MTACardStatusID = mta.MTACardStatusID LEFT JOIN MTACardType mtt on mtt.MTACardTypeID = mta.MTACardTypeID LEFT JOIN IDCard i on i.IDCardID = mta.IssuedTo_IDCardID LEFT JOIN Person p on p.PersonID = i.PersonID LEFT JOIN student s on s.PersonID = p.PersonID LEFT JOIN School sch on sch.schoolid = p.SchoolID WHERE (UID=@UID OR @UID IS NULL) AND (mta.SerialNbr=@SerialNbr OR @SerialNbr IS NULL)

答案 1 :(得分:1)

是的,你可以。您可以同时制作@UID@serialno optional parameters

类似的东西:

Create Procedure [dbo].[usp_MTACardStatus]
(
    @UID Varchar(50) = NULL,
    @serialno Varchar(50) = NULL
)
As
SELECT     mta.OrderNbr,mta.SerialNbr,mta.BatchNbr,sch.schoolnumber as  SchNbr,sch.schoolname as SchName,mti.shipmentdate as ShipmentDt,mti.ReceivedDate as ReceivedDt,s.studentid as StuID, i.IssueDT,mtst.MTACardStatusDesc as CardStatus,mtt.MTACardTypeDesc as CardType,p.firstname as FName,p.lastname as LName
FROM       MTACardInventory mta
      LEFT JOIN MTACardShipmentBlock mts on mts.BatchNbr = mta.BatchNbr and mts.OrderNbr = mta.OrderNbr
      LEFT JOIN MTACardShipment mti on mti.MTACardShipmentID = mts.MTACardShipmentID
      LEFT JOIN MTACardStatus mtst on mtst.MTACardStatusID = mta.MTACardStatusID
      LEFT JOIN MTACardType mtt on mtt.MTACardTypeID = mta.MTACardTypeID
      LEFT JOIN IDCard i on i.IDCardID = mta.IssuedTo_IDCardID
      LEFT JOIN Person p on p.PersonID = i.PersonID
      LEFT JOIN student s on s.PersonID = p.PersonID
      LEFT JOIN School sch on sch.schoolid = p.SchoolID
WHERE (UID = @UID OR @UID IS NULL) AND (mta.SerialNbr = @SerialNbr OR @SerialNbr IS NULL)

答案 2 :(得分:-1)

没办法。传递给存储过程的值的数量必须与存储过程中声明的参数数量相匹配。您无法为接收两个不同参数的存储过程传递单个值。

如果您想要达到您的要求,唯一的方法是使用两个不同的存储过程。在您的情况下接受第一个参数和其他接受第二个参数。要执行此操作,您可以将其包含在try-catch块中。如果您传递的值与try块存储过程中的参数匹配,那么它将被执行。如果它不匹配,抛出异常然后执行catch块存储过程。

create procedure [dbo].[sp1]
    @UID varchar(50)
as
begin
    'your code'
end

create procedure [dbo].[sp2] 
    @SNo int
as
begin
    'your code'
end

begin try
    exec sp1 10
end try
begin catch
    exec sp2 10
end catch