Oracle过程不返回结果[已关闭]

wz3gfoph  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(113)

**已关闭。**此问题需要debugging details。它目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
关闭7天前。
Improve this question
我在Oracle中有一个主过程,它调用其他较小的过程,即使它们中的每一个在单独运行时都能正常工作,但当我试图运行主过程时,什么也没有发生。没有错误,但没有结果枯萎。我想知道我是否在代码中遗漏了一些东西:

create or replace PROCEDURE ADMIN.sp_league_pool_users_master AS
    v_SEASON_NUMBER_DERIVED NUMBER;
    v_LEAGUE_COUNTRY VARCHAR2(100);
    v_LEAGUE_LEVEL NUMBER;
    v_ALPHA2CODE_DERIVED VARCHAR2(2);
    v_count NUMBER;
BEGIN
    -- Step 1: Derive the Season Number
    ADMIN.sp_deriveSeasonNumber(v_SEASON_NUMBER_DERIVED);

    -- Step 2: Scan the LEAGUE_POOL table
    FOR r IN (SELECT lp.LEAGUE_POOL_ID, lp.LEAGUE_COUNTRY, lp.LEAGUE_LEVEL FROM LEAGUE_POOL lp) LOOP
        v_LEAGUE_COUNTRY := r.LEAGUE_COUNTRY;
        v_LEAGUE_LEVEL := r.LEAGUE_LEVEL;

        -- Step 3: Derive ALPHA2CODE_DERIVED and check for existing records
        CASE r.LEAGUE_COUNTRY
            WHEN 'Rest of Africa' THEN v_ALPHA2CODE_DERIVED := 'BJ';
            WHEN 'Rest of Asia and Oceania' THEN v_ALPHA2CODE_DERIVED := 'AF';
            WHEN 'Rest of North America' THEN v_ALPHA2CODE_DERIVED := 'AG';
            WHEN 'Rest of South America' THEN v_ALPHA2CODE_DERIVED := 'FK';
            ELSE 
                -- Fetch from the COUNTRIES table
                SELECT ct.ALPHA2CODE INTO v_ALPHA2CODE_DERIVED
                FROM COUNTRIES ct WHERE ct.LEAGUE_COUNTRY = r.LEAGUE_COUNTRY;
        END CASE;

        SELECT COUNT(*) INTO v_count FROM LEAGUE_POOL_USERS lpu
        WHERE lpu.LEAGUE_POOL_ID = r.LEAGUE_POOL_ID AND lpu.SEASON_NUMBER = v_SEASON_NUMBER_DERIVED;

        IF v_count > 0 THEN
            SELECT COUNT(*) INTO v_count 
            FROM LEAGUE_POOL_USERS 
            WHERE LEAGUE_POOL_ID = r.LEAGUE_POOL_ID AND SEASON_NUMBER = v_SEASON_NUMBER_DERIVED + 1;

            IF v_count = 0 THEN
                -- Step 4: Call the appropriate level procedure based on LEAGUE_LEVEL
                CASE r.LEAGUE_LEVEL
                    WHEN 1 THEN
                        ADMIN.sp_handleLeagueLevel1(v_SEASON_NUMBER_DERIVED, v_LEAGUE_COUNTRY);
                    WHEN 2 THEN
                        ADMIN.sp_handleLeagueLevel2(v_SEASON_NUMBER_DERIVED, v_LEAGUE_COUNTRY);
                    WHEN 3 THEN
                        ADMIN.sp_handleLeagueLevel3(v_SEASON_NUMBER_DERIVED, v_LEAGUE_COUNTRY);
                    WHEN 4 THEN
                        ADMIN.sp_handleLeagueLevel4(v_SEASON_NUMBER_DERIVED, v_LEAGUE_COUNTRY);
                END CASE;
            END IF;
        ELSE
            -- If the LEAGUE_POOL_ID doesn't exist in LEAGUE_POOL_USERS, call the insert procedure
            ADMIN.sp_insertLeaguePoolUsers(v_SEASON_NUMBER_DERIVED, v_LEAGUE_COUNTRY, v_LEAGUE_LEVEL, r.LEAGUE_POOL_ID);
        END IF;
    END LOOP;
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('An error occurred in sp_league_pool_users_master: ' || SQLERRM);
END sp_league_pool_users_master;
mctunoxg

