Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
MuadDBA
628 Posts |
Posted - 2004-05-10 : 11:58:51
|
| When I took my current job, I was 90% DBA and 10% developer. Now, I am doing more devleoper work and less DBA work, and my weaknesses are showing.First, some background on the system:It's an OCR system for automating validation of bank documents. We have 2 main tables, LOAN and DOCUMENT. The LOAN table has most of the info that is global to the entire loan (Loan number, product type, correspondant, closing date, etc). This table gets information from a nightly file feed, and is refreshed live (based on a status code field) from a loan origination system.The DOCUMENT table has info specific to documents which are scanned into an imaging workflow system (loan number, document type, object_id, street addresses, various other document level information). This information is added by the OCR software. Once this is done, a set of business rules is run against it, and it results in message codes benig appended to the image, and these codes are then brought back into the workflow imaging system.The origination system is constantly polling for items which need their data refreshed, and the logic engine is also constatly polling for items which are ready for the business rules to be run. The workflow system also polls for data and will cut the OCR process short if someone manually reviews the documents instead of waiting for them to be reveiwed automatically.All of these systems poll based on a status code field present at both the loan and the document level. As you will see when I post the code for the stored procedure interfaces, each time one table's status code is updated, it updates the status code for the other table so the statuses are kept in synch. Also, each system is polling for DIFFERENT status codes and updating them (ie status 320 means OCR should extract info, status 500 means origination system should refresh, status 550 means logic engine should run, etc).My problem is that I am experiencing deadlocks several times during the day. Can you see any reason why this should happend based on my code? CREATE PROCEDURE SP_LOAN_UPDATE@eiStream_ID [varchar](15) = '-1' ,@Status [varchar](4) = '-1' ,@M_refresh_date_time [datetime] = '12/31/2999' ,@M_loan [varchar](10) = '-1' ,@M_loan_status [varchar](3) = '-1' ,@M_loan_type [varchar](4) = '-1' ,@M_financing_type [int] = -1 ,@M_program_type [int] = -1 ,@M_program_sub_type [int] = -1 ,@M_note_rev_date [datetime] = '12/31/2999' ,@M_audit_flag [varchar](2) = '-1' ,@M_MI_coverage [varchar](2) = '-1' ,@M_lender_type [numeric](10,0) = -1 ,@M_state_code [varchar](2) = '-1' ,@M_purpose_code [smallint] = -1 ,@M_closing_date [datetime] = '12/31/2999' ,@M_CLC_code [varchar](2) = '-1' ,@M_condition_0176 [varchar](2) = '-1' ,@M_city [varchar](30) = '-1' ,@M_bldg_no [varchar](10) = '-1' ,@M_street_no [varchar](10) = '-1' ,@M_pre_dir [varchar](2) = '-1' ,@M_street_name [varchar](50) = '-1' ,@M_post_dir [varchar](2) = '-1' ,@M_street_type [varchar](7) = '-1' ,@M_apt_no [varchar](6) = '-1' ,@M_state [varchar](2) = '-1' ,@M_ZIP [varchar](10) = '-1' ,...@S_Loan_Type [varchar](10) = '-1' ,@Source [varchar](4) = null,@NO_HISTORY [varchar](10) = 'history',@INT_CALL tinyint = 0,@ERR [int] = 3 outputasdeclare @RC int declare @nummostosubtract int /* Number of months to subtract from IPTD from mupit*/select @nummostosubtract = 0select @source = rtrim(@source)select @status = rtrim(@status)IF @M_LOAN is null OR @M_LOAN = '-1' OR @STATUS = '-1' OR @STATUS is null OR @status not in (select status from STATUS_CODES)BEGIN PRINT 'You must supply a valid LOAN and STATUS code' SELECT @ERR = 4 SELECT @ERR RAISERROR (50004,16,2) RETURN @ERRENDexec SP_SOURCE_CHECK @SOURCE, @ERR outputIF @ERR <> 3 BEGIN SELECT @ERR RETURN @ERREND select @eistream_id = eistream_id from document where loan = @m_loanUPDATE LOAN SET eistream_id = CASEwhen @eistream_id = '-1' then eistream_idElse @eistream_idEnd,status = @status,time_stamp = getdate(),m_refresh_date_time = CASEwhen @m_refresh_date_time = '12/31/2999' AND @source<> 'M' then m_refresh_date_timewhen @m_refresh_date_time = '12/31/2999' AND @source = 'M' and @status not in ('530','560') then m_refresh_date_timewhen @source = 'M' and @status in ('530','560') then getdate()Else @m_refresh_date_timeend,m_loan = @m_loan,m_loan_status = CASEwhen @m_loan_status = '-1' OR @source<> 'M' then m_loan_statusElse @m_loan_statusend,m_loan_type = CASEwhen @m_loan_type = '-1' OR @source<> 'M' then m_loan_typeElse @m_loan_typeend,m_financing_type = CASEwhen @m_financing_type = -1 OR @source<> 'M' then m_financing_typeElse @m_financing_typeend,m_program_type = CASEwhen @m_program_type = -1 OR @source<> 'M' then m_program_typeElse @m_program_typeend,m_program_sub_type = CASEwhen @m_program_sub_type = -1 OR @source<> 'M' then m_program_sub_typeElse @m_program_sub_typeend,m_note_rev_date = CASEwhen @m_note_rev_date = '12/31/2999' OR @source<> 'M' then m_note_rev_dateElse @m_note_rev_dateend,m_audit_flag = CASEwhen @m_audit_flag = '-1' OR @source<> 'M' then m_audit_flagElse @m_audit_flagend,m_MI_coverage = CASEwhen @m_MI_coverage = '-1' OR @source<> 'M' then m_MI_coverageElse @m_MI_Coverageend,m_lender_type = CASEwhen @m_lender_type = -1 OR @source<> 'M' then m_lender_typeElse @m_lender_typeend,m_state_code = CASEwhen @m_state_code = '-1' OR @source<> 'M' then m_state_codeElse @m_state_codeend,m_purpose_code = CASEwhen @m_purpose_code = '-1' OR @source<> 'M' then m_purpose_codeElse @m_purpose_codeend,m_closing_date = CASEwhen @m_closing_date = '12/31/2999' OR @source<> 'M' then m_closing_dateElse @m_closing_dateend,m_clc_code = CASEwhen @m_CLC_code = '-1' OR @source<> 'M' then m_clc_codeElse @m_clc_codeend,m_condition_0176 = CASEwhen @m_condition_0176 = '-1' OR @source<> 'M' then m_condition_0176Else @m_condition_0176end,m_city = CASEwhen @m_city = '-1' OR @source<> 'M' then m_cityElse @m_cityend,m_bldg_no = CASEwhen @m_bldg_no = '-1' OR @source<> 'M' then m_bldg_noElse @m_bldg_noend,m_street_no = CASEwhen @m_street_no = '-1' OR @source<> 'M' then m_street_noElse @m_street_noend,m_pre_dir = CASEwhen @m_pre_dir = '-1' OR @source<> 'M' then m_pre_dirElse @m_pre_dirend,m_street_name = CASEwhen @m_street_name = '-1' OR @source<> 'M' then m_street_nameElse @m_street_nameend,m_post_dir = CASEwhen @m_post_dir = '-1' OR @source<> 'M' then m_post_dirElse @m_post_dirend,m_street_type = CASEwhen @m_street_type = '-1' OR @source<> 'M' then m_street_typeElse @m_street_typeend,m_apt_no = CASEwhen @m_apt_no = '-1' OR @source<> 'M' then m_apt_noElse @m_apt_noend,m_state = CASEwhen @m_state = '-1' OR @source<> 'M' then m_stateElse @m_stateend,m_zip = CASEwhen @m_zip = '-1' OR @source<> 'M' then m_zipElse @m_zipend,...s_loan_type = CASEwhen @s_loan_type = '-1' OR @source <> 'S' or @s_loan_type not in ('CONV', 'ARM') then s_loan_typeelse @s_loan_typeendWhere m_loan = @m_loanSelect @RC = @@ROWCOUNTIF @RC = 1 and @NO_HISTORY = 'history'BEGIN exec SP_HISTORY_APPEND @m_loan, @eistream_id, @status, @source, @err output IF @ERR <> 3 BEGIN SELECT @ERR RETURN @ERR END IF @INT_CALL = 0 and @EISTREAM_ID <> '-1' begin exec @err = SP_DOCUMENT_UPDATE @LOAN = @M_LOAN, @EISTREAM_ID = @EISTREAM_ID, @STATUS = @STATUS, @SOURCE = @SOURCE, @NO_HISTORY ='NO_HISTORY', @INT_CALL = 1 end IF @ERR <> 3 BEGIN SELECT @ERR RETURN @ERR ENDENDELSE BEGIN IF @RC = 1 and @NO_HISTORY = 'NO_HISTORY' BEGIN IF @INT_CALL = 0 and @EISTREAM_ID <> '-1' begin exec @err = SP_DOCUMENT_UPDATE @LOAN = @M_LOAN, @EISTREAM_ID = @EISTREAM_ID, @STATUS = @STATUS, @SOURCE = @SOURCE, @INT_CALL = 1 end IF @ERR <> 3 BEGIN SELECT @ERR RETURN @ERR END END ELSE BEGIN IF @RC <> 1 BEGIN SELECT @ERR = 5 select @ERR RAISERROR (50005,16,2) RETURN @ERR END ENDENDIF @INT_CALL = 0 select @errreturn @ERRGOCREATE procedure SP_DOCUMENT_UPDATE@eiStream_ID varchar(15) ='-1',@DocType varchar(5) ='-1',@Loan varchar(10) ='-1',@Status varchar(4) ='-1',@L_result_text varchar(250) ='-1',@L_P_I numeric(15,2) =-1,@L_note_review_date datetime = '12/31/2999',@L_middle_1 varchar(1) ='-',@L_middle_2 varchar(1) ='-',@L_middle_3 varchar(1) ='-',@L_middle_4 varchar(1) ='-',@O_note_loan varchar(10) ='-1',@O_note_closing_date datetime = '12/31/2999',@O_note_city varchar(30) ='-1',@O_note_street_address varchar(50) ='-1',@O_note_state varchar(2) ='-1',@O_note_zip varchar(10) ='-1',@O_note_loan_amt numeric(15,2) =-1,@O_note_lender varchar(50) ='-1',@O_note_interest_rate numeric(7,4) =-1,...@L_SECURE_FLAG smallint = -1,@source varchar(4) ='-1',@NO_HISTORY varchar(10) ='history',@INT_CALL tinyint =0,@err smallint = 0 outputASselect @source = rtrim(@source)select @status = rtrim(@status)IF @LOAN is null OR @LOAN = '-1' OR @STATUS = '-1' OR @STATUS is null OR @status not in (select status from STATUS_CODES) OR @eiStream_id is null or @eiStream_ID = '-1'BEGIN PRINT 'You must supply a valid LOAN, eiStream_ID, and STATUS code' SELECT @ERR = 4SELECT @ERR RAISERROR (50004,16,2) RETURN @ERRENDexec SP_SOURCE_CHECK @SOURCE, @ERR outputIF @ERR <> 3 BEGIN SELECT @ERR RETURN @ERRENDIF (SELECT COUNT(*) FROM DOCUMENT WHeRE EISTREAM_ID = @EISTREAM_ID) < 1 begin exec @err = SP_LOAN_UPDATE @M_LOAN = @LOAN, @STATUS = @STATUS, @SOURCE = @SOURCE, @INT_CALL = 1 Print 'No Document Found, Updating Loan Record' endUpdate DOCUMENT setDocType = CASEwhen @DocType= '-1' then DocTypeelse @DocTypeend,Status = CASEwhen @Status= '-1' then Statuselse @Statusend,Time_stamp = getdate(),L_result_text = CASEwhen @L_result_text= '-1' OR @source<> 'L' then L_result_textelse @L_result_textend,L_P_I = CASEwhen @L_P_I= -1 OR @source<> 'L' then L_P_Ielse @L_P_Iend,L_note_review_date = CASEwhen @L_note_review_date= '12/31/2999' OR @source<> 'L' then L_note_review_dateelse @L_note_review_dateend,L_middle_1 = CASEwhen @L_middle_1= '-' OR @source<> 'L' then L_middle_1else @L_middle_1end,L_middle_2 = CASEwhen @L_middle_2= '-' OR @source<> 'L' then L_middle_2else @L_middle_2end,L_middle_3 = CASEwhen @L_middle_3= '-' OR @source<> 'L' then L_middle_3else @L_middle_3end,L_middle_4 = CASEwhen @L_middle_4= '-' OR @source<> 'L' then L_middle_4else @L_middle_4end,O_note_loan = CASEwhen @O_note_loan= '-1' OR @source<> 'O' then O_note_loanelse @O_note_loanend,O_note_closing_date = CASEwhen @O_note_closing_date= '12/31/2999' OR @source<> 'O' then O_note_closing_dateelse @O_note_closing_dateend,O_note_city = CASEwhen @O_note_city= '-1' OR @source<> 'O' then O_note_cityelse @O_note_cityend,O_note_street_address = CASEwhen @O_note_street_address = '-1' OR @source<> 'O' then O_note_street_addresselse @O_note_street_addressend,O_note_state = CASEwhen @O_note_state= '-1' OR @source<> 'O' then O_note_stateelse @O_note_stateend,O_note_zip = CASEwhen @O_note_zip= '-1' OR @source<> 'O' then O_note_zipelse @O_note_zipend,O_note_loan_amt = CASEwhen @O_note_loan_amt= -1 OR @source<> 'O' then O_note_loan_amtelse @O_note_loan_amt end,O_note_lender = CASEwhen @O_note_lender= '-1' OR @source<> 'O' then O_note_lenderelse @O_note_lenderend,O_note_interest_rate = CASEwhen @O_note_interest_rate= -1 OR @source<> 'O' then O_note_interest_rateelse @O_note_interest_rateend,...L_SECURE_FLAG = CASEwhen @L_SECURE_FLAG= -1 OR @source <> 'L' then L_SECURE_FLAGelse @L_SECURE_FLAGendwhere @eistream_id = eistream_id and @loan = loanIF @@ROWCOUNT <> 1BEGIN SELECT @ERR = 5select @err RAISERROR (50005,16,2) RETURN @ERRENDIF @NO_HISTORY <> 'NO_HISTORY'BEGIN exec SP_HISTORY_APPEND @loan, @eistream_id, @status, @source, @err outputIF @ERR <> 3 BEGIN SELECT @ERR RETURN @ERREND IF @INT_CALL = 0 BEGIN exec @err = SP_LOAN_UPDATE @M_LOAN = @LOAN, @STATUS = @STATUS, @SOURCE = @SOURCE, @NO_HISTORY ='NO_HISTORY', @INT_CALL = 1 ENDIF @ERR <> 3 BEGIN SELECT @ERR RETURN @ERRENDENDELSEBEGIN IF @NO_HISTORY = 'NO_HISTORY' BEGIN IF @INT_CALL = 0 BEGIN exec @err = SP_LOAN_UPDATE @M_LOAN = @LOAN, @STATUS = @STATUS, @SOURCE = @SOURCE, @INT_CALL = 1 ENDIF @ERR <> 3 BEGIN SELECT @ERR RETURN @ERREND ENDENDIF @INT_CALL = 0 SELECT @ERRRETURN @ERRGO |
|
|
|
|
|
|
|