zoukankan      html  css  js  c++  java
  • 我对 PostgreSQL tidscan的理解

    PostgreSQL中,有一种 tidscan。当我在使用cursor的时候,会用到此种scan:

    [postgres@lex pgsql]$ cat ./data/test.sqlCREATE OR REPLACE Function FindCourse
       ( name_in IN varchar )
       RETURNS integer LANGUAGE plpgsql AS $$
    DECLARE
        cnumber integer;
        c1 CURSOR
        FOR
           SELECT course_number, instructor
            from course_tbl
            where course_name = name_in
            FOR UPDATE;
    
    BEGIN
    
    BEGIN
    open c1;
    fetch c1 into cnumber;
    
    IF not found THEN
         cnumber := 9999;
    ELSE
         UPDATE course_tbl
            SET instructor = 'SMITH'
            WHERE CURRENT OF c1;
        COMMIT;
    END IF;
    
    close c1;
    
    EXCEPTION
    WHEN OTHERS THEN
    END;
    RETURN cnumber;
    END;$$;
    [postgres@lex pgsql]$ 

     下面再进行一下修正:

    在PostgreSQL的 function 里面,是不允许进行commit的。所以其实,上面的程序需要改成:

    [postgres@lex pgsql]$ cat ./data/test.sqlCREATE OR REPLACE Function FindCourse
       ( name_in IN varchar )
       RETURNS integer LANGUAGE plpgsql AS $$
    DECLARE
        cnumber integer;
        c1 CURSOR
        FOR
           SELECT course_number, instructor
            from course_tbl
            where course_name = name_in
            FOR UPDATE;
    
    BEGIN
    
    BEGIN
    open c1;
    fetch c1 into cnumber;
    
    IF not found THEN
         cnumber := 9999;
    ELSE
         UPDATE course_tbl
            SET instructor = 'SMITH'
            WHERE CURRENT OF c1;
    END IF;
    
    close c1;
    
    EXCEPTION
    WHEN OTHERS THEN
    END;
    RETURN cnumber;
    END;$$;
    [postgres@lex pgsql]$ 
  • 相关阅读:
    python学习永久存储和异常处理
    python学习os文件系统模块
    python学习文件
    python学习集合
    python学习字典
    python学习递归
    python学习函数
    python学习序列
    js加入收藏
    判断dataset和datareader中是否存在某列
  • 原文地址:https://www.cnblogs.com/gaojian/p/3130635.html
Copyright © 2011-2022 走看看