SQL Server - 确定锁定数据库的内容

时间:2017-02-22 17:11:57

标签: sql-server stored-procedures

我有一个正在执行插入的存储过程。但是,一旦我执行该存储过程,锁就会保留在数据库中,并且不能再进行插入。事实上,你甚至无法在桌面上做出选择。

当我在Management Studio中执行调用时,似乎报告了两个选项,但存储过程中只有一个。

所以我想知道是否有一个我可以使用的函数列出了当你调用存储过程时实际执行的所有T-SQL

存储过程的来源如下;

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Create_IRM_Appointment]
    @VisitNumber varchar(50) = null,
    @AppointmentTypeText varchar (255) = null,
    @ClinicCode varchar(50) = null,
    @DoctorCode varchar(50) = null,
    @ScheduledDateTime varchar(50) = null,
    @AppointmentStatusCode varchar(255) = null,
    @PatientNumber varchar(200) = null,
    @EpisodeNumber varchar(255) = null,
    @ConsultantCode varchar(200) = null,
    @ReferrerCode varchar(200) = null,
    @MainSpecialtyCode varchar(100) = null,
    @MessageType varchar (60) = null,
    @StopDateTime varchar(50) = null,
    @EpisodeType varchar (50) = null,
    @EpisodeStatusCode varchar (50) = null,
    @ParentEpisodeType varchar (50) = null,
    @AdmissionType varchar (50) = null,
    @AdmitDateTime varchar(50) = null,
    @AdmitReason varchar(255) = null,
    @AppointmentDateTime varchar(50) = null,
    @BuildingCode varchar(50) = null,
    @BedNumber varchar(50) = null,
    @CategoryCode varchar(50) = null,
    @ConsultantName varchar(255) = null,
    @DischargeDateTime varchar(50) = null,
    @DischargeDestination varchar(50) = null,
    @DischargeMethod varchar(50) = null,
    @FacilityCode varchar(50) = null,
    @ReferralReason varchar(255) = null,
    @ReferralAssigningAuthority varchar(50) = null,
    @ReferrerName varchar(255) = null,
    @TCIDate varchar(255) = null,
    @TCILocation varchar (255) = null,
    @WardCode varchar(50) = null
AS
BEGIN
    declare @return_value int 
    declare @error_message varchar(255)

begin try
     insert into dbo.IRM_TEMP_Appointment
     (
        VisitNumber,
        WardCode,
        ClinicCode,
        DoctorCode ,
        ScheduledDateTime ,
        AppointmentStatusCode ,
        PatientNumber ,
        EpisodeNumber ,
        ConsultantCode ,
        ReferrerCode,
        MainSpecialtyCode ,
        MessageType ,
        StopDateTime ,
        EpisodeType ,
        EpisodeStatusCode ,
        ParentEpisodeType ,
        AdmissionType ,
        AdmitDateTime,
        AdmitReason,
        AppointmentDateTime ,
        BuildingCode ,
        BedNumber,
        CategoryCode ,
        ConsultantName ,
        DischargeDateTime ,
        DischargeDestination,
        DischargeMethod ,
        FacilityCode ,
        ReferralReason,
        ReferrerAssigningAuthority,
        ReferrerName, 
        timeOfInsert,
        timeOfAnalyse,
        analysed,
        currentStatus,
        AppointmentTypeText,
        TCIDate,
        TCILocation)
     values (
        @VisitNumber,
        @WardCode,
        @ClinicCode,
        @DoctorCode ,
                case when @ScheduledDateTime = null then null  else convert(datetime, STUFF(STUFF(STUFF(@ScheduledDateTime,9,0,' '),12,0,':'),15,0,':')) end,
        @AppointmentStatusCode ,
        @PatientNumber ,
        @EpisodeNumber ,
        @ConsultantCode ,
        @ReferrerCode,
        @MainSpecialtyCode ,
        @MessageType ,
                case when @StopDateTime = null then null  else convert(datetime, STUFF(STUFF(STUFF(@StopDateTime,9,0,' '),12,0,':'),15,0,':')) end,
        @EpisodeType ,
        @EpisodeStatusCode ,
        @ParentEpisodeType ,
        @AdmissionType ,
                case when @AdmitDateTime = null then null  else convert(datetime, STUFF(STUFF(STUFF(@AdmitDateTime,9,0,' '),12,0,':'),15,0,':')) end,
        @AdmitReason,
                case when @AppointmentDateTime = null then null  else convert(datetime, STUFF(STUFF(STUFF(@AppointmentDateTime,9,0,' '),12,0,':'),15,0,':')) end,
        @BuildingCode ,
        @BedNumber,
        @CategoryCode ,
        @ConsultantName ,
                case when @DischargeDateTime = null then null  else convert(datetime, STUFF(STUFF(STUFF(@DischargeDateTime,9,0,' '),12,0,':'),15,0,':')) end,
        @DischargeDestination,
        @DischargeMethod ,
        @FacilityCode ,
        @ReferralReason,
        @ReferralAssigningAuthority,
        @ReferrerName, 
        CURRENT_TIMESTAMP,
        NULL,
        0,
        'UNPROCESSED',
        @AppointmentTypeText,
                case when @TCIDate = null then null  else convert(datetime, STUFF(STUFF(STUFF(@TCIDate,9,0,' '),12,0,':'),15,0,':')) end,
                @TCILocation);
    set @return_value = 0


    end try
    begin catch
        set @return_value = (SELECT ERROR_NUMBER() as Error_Number)
        set @error_message = (SELECT ERROR_MESSAGE() as Error_Message)
        RAISERROR (@error_message,10,1) 


    end catch;
    return @return_value
END

2 个答案:

答案 0 :(得分:0)

正如评论中所述,存储过程似乎无法阻止任何事情。

  

所以我想知道是否有一个我可以使用的函数列出了当你调用存储过程时实际执行的所有T-SQL

您需要的是一个脚本,它告诉当前正在执行的语句,阻塞,读取,写入......

这可以通过运行Adam Machanic的sp_whoisactive存储过程来完成..

答案 1 :(得分:0)

在这种情况下,我通常使用T-SQL模板启动SQL Server Profiler,确保选择显示所有相关的LOCK事件以及其他有用位,设置数据库过滤到其他未使用的数据库,以确保我只捕获我的事件,然后针对该数据库运行SP。

通常,在大约五到十五分钟内,我的手掌快速接近我的前额:)