postgresql - Using variables in SQL script -


i writing unit test postgresql functionality. starts inserting data in database , calling function. however, because use auto-incrementing ids, cannot know parameter put in function call.

i want able this:

insert mytable ...;  select id mytable l_id;  select my_function(l_id); 

updates

  • i using sql script, not pl/pgsql
  • in mysql can this: select @id:=itemid mytable;, later on, can use @id anywhere like.

drop schema tmp cascade; create schema tmp ; set search_path=tmp;  create table mytab     ( id integer not null primary key     ); insert mytab (id) select gs generate_series(1,10) gs;  create or replace function tmp.myfunc ( _int  integer ) returns text $func$  declare ret text;  begin     ret = 'omg_' || _int::text; return ret; end; $func$ language 'plpgsql'     ;  select myfunc(1);  select myfunc(mt.id) mytab mt     ; 

also, smaller things use psql's \gset command : (link documentation)


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -