Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE OR REPLACE FUNCTION
- for_input.codes_handling(table_name text, UID text)
- returns text
- language plpgsql
- returns NULL ON NULL input
- security definer
- volatile
- AS
- $$
- DECLARE
- r RECORD;
- cnt int;
- BEGIN
- EXECUTE('create temp table if not exists tmp2 ( like for_input.reserve_' || table_name || ' including all ) on commit drop ;');
- INSERT INTO tmp2 (SELECT
- uid_main,
- nl_mob,
- nu_tob,
- code_tob,
- unit_tob,
- quantity_tob,
- doc_type_mob
- unit_tob
- FROM buh.mob_n_tob
- WHERE uid_main=$2 ) ;
- FOR r IN
- SELECT code FROM tmp2
- LOOP
- WITH pre_select AS (
- SELECT code
- FROM buh.nom
- WHERE article = (SELECT article FROM buh.nom WHERE code=r.code) AND code != r.code
- )
- SELECT INTO cnt COUNT(*) FROM pre_select ;
- IF cnt = 1 THEN
- WITH pre_select AS (
- SELECT code
- FROM buh.nom
- WHERE article = (SELECT article FROM buh.nom WHERE code=r.code) AND code != r.code
- )
- UPDATE tmp2 SET code_2 = (SELECT * FROM pre_select) WHERE code=r.code;
- END IF ;
- IF cnt = 2 THEN
- WITH pre_select AS (
- SELECT code
- FROM buh.nom
- WHERE article = (SELECT article FROM buh.nom WHERE code=r.code) AND code != r.code
- )
- UPDATE tmp2 SET code_3 = (SELECT * FROM pre_select ORDER BY code DESC limit 1) WHERE code=r.code;
- WITH pre_select AS (
- SELECT code
- FROM buh.nom
- WHERE article = (SELECT article FROM buh.nom WHERE code=r.code) AND code != r.code
- )
- UPDATE tmp2 SET code_2 = (SELECT * FROM pre_select ORDER BY code ASC limit 1) WHERE code=r.code;
- END IF ;
- END LOOP ;
- EXECUTE('insert into for_input.reserve_' || table_name || ' select uid,
- nl,
- nu,
- code,
- unit,
- quantity,
- doc_type,
- i,
- code_2,
- code_3,
- article,
- code_upr_nom,
- dead_end
- from
- tmp2 ;');
- RETURN NULL ;
- END ;
- $$ ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement