Showing posts with label pipelined. Show all posts
Showing posts with label pipelined. Show all posts

Sunday, September 27, 2015

Function to let user B see all tables of user A



In case you do not want to grant a user access to data dictionary tables like DBA_TABLES, but will let user B see the list of all tables belonging to user A, you can work around it with a pipelined function in schema A:

create type str_set as table of varchar2(30);
/

create or replace function a_tables return str_set pipelined is
l_str varchar2(30);
begin
for l_str in (select table_name from user_tables)
loop
pipe row(l_str.table_name);
end loop;
return;
end;
/
grant execute on a_tables to B;

Then user B can see the list of A's table with:

select * from table(a.a_tables);

Trying to solve this with a view in schema A that selects on USER_TABLES does not work, but prove me wrong, please.

Saturday, March 27, 2010

Collatz conjecture in PL/SQL

A simple implementation of Collatz conjecture in PL/SQL:


create or replace type int_tab_typ is table of integer;
/

create or replace function collatz(p_n in integer)
RETURN int_tab_typ PIPELINED as
n integer;
BEGIN
if p_n < 1 or mod(p_n,1)>0 then
RETURN ;
end if;
n:=p_n;
while n > 1
loop
pipe row (n);
if mod(n,2)=1 then
n:=3*n+1;
else
n:=n / 2;
end if;
end loop;
pipe row(n);
end;
/

select * from table(collatz(101));


More on Collatz Conjecture (Wikipedia).

The point? Just for fun. Showed it to my 11 years old kid and asked him to figure out the 17 steps sequence of collatz(7) in his head. "Is this going to stop?", he asked half through. "That is what we are going to find out, nobody really knows", I told him.

If you have absolutely nothing useful to do, but have CPU and storage to waste, you may create a table containing n and the number of steps for collatz(n), with values of n ranging from 1 to 231-1 or until storage is exhausted, whichever comes first. Before doing so you may consider changing the function above to just count the steps and look up previous calculations from the table.