Implicit SQL cursor of the common attributes% FOUND, SQL% NOTFOUND and SQL% ROWCOUNT

  Implicit cursor several common properties as follows:

  SQL% ROWCOUNT number of rows affected by the most recent SQL statement impact - numeric

  SQL% FOUND recent SQL statement is affected more than one row of data - Boolean

  SQL% NOTFOUND whether the most recent SQL statement does not affect any data - Boolean

  SQL% ISOPEN for implicit cursor is always in terms of Boolean FALSE--

  When a DML statement is executed, the results of DML will be saved in four cursor property. Value of SQL% ISOPEN implicit cursor is always FALSE.

  When SELECT INTO, INSERT, UPDATE, or DELETE success,

    SQL%FOUND为True,

    SQL%NOTFOUND为False,

    Is equal to the number of rows SQL% ROWCOUNT effects ( the SELECT the INTO is necessarily ROWCOUNT 1; the INSERT the VALUES ... ... of ROW_COUNT necessarily 1).

  When the SELECT INTO or INSERT unsuccessful, throw an exception ;

  When UPDATE or DELETE is unsuccessful,

    SQL%FOUND为False,

    SQL%NOTFOUND为True,

    SQL% ROWCOUNT equal to zero.

 

  Attach test code, SELECT Exception more than the other three:

  SELECT test

-- SQL%FOUND, SQL%NOTFOUND, SQL%ROWCOUNT 对 SELECT 的测试  
DECLARE 
  v_temp employees%ROWTYPE;
BEGIN
  SELECT *
  INTO v_temp
  FROM employees e
  WHERE employee_id = 10;
  
  The IF the SQL % FOUND THEN 
     dbms_output.put_line ( ' the SQL% FOUND ' );
   the END  the IF ;
   - fail to here, because INTO assignment of 
  the IF the SQL % NOTFOUND THEN 
     dbms_output.put_line ( ' the SQL% NOTFOUND ' );
   the END  the IF ;
  DBMS_OUTPUT.put_line('SQL%ROWCOUNT : ' || SQL%ROWCOUNT);
  
  EXCEPTION 
    WHEN no_data_found THEN
      DBMS_OUTPUT.put_line('Exception: no_data_found');
      DBMS_OUTPUT.put_line('SQL%ROWCOUNT : ' || SQL%ROWCOUNT);
END;
/*
Test results: If found in the results, SQL% FOUND = True, SQL% NOTFOUND = False, SQL% ROWCOUNT = 1;
          If the result is not found, SQL% ROWCOUNT = 0, an exception is thrown
*/

  INSERT test

- the SQL% FOUND, the SQL% NOTFOUND, the SQL INSERT% ROWCOUNT testing of   
- inserting the n 
the BEGIN 
  INSERT  the INTO Departments (DEPARTMENT_ID, DEPARTMENT_NAME)
   the VALUES ( 270 , ' Tang, ' );
  
  IF SQL%FOUND THEN
     DBMS_OUTPUT.put_line('SQL%FOUND');
  END IF;
  IF SQL%NOTFOUND THEN
     DBMS_OUTPUT.put_line('SQL%NOTFOUND');
  END IF;
  DBMS_OUTPUT.put_line('SQL%ROWCOUNT : ' || SQL%ROWCOUNT);
  
  EXCEPTION
    WHEN OTHERS THEN
      DBMS_OUTPUT.put_line('Something Error!');
      DBMS_OUTPUT.put_line('SQL%ROWCOUNT : ' || SQL%ROWCOUNT);
END;
/*
Test Results: If the insertion occurs, SQL% FOUND = True, SQL% NOTFOUND = False, SQL% ROWCOUNT number of rows inserted, usually 1;
          If the insertion is not successful, SQL% ROWCOUNT = 0, an exception is thrown
*/

  UPDATE test

-- SQL%FOUND, SQL%NOTFOUND, SQL%ROWCOUNT 对 UPDATE 的测试  
BEGIN
  UPDATE employees e
  SET salary = 10000
  -- WHERE employee_id < 103;
  WHERE employee_id < 10;
  
  IF SQL%FOUND THEN
     DBMS_OUTPUT.put_line('SQL%FOUND');
  END IF;
  IF SQL%NOTFOUND THEN
     DBMS_OUTPUT.put_line('SQL%NOTFOUND');
  END IF;
  DBMS_OUTPUT.put_line('SQL%ROWCOUNT : ' || SQL%ROWCOUNT);
END;
/*
Test results: If the change occurs, SQL% FOUND = True, SQL% NOTFOUND = False, SQL% ROWCOUNT to correspond to the number of rows modified;
          If the change had not occurred, SQL% FOUND = False, SQL% NOTFOUND = True, SQL% ROWCOUNT = 0
*/

  DELETE test

-- SQL%FOUND, SQL%NOTFOUND, SQL%ROWCOUNT 对 DELETE 的测试  
BEGIN
  DELETE FROM departments
  WHERE department_id IN (250, 260);
  
  IF SQL%FOUND THEN
     DBMS_OUTPUT.put_line('SQL%FOUND');
  END IF;
  IF SQL%NOTFOUND THEN
     DBMS_OUTPUT.put_line('SQL%NOTFOUND');
  END IF;
  DBMS_OUTPUT.put_line('SQL%ROWCOUNT : ' || SQL%ROWCOUNT);
END;
/*
Test results: If successfully deleted, SQL% FOUND = True, SQL% NOTFOUND = False, SQL% ROWCOUNT number of rows inserted, usually 1;
          If you delete unsuccessful, SQL% FOUND = False, SQL% NOTFOUND = True, SQL% ROWCOUNT = 0
*/

  Completed. Next write contrast NO_DATA_FOUND and SQL% NOT_FOUND of.

Guess you like

Origin www.cnblogs.com/AlleyMeowy/p/11010395.html
SQL