使用SELECT中的参数执行存储过程

时间:2012-02-15 12:58:29

标签: sql sql-server-2005 stored-procedures

我有一个SP需要执行多次,因为我在临时表中找到行(就像使用select语句插入一样)

我的表@OutMessageQIDsid列(比方说10行) SP HL7_InsertComponentLog需要5个参数,如下所示

EXEC [HL7_InsertComponentLog] --@iPraID,@iComponentID,@vComponentType,'Send',id
SELECT @iPrcID,@iComponentID,@vComponentType,'Send',id
FROM @OutMessageQIDs

我可以不使用使用CursorLoop类型的内容吗?

编辑1:更多解释 - @OutMessageQIDs是一个临时表,用于存储QueueTable(在SP中)排队的项目ID。并且SP HL7_InsertComponentLog记录队列编辑的项目。根据@vComponentType,它会记录不同类型的数据。

编辑2: SP如下:

Create Procedure [dbo].[HL7_InsertComponentLog]
    @IPracID [int],
    @iComponentID [bigint],
    @vComponentType varchar(50),
    @vStatus varchar(200),
    @iOutMessageQueueID [bigint]
AS
    select* from hl7_outmessagelog

    IF @vStatus != 'Success'
    BEGIN
        SELECT -1
    END
    ELSE IF @vComponentType = 'LabOrder' OR @vComponentType = 'ProcedureOrder' OR @vComponentType = 'RadiologyOrder' 
    BEGIN 
        SELECT -1
    END
    ELSE IF @vComponentType = 'LabResult' OR @vComponentType = 'ProcedureResult' 

    BEGIN       
        INSERT INTO OrderResult_Addendum (iOrderDetailID,IUserID,DateTime_Stamp,iType,VchComments,iOrderID,iPracID,vAction,bAcknowledge)
                                SELECT   NULL,0,dTimeStamp,NULL,NULL,@iComponentID,@iPracID,@vStatus,0
                                FROM HL7_OutMessageQueue Q 
                                WHERE Q.iOutQueueID = @iOutMessageQueueID and iPracID = @iPracID
    END
    ELSE IF @vComponentType = 'RadiologyResult'
    BEGIN 
        INSERT INTO OrderResult_Addendum (iOrderDetailID,IUserID,DateTime_Stamp,iType,VchComments,iOrderID,iPracID,vAction,bAcknowledge)
                                SELECT   iOrderDetailID,0,Q.dTimeStamp,NULL,NULL,@iComponentID,@iPracID,@vStatus ,0
                                FROM HL7_OutMessageQueue Q 
                                INNER JOIN OrderResultDetails det ON Q.iComponentID = det.iOrderID
                                WHERE Q.iOutQueueID = @iOutMessageQueueID and Q.iPracID = @iPracID
    END
    ELSE IF @vComponentType = 'ClinicalNotes' 
    BEGIN       

        INSERT INTO Note_provider_encounter(iReportID,iUserID,iComponentID,dEncounterDate,vaction)
                                    SELECT  @iComponentID,0,0,dTimeStamp,@vStatus 
                                    FROM HL7_OutMessageLog Where iOutMessageLogID = @iOutMessageQueueID and iPracID = @iPracID
    END 
    ELSE IF @vComponentType = 'PatientDemo' 
    BEGIN       
        DECLARE @IPatID int
        DECLARE @IUserID int
        SELECT @IPatID = iPatID,@IUserID = iUserID
        FROM HL7_OutMessageQueue Q 
                                WHERE Q.iOutQueueID = @iOutMessageQueueID and iPracID = @iPracID
        EXEC [dbo].[InsertPatientLog] @IPracID,@IPatID,@vStatus,@IUserID
    END

1 个答案:

答案 0 :(得分:5)

如果不使用某种循环,不能为表中的每一行EXEC INSERT INTO ComponentLog SELECT @iPrcID,@iComponentID,@vComponentType,'Send',id FROM @OutMessageQIDs 存储过程。

您的存储过程有何功能?有可能从中提取逻辑并以基于集合的方式对整个表执行。

e.g。只需使用

{{1}}

而不是为每一行调用存储过程。