mctunoxg1#

正如在对您的问题的评论中提到的,很难帮助您,因为我们没有任何样本数据来运行上述过程,所以我们不能说为什么它不像您期望的那样工作。也许问题出在其他地方,在它所要求的程序中?
但是,您可以使调试过程更加容易(也许同时还可以提高性能!),方法是将循环内的select语句合并到游标中。这样,您就可以单独运行查询(适当地修改季节细节),以查看该过程应该在单个数据集中执行什么操作,而不必逐行调试该过程。
我花时间重写了你的程序,虽然它显然是未经测试的!它应该给给予你一个如何继续的想法:

CREATE OR REPLACE PROCEDURE admin.sp_league_pool_users_master AS
  v_season_number_derived NUMBER;
BEGIN
  -- Derive the Season Number
  admin.sp_deriveseasonnumber(v_season_number_derived);

  -- Scan the LEAGUE_POOL table
  FOR r IN (WITH country_codes AS (SELECT 1 qry_num, 'Rest of Africa' league_country, 'BJ' alpha2code_derived FROM dual UNION ALL
                                   SELECT 1 qry_num, 'Rest of Asia and Oceania' league_country, 'AF' alpha2code_derived FROM dual UNION ALL
                                   SELECT 1 qry_num, 'Rest of North America' league_country, 'AG' alpha2code_derived FROM dual UNION ALL
                                   SELECT 1 qry_num, 'Rest of South America' league_country, 'FK' alpha2code_derived FROM dual
                                   UNION ALL
                                   SELECT 2 qry_num, league_country, alpha2code
                                   FROM   countries) -- if the first four rows above override rows in the countries table, update the subquery to return the 1st row based on qry_num. Otherwise the qry_num column can be removed from this subquery.
                           lpu AS (SELECT league_pool_id,
                                          season_number,
                                          COUNT(CASE WHEN season_number = v_season_number_derived THEN 1 END) lpu_season_count,
                                          COUNT(CASE WHEN season_number = v_season_number_derived + 1 THEN 1 END) lpu_season_count_plus_1
                                   FROM   league_pool_users
                                   WHERE  season_number IN (v_season_number_derived, v_season_number_derived + 1))
            SELECT lp.league_pool_id,
                   lp.league_country,
                   lp.league_level,
                   cc.alpha2code_derived, -- is this even needed? it doesn't seem to be used in the procedure!
                   lpu.lpu_season_count,
                   lpu.lpu_season_count_plus_1
            FROM   league_pool lp
                   LEFT OUTER JOIN country_codes cc ON lp.league_country = cc.league_country
                   LEFT OUTER JOIN lpu ON lp.league_pool_id = lpu.league_pool_id;)
  LOOP
    -- If the LEAGUE_POOL_ID exists in LEAGUE_POOL_USERS for the derived season but not the following season,
    -- call the appropriate level procedure based on LEAGUE_LEVEL
   IF lpu_season_count > 0 AND lpu_season_count_plus_1 = 0
   THEN
     
     CASE r.league_level
          WHEN 1 THEN
            admin.sp_handleleaguelevel1(v_season_number_derived, r.league_country);
          WHEN 2 THEN
            admin.sp_handleleaguelevel2(v_season_number_derived, r.league_country);
          WHEN 3 THEN
            admin.sp_handleleaguelevel3(v_season_number_derived, r.league_country);
          WHEN 4 THEN
            admin.sp_handleleaguelevel4(v_season_number_derived, r.league_country);
       END CASE;
    
    -- Otherwise, If the LEAGUE_POOL_ID doesn't exist in LEAGUE_POOL_USERS, call the insert procedure
    ELSIF lpu_season_count = 0
    THEN
    admin.sp_insertleaguepoolusers(v_season_number_derived, r.league_country, r,league_level, r.league_pool_id);
    END IF;

  END LOOP;
  
/*EXCEPTION
  WHEN OTHERS THEN
    dbms_output.put_line('An error occurred in sp_league_pool_users_master: ' || SQLERRM);*/
END sp_league_pool_users_master;
/

注意:是否需要围绕alpha2code_derived国家代码的逻辑?在你的手术中似乎没有用到它!

相关问题