我有一个 MS SQL Server 脚本,我在其中创建了一个临时存储过程,当传递两个患者标识符时,它会选择并打印第二个患者的详细信息。然后,它使用这些标识符和详细信息来调用另一个存储过程以将这两个患者合并在一起。
该脚本在这里的几个测试数据库上完美运行,但是当我在繁忙的站点上运行相同的脚本时,看起来好像我选择那些患者详细信息的局部变量“偶尔”没有及时填充以进行打印,但是合并过程的后续执行总是让它们填充得很好。大多数情况下,PRINT 正确显示了这些值。
这是我正在做的精简版。你怎么看?
CREATE PROCEDURE [dbo].[TMP_MergeMRNs] @Merge_From_Medical_Record_Number VARCHAR(64) = NULL,
@Merge_From_Institution_Name VARCHAR(64) = NULL,
@Merge_To_Medical_Record_Number VARCHAR(64) = NULL,
@Merge_To_Institution_Name VARCHAR(64) = NULL
AS
BEGIN
SET NOCOUNT ON
SET DEADLOCK_PRIORITY LOW
/* ALL parameters must be supplied */
IF @Merge_From_Medical_Record_Number IS NULL
OR @Merge_From_Institution_Name IS NULL
OR @Merge_To_Medical_Record_Number IS NULL
OR @Merge_To_Institution_Name IS NULL
BEGIN
PRINT 'ERROR. One or more of the 4 required fields was empty.'
RETURN 4
END
/* Retrieve the merge_to patient demographics */
DECLARE @lstNm NVARCHAR(64),
@frstNm NVARCHAR(64),
@mdlNm NVARCHAR(64)
SELECT @lstNm = id1.LastName,
@frstNm = id1.FirstName,
@mdlNm = id1.MiddleName
FROM [dbo].[PATIENT] pid1
INNER JOIN [dbo].[INSTITUTION] i1
ON pid1.InstitutionDBKey = i1.InstitutionDBKey
WHERE i1.InstitutionName = @Merge_To_Institution_Name
AND pid1.MedicalRecordNumber = @Merge_To_Medical_Record_Number
PRINT LTrim('Merge From Patient: ' + RTrim(@Merge_From_Medical_Record_Number)) + ' / ' + LTRIM(RTRIM(@Merge_From_Institution_Name))
PRINT LTrim('Merge To Patient: ' + RTrim(@Merge_To_Medical_Record_Number)) + ' / ' + LTRIM(RTRIM(@Merge_To_Institution_Name))
PRINT ' Name: ' + ISNULL(LTRIM(RTRIM(@lstNm)), '') + IsNull(', ' + LTRIM(RTRIM(@frstNm)), '') + IsNull(' ' + LTRIM(RTRIM(@mdlNm)), '')
EXEC PatientMerge
@Merge_From_Medical_Record_Number,
@Merge_From_Institution_Name,
@Merge_To_Medical_Record_Number,
@Merge_To_Institution_Name,
/* last */ @lstNm,
/* first */ @frstNm,
/* middle */ @mdlNm
RETURN 0
END
GO