Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 12.0.1 » SQL Anywhere 服务器 - SQL 的用法 » 存储过程、触发器、批处理和用户定义的函数 » 过程、触发器、用户定义函数和批处理中的游标

 

过程、触发器、用户定义函数和批处理中的定位更新

以下过程在 SELECT 语句上使用可更新的游标。它说明了如何使用存储过程语言在某一行上执行定位更新。



CREATE PROCEDURE UpdateSalary( 
  IN employeeIdent INT, 
  IN salaryIncrease NUMERIC(10,3) )
BEGIN 
-- Procedure to increase (or decrease) an employee's salary
  DECLARE err_notfound
      EXCEPTION FOR SQLSTATE '02000';
  DECLARE oldSalary NUMERIC(20,3);
  DECLARE employeeCursor 
    CURSOR FOR SELECT Salary from Employees
               WHERE EmployeeID = employeeIdent
    FOR UPDATE;
  OPEN employeeCursor;
  FETCH employeeCursor INTO oldSalary FOR UPDATE;
  IF SQLSTATE = err_notfound THEN
    MESSAGE 'No such employee' TO CLIENT;
  ELSE
    UPDATE Employees SET Salary = oldSalary + salaryIncrease 
      WHERE CURRENT OF employeeCursor;
  END IF;
  CLOSE employeeCursor;
END;

以下语句调用上述存储过程:

CALL UpdateSalary( 105, 220.00 );