在另一个存储过程中执行存储过程

时间:2018-03-28 05:58:21

标签: sql-server

我正在从主存储过程执行另一个存储过程。主存储过程如下:

    ALTER Procedure [dbo].[SaveLabTestEntry] 
    (
     @LE_No         nvarchar(100)=null,
     @hcp_id        varchar(25)=null,
     @Pat_prof_Id   nvarchar(100)=null,
     @Usr_Id        nvarchar(100)=null,
     @LE_Date       nvarchar(100)=null,
     @LE_Time       nvarchar(100)=null,
     @HospitalName  nvarchar(100)=null,
     @Mode          nvarchar(10)=null,
     @dt    LabEntryD_TVP   READONLY    
     )
AS
Declare  @ResultMsg as nvarchar(50)
Declare  @MaxID as Int
Declare  @Status as Int
Begin
    BEGIN TRY
        BEGIN TRANSACTION SaveData
        IF @Mode='ADD'
            BEGIN           

                IF @HospitalName IS NOT NULL AND @HospitalName<>''
                    BEGIN
                        EXEC  @hcp_id= InsertPatAddedHosp @HospitalName, @Usr_Id 
                    END             

                SET @MaxID = (SELECT [Vr_No] + 1 FROM [VrLastNo] WHERE [Vr_Code]='LAB-H' AND [Loc_Id]=1 AND [Co_Cd]='HO')
                SET @LE_No = 'P'+CAST(@MaxID AS NVARCHAR(100))
                INSERT  INTO HCPLabEntryH(LE_No,    hcp_id,     Pat_prof_Id,    Usr_Id,     LE_Date,    LE_Time,    Entry_Type)
                VALUES                   (@LE_No,   @hcp_id,    @Pat_prof_Id,   @Usr_Id,    @LE_Date,   @LE_Time,   'P')

                INSERT INTO HCPLabEntryD(
                            hcp_id,             Pat_prof_Id,            LE_No,      LED_Ctr,        Test_Cd,    Test_Desc,              
                            LED_Result,         LED_Normal_Value)                           
                SELECT  
                            @hcp_id,            @Pat_prof_Id,           @LE_No,     tvp.LabTestCtr, tvp.TestCd, tvp.TestName,
                            tvp.TestResult,     tvp.NormalValue                         

                FROM @dt tvp 

                SET @ResultMsg='Saved Successfully..'
                Set @Status=1

                UPDATE [VrLastNo] SET [Vr_No]= @MaxID WHERE [Vr_Code]='LAB-H' AND [Loc_Id]=1 AND [Co_Cd]='HO'           
            END
        IF @Mode='EDIT'
            BEGIN

                IF @HospitalName IS NOT NULL AND @HospitalName<>''
                    BEGIN
                        EXEC  @hcp_id= InsertPatAddedHosp @HospitalName, @Usr_Id
                    END     

                UPDATE HCPLabEntryH SET 
                        hcp_id=@hcp_id,     Pat_prof_Id=@Pat_prof_Id,   Usr_Id=@Usr_Id,     LE_Date=@LE_Date,   LE_Time=@LE_Time
                WHERE LE_No=@LE_No

                DELETE FROM HCPLabEntryD WHERE LE_No=@LE_No

                INSERT INTO HCPLabEntryD(
                            hcp_id,             Pat_prof_Id,            LE_No,      LED_Ctr,        Test_Cd,    Test_Desc,              
                            LED_Result,         LED_Normal_Value)                           
                SELECT  
                            @hcp_id,            @Pat_prof_Id,           @LE_No,     tvp.LabTestCtr, tvp.TestCd, tvp.TestName,
                            tvp.TestResult,     tvp.NormalValue                         

                FROM @dt tvp 

                Set @Status=1
                SET @ResultMsg='Updated Successfully..'
            END
        IF @Mode='DELETE'
            BEGIN
                DELETE FROM HCPLabEntryH WHERE LE_No=@LE_No
                DELETE FROM HCPLabEntryD WHERE LE_No=@LE_No

                Set @Status=1
                SET @ResultMsg='Deleted Successfully..'
            END
    SELECT @ResultMsg as ResultMsg,@Status as [Status],0 as Id
    COMMIT TRANSACTION SaveData
END TRY
BEGIN CATCH
    rollback transaction SaveData;
    Set @Status=0
    select Error_Message() as ResultMsg,@Status as [Status],0 as Id
END CATCH 
End

下面给出第二个存储过程;它返回@HcpId从主sp中获取此@HcpId并尝试从主sp中保存。但它没有获得@HcpId。怎么做到这一点?还有其他解决办法吗?

    ALTER PROCEDURE [dbo].[InsertPatAddedHosp]
    (
    @HospitalName nvarchar(100),
    @UserId nvarchar(100)
)
As

