Advertisement
Guest User

Untitled

a guest
Feb 6th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 2.18 KB | None | 0 0
  1. CREATE OR REPLACE FUNCTION
  2. for_input.codes_handling(table_name text, UID text)
  3.    returns text
  4.    language plpgsql
  5.    returns NULL ON NULL input
  6.    security definer
  7.    volatile
  8.    AS
  9. $$
  10. DECLARE
  11.     r RECORD;
  12.     cnt int;
  13. BEGIN
  14.  
  15.     EXECUTE('create temp table if not exists tmp2  ( like for_input.reserve_' || table_name || ' including all ) on commit drop ;');
  16.     INSERT INTO tmp2 (SELECT
  17.                             uid_main,
  18.                             nl_mob,
  19.                             nu_tob,
  20.                             code_tob,
  21.                             unit_tob,
  22.                             quantity_tob,
  23.                             doc_type_mob
  24.                             unit_tob
  25.                         FROM buh.mob_n_tob
  26.                         WHERE uid_main=$2 ) ;
  27.  
  28.     FOR r IN
  29.             SELECT code FROM tmp2
  30.         LOOP
  31.             WITH pre_select AS (
  32.                 SELECT code
  33.                     FROM buh.nom
  34.                     WHERE article = (SELECT article FROM buh.nom WHERE code=r.code) AND code != r.code
  35.             )
  36.             SELECT INTO cnt COUNT(*) FROM pre_select ;
  37.             IF cnt = 1 THEN
  38.                 WITH pre_select AS (
  39.                     SELECT code
  40.                         FROM buh.nom
  41.                         WHERE article = (SELECT article FROM buh.nom WHERE code=r.code) AND code != r.code
  42.                 )
  43.                 UPDATE tmp2 SET code_2 = (SELECT * FROM pre_select) WHERE code=r.code;
  44.             END IF ;
  45.             IF cnt = 2 THEN
  46.                 WITH pre_select AS (
  47.                     SELECT code
  48.                         FROM buh.nom
  49.                         WHERE article = (SELECT article FROM buh.nom WHERE code=r.code) AND code != r.code
  50.                 )
  51.                 UPDATE tmp2 SET code_3 = (SELECT * FROM pre_select ORDER BY code DESC limit 1) WHERE code=r.code;
  52.            
  53.                 WITH pre_select AS (
  54.                     SELECT code
  55.                         FROM buh.nom
  56.                         WHERE article = (SELECT article FROM buh.nom WHERE code=r.code) AND code != r.code
  57.                 )
  58.                 UPDATE tmp2 SET code_2 = (SELECT * FROM pre_select ORDER BY code ASC limit 1) WHERE code=r.code;
  59.             END IF ;
  60.         END LOOP ;
  61.  
  62.     EXECUTE('insert into for_input.reserve_' || table_name || ' select uid,
  63.                                                                        nl,
  64.                                                                        nu,
  65.                                                                        code,
  66.                                                                        unit,
  67.                                                                        quantity,
  68.                                                                        doc_type,
  69.                                                                        i,
  70.                                                                        code_2,
  71.                                                                        code_3,
  72.                                                                        article,
  73.                                                                        code_upr_nom,
  74.                                                                        dead_end
  75.                                                                     from
  76.                                                                        tmp2 ;');
  77.  RETURN NULL ;
  78.  
  79. END ;
  80. $$ ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement