... until the collector arrives ...

This "blog" is really just a scratchpad of mine. There is not much of general interest here. Most of the content is scribbled down "live" as I discover things I want to remember. I rarely go back to correct mistakes in older entries. You have been warned :)

2007-10-26

Oracle and SQL Server Table Functions

For reference, here are examples of table-valued functions in both Oracle and SQL Server

Oracle:

create or replace type range_row_type as object(n number);

create or replace type range_table_type as table of range_row_type;

create or replace function number_range(startNumber in number, endNumber in number)
return range_table_type
pipelined
is
  result_row range_row_type := range_row_type(0);
begin
  for i in startNumber .. endNumber loop
    result_row.n := i;
    pipe row(result_row);
  end loop;
  return;
end;

select * from table(number_range(10,20));

SQL Server:

create function number_range (@start int, @end int)
returns @sequence table (n int)
as begin
  declare @i int
  set @i = @start
  while @i <= @end begin
    insert into @sequence(n) values(@i)
    set @i = @i + 1
  end
  return
end
go

select * from number_range(10,20)

Blog Archive