Begin

    DECLARE @MaxHcpId int
    DECLARE @HcpId nvarchar(25)

    IF NOT EXISTS (SELECT Hcp_Name FROM HCPPatAdded WHERE [User_Id]=@UserId AND Hcp_Name=@HospitalName)
        BEGIN
            SET @MaxHcpId= (SELECT ISNULL((SELECT ISNULL([VALUE],0) FROM [Param] WHERE [KEY]='PatAddedHosId'),0))
            SET @HcpId='H'+CAST((@MaxHcpId+1) AS NVARCHAR(100))

            IF @MaxHcpId = 0
                BEGIN
                    INSERT INTO [Param]([KEY], [YEAR], [VALUE]) VALUES ('PatAddedHosId', -1, 1)
                END
            ELSE
                BEGIN
                    UPDATE [Param] SET [VALUE]=@MaxHcpId+1 WHERE [KEY]='PatAddedHosId'
                END

            INSERT INTO HCPPatAdded(Hcp_Id, Hcp_Name, [User_Id]) VALUES(@HcpId, @HospitalName, @UserId)

            SELECT @HcpId
        END
    ELSE
        BEGIN
            SELECT Hcp_Id FROM HCPPatAdded WHERE [User_Id]=@UserId AND Hcp_Name=@HospitalName
        END
End

我的问题来自Main sp

中的以下行
IF @HospitalName IS NOT NULL AND @HospitalName<>''
                BEGIN
                    EXEC  @hcp_id= InsertPatAddedHosp @HospitalName, @Usr_Id 
                END   

1 个答案:

答案 0 :(得分:3)

您应该为此使用OUTPUT参数。这是一个非常简单的示例,说明如何将可变参数从一个proc传递到另一个proc:

CREATE PROCEDURE innerProcedure
@user_id INT, 
@hcp_id INT OUTPUT
AS 
BEGIN
    IF @user_id = 1
        SET @hcp_id = 100
    ELSE
        SET @hcp_id = 200
END
GO

CREATE PROCEDURE outerProcedure
@user_id INT
AS
BEGIN
    DECLARE @hcp_id INT
    EXEC innerProcedure @user_id, @hcp_id OUTPUT
    SELECT @hcp_id
END
GO

尝试使用params 1和除1之外的任何东西:

EXEC outerProcedure 1

返回100

EXEC outerProcedure 2

返回200

注意outerProcedure将@hcp_id传递给innerProcedure,innerProcedure使用SET操作修改其值,然后将值传递回outerProcedure。请注意,在编写获取参数的过程时以及随时调用它时,需要包括OUTPUT。

我认为你需要做出这些改变:

ALTER PROCEDURE [dbo].[InsertPatAddedHosp]
(
    @HospitalName nvarchar(100),
    @UserId nvarchar(100),
    @HcpId nvarchar(25) OUTPUT
)
As

Begin

    DECLARE @MaxHcpId int

    IF NOT EXISTS (SELECT Hcp_Name FROM HCPPatAdded WHERE [User_Id]=@UserId AND Hcp_Name=@HospitalName)
        BEGIN
            SET @MaxHcpId= (SELECT ISNULL((SELECT ISNULL([VALUE],0) FROM [Param] WHERE [KEY]='PatAddedHosId'),0))
            SET @HcpId='H'+CAST((@MaxHcpId+1) AS NVARCHAR(100))

            IF @MaxHcpId = 0
                BEGIN
                    INSERT INTO [Param]([KEY], [YEAR], [VALUE]) VALUES ('PatAddedHosId', -1, 1)
                END
            ELSE
                BEGIN
                    UPDATE [Param] SET [VALUE]=@MaxHcpId+1 WHERE [KEY]='PatAddedHosId'
                END

            INSERT INTO HCPPatAdded(Hcp_Id, Hcp_Name, [User_Id]) VALUES(@HcpId, @HospitalName, @UserId)
        END
    ELSE
        BEGIN
            SELECT @HcpId = Hcp_Id FROM HCPPatAdded WHERE [User_Id]=@UserId AND Hcp_Name=@HospitalName
        END
End

然后,而不是这个:

EXEC  @hcp_id= InsertPatAddedHosp @HospitalName, @Usr_Id 

请致电:

EXEC  InsertPatAddedHosp @HospitalName, @Usr_Id, @hcp_id OUTPUT

此外,将@hcp_id的数据类型更改为nvarchar(25)而不是varchar(25)以匹配InsertPatAddedHosp中的数据类型。