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 |
|
dasu
Posting Yak Master
104 Posts |
Posted - 2004-12-13 : 05:10:18
|
| IF EXISTS (SELECT name FROM sysobjects WHERE name = 'pr_cbs_upd_rlbk' AND type = 'P') DROP PROCEDURE pr_cbs_upd_rlbk GO/******************************************************************** PROCEDURE: pr_cbs_upd_rlbk* CREATED: Dasu Gairaboni 22-08-2004* LAST UPDATED ON: 22-08-2004** Variables in:* * * pintJobId - Job identifier** Variables out: pintStat - Stores stored procedures return value statement *** Description: For the given jobid this stored procedure rollbacks all the updates have done on request response table . * DATE AUTHOR DESCRIPTION*-------------------------------------------------------------------*{modification description}********************************************************************/CREATE PROCEDURE pr_cbs_upd_rlbk @pintJobId INT,@pintStat INT OUTPUTAS/* VARIABLE DESCRIPTION VARIABLE NAME TYPE OF VARIABLE DESCRIPTION @intUpdErr INT Update error code @intFail INT Stores failure status @intSuccess INT Stores success status @strErrMsg VARCHAR(255) Error information@strErrCD VARCHAR(30) Error code@strMdulNm VARCHAR(30) Name of the module @intJobid INT Stores jobid @intUpdJbobid INT Stores update jobid @strActNo VARCHAR(30) Stores account number@intAStatCd INT Stores audit table's status code@intDStatCd INT Stores request response tabel's status code@intRqstCd INT Stores request code @dtValDt DATETIME Stores request valuation date@intErrRsnId INT Stores error reason id */--Declaration of local variables DECLARE @intUpdErr INT, @intFail INT , @intSuccess INT, @strErrMsg VARCHAR(255), @strErrCD VARCHAR(30), @strMdulNm VARCHAR(30), @intJobid INT, @intUpdJbobid INT, @strActNo VARCHAR(30), @intAStatCd INT, @intDStatCd INT, @intRqstCd INT, @dtValDt DATETIME, @intErrRsnId INT SET @intFail =DBO.FN_CBS_FAIL() --Generic constant for failureSET @intSuccess =DBO.FN_CBS_SUCCESS() --Generic constant for successSET @strMdulNm ='pr_cbs_upd_rlbk' --Stores module name -- Validation for jobidIF NOT EXISTS (SELECT upd_job_id FROM T_CBS_RQST_DTLS WHERE upd_job_id=@pintJobId) RETURN /* Update the jobstatus to invalid when the requested feed is trying to rollback. For example The outbound feed epi.req.txnpos.dts will populate the t_cbs_rqst_dtls table.After populating the records into t_cbs_rqst_dtls table if the feed fails to execute,the jobstatus has to rollback to previos status.But we do not have previos status in audit table so we make this status code as invalid.This piece of code will execute when job_id is equal to upd_job_id*/SELECT @intJobid=job_id,@intUpdJbobid=upd_job_id FROM T_CBS_RQST_DTLS WHERE job_id=@pintJobIdIF (@intJobid=@intUpdJbobid) BEGIN UPDATE t_cbs_rqst_dtls SET stat_cd=DBO.FN_CBS_INVALIDATED(),upd_job_id=@pintJobId WHERE upd_job_id=@pintJobId AND job_id=upd_job_id IF (@@ERROR=0)--Logs information message about successful rollback BEGIN SET @pintStat=@intSuccess SET @strErrMsg ='Status code updated to '+CAST (DBO.FN_CBS_INVALIDATED() AS VARCHAR(30)) SET @strErrCD=DBO.FN_CBS_INFO_UPD_RLBK() exec pr_cbs_log_error @pintJobId,@strMdulNm,@strErrCD,@strErrMsg RETURN END ELSE-- Error handling for update statement BEGIN SET @pintStat=@intFail SET @strErrMsg ='SQL ERROR NO IS '+ CAST (@intUpdErr AS VARCHAR(30))+'occured while updating status code to invalidate' SET @strErrCD=DBO.FN_CBS_UPD_ERR() exec pr_cbs_log_error @pintJobId,@strMdulNm,@strErrCD,@strErrMsg RETURN END END /* Update the jobstatus to its previous status when response feed is trying to rollback. For example The inbound feed epi.in.posapos.dts will update the t_cbs_rqst_dtls table. After updating the records into t_cbs_rqst_dtls table if feed fails to execute, jobstatus has to rollback to previos status by using audit table.This piece of code will execute if job_id not equals to upd_job_id */ DECLARE curRqstDtls CURSOR LOCAL FOR select acct_no,val_dt,rqst_cd from t_cbs_rqst_Dtls where upd_job_id=@pintJobId -- Open cursor named curRqstDtls OPEN curRqstDtls-- Search for first record set in curRqstDtls FETCH NEXT FROM curRqstDtls INTO @strActNo,@dtValDt,@intRqstCd -- Looping through all the records WHILE @@FETCH_STATUS=0 BEGIN-- update stat_cd and upd_job_id and err_rsn_id to its previous status select @intDStatCd=stat_cd,@intJobid=job_id,@intErrRsnId=err_rsn_id from t_cbs_rqst_dtls_adt where acct_no=@strActNo and rqst_cd=@intRqstCd and sys_ts=(select max(sys_ts) from t_cbs_rqst_dtls_adt where acct_no=@strActNo and rqst_cd=@intRqstCd and job_id<>@pintJobId) update t_cbs_rqst_dtls set stat_cd=@intDStatCd, upd_job_id=@intJobid, err_rsn_id=@intErrRsnIdwhere upd_job_id=@pintJobIdand rqst_cd=@intRqstCdand acct_no=@strActNoand val_dt =@dtValDtSET @intUpdErr=@@ERRORIF(@@ERROR<>0) BEGIN SET @pintStat=@intFail SET @strErrMsg ='SQL ERROR NO IS '+ CAST (@intUpdErr AS VARCHAR(30))+'occured while updating status code ' SET @strErrCD=DBO.FN_CBS_UPD_ERR() exec pr_cbs_log_error @pintJobId,@strMdulNm,@strErrCD,@strErrMsg RETURN END -- Fetch next record from curser FETCH NEXT FROM curRqstDtls INTO @strActNo,@dtValDt,@intRqstCdend CLOSE curRqstDtls-- Close curRqstDtls cursor DEALLOCATE curRqstDtlsIF (@intUpdErr=0) BEGIN SET @pintStat=@intSuccess SET @strErrMsg ='Successfully updating stat_cd to its previous status ' SET @strErrCD=DBO.FN_CBS_INFO_UPD_RLBK() exec pr_cbs_log_error @pintJobId,@strMdulNm,@strErrCD,@strErrMsg RETURNEND Some times this stored procedure taking 44 mnts for 20000 recordssomet times it is processing with in 3 mnts.why this difference suggest me proper solution with out cursor.please.dasu.g |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-12-13 : 14:26:43
|
| You haven't provided your table structure or an explanation of what it is doing. We can't help you given what you have posted so far.Tara |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-12-13 : 16:20:09
|
| Lose the cursorBrett8-) |
 |
|
|
dasu
Posting Yak Master
104 Posts |
Posted - 2004-12-13 : 22:56:59
|
| Here iam using two tables 1.t_cbs_rqst_dtls(acct_no,val_dt,rqst_cd,upd_job_id,job_id,err_rsn_id,stat_cd)in this primary key is on (acct_no,val_dt,rqst_cd,stat_Cd)initially job_id and upd_job_id are equal if that time if call the above stored proicedure that will make stat_cd to 4.after one update on above table the upd_job_id will be populated.if i am trying to execute above stored procedure then it will take previous status from t_cbs_rqst_dtls_adt table.this table consists of complete info about t_cbs_rqst_dtls.like all insertion and all updates will be populated by insert , update trigger on t_cbs_rqst_dtls tablethis is schema for above table1.t_cbs_rqst_dtls_adt(acct_no,val_dt,rqst_cd,upd_job_id,job_id,err_rsn_id,stat_cd,sys_ts)this sys_ts is datetime type.it will say that when the insertion andupdates happened on t_cbs_rqst_dtls table.thanksdasu.g |
 |
|
|
|
|
|
|
|