Skip to main content

Stored procedure exception handling

 DECLARE

 TotalUpd   NUMBER(36) := 0;
 BEGIN
    dbms_output.put_line ('Job Start time............... : ' 
        || TO_CHAR(SYSDATE, '             hh24:mi:ss'));
    UPDATE Asset SET _status = 'PROGRESS' WHERE status IS null;
    TotalUpd := SQL%ROWCOUNT; 
    IF TotalUpd = 0 THEN
        dbms_output.put_line ('No more data to update.');
    ELSE
        dbms_output.put_line('Total Records Updated. : '
            || TotalUpd);
    END IF; 
    COMMIT; 
 EXCEPTION 
 WHEN OTHERS THEN
    dbms_output.put_line ('Error while status as SUCCESS '); 
 END; 



  1. Declare @val1 int;  
  2. Declare @val2 int;  
  3. BEGIN TRY  
  4. Set @val1=8;  
  5. Set @val2=@val1/0; /* Error Occur Here */  
  6. END TRY  
  7. BEGIN CATCH  
  8. Print 'Error Occur that is:'  
  9. Print Error_Message()  
  10. END CATCH  

Exception handling is mainly used for Transaction Management. Let us see an example.
  1. Begin Transaction Trans  
  2.    Begin Try  
  3.       Delete From Employee Where Employee.Emp_IID<3  
  4.       Update Employee Set Employee.First_Name='Pankaj kumar' 
  5.       Where Employee.Emp_IID='6th' /* Error Will Occur Here */  
  6.       If @@TranCount>0  
  7.       begin Commit Transaction Trans  
  8.    End  
  9. End Try  
  10. Begin Catch  
  11.    if @@TranCount>0  
  12.    Print 'Error Is Occur in Transaction'  
  13.    begin Rollback Transaction Trans  
  14. End  
  15. End Catch  
  16.   
  17. Select * From Employee 

Comments

Popular posts from this blog

Select Names from table which have vowels

  Problem Query the list of  CITY  names from  table  which have vowels (i.e.,  a ,  e ,  i ,  o , and  u ) as both their first  and  last characters. Your result cannot contain duplicates. Input Format The  STATION  table is described as follows: Field Type ID NUMBER CITY VARCHAR2(21) STATE VARCHAR2(2) LAT_N NUMBER LONG_W NUMBER STATION where  LAT_N  is the northern latitude and  LONG_W  is the western longitude. MYSQL select distinct city from station where (city like 'a%' or city like 'e%' or city like 'i%' or city like 'o%' or city like 'u%' ) and ( city like '%a' or city like '%e' or city like '%i' or city like '%o' or city like '%u' )

SQL SERVER – Fix : Error 1702 CREATE TABLE failed because column in table exceeds the maximum of columns

  Error 1702 CREATE TABLE failed because column in table exceeds the maximum of columns SQL Server 2000 supports table with maximum 1024 columns. This errors happens when we try to create table with 1024 columns or try to add columns to table which exceeds more than 1024. Fix/Solution/WorkAround: Reduce the number of columns in the table to 1,024 or less
Q- What will be the result of the query below? Explain your answer and provide a version that behaves correctly. select case when null = null then 'Yup' else 'Nope' end as Result; Ans-This query will actually yield “Nope”, seeming to imply that  null  is not equal to itself! The reason for this is that the proper way to compare a value to  null  in SQL is with the  is  operator, not with  = . Accordingly, the correct version of the above query that yields the expected result (i.e., “Yup”) would be as follows: select case when null is null then 'Yup' else 'Nope' end as Result;