0

T-SQL gurus, what is causing the following procedure to return this error "Select statements included within a function cannot return data to a client."?

create function dbo.func_get_times_for_stop(@stopId as varchar(5))
returns varchar(max) as
begin 
    declare @arrival_times varchar(max)
    set @arrival_times = '';
    declare @arrival_time varchar(5)
    declare arrival_cursor cursor for
        select arrival from schedules where stopid=@stopId;

    open arrival_cursor;
    fetch next from arrival_cursor;
    while @@fetch_status = 0
       begin
          fetch next from arrival_cursor into @arrival_time;    
          set @arrival_times += ',' + @arrival_time;    
       end;
    close arrival_cursor;
    deallocate arrival_cursor;

    return  @arrival_times;
end;

I am simply trying to return a scalar which comprises the string concatenated from arrival times. I do not see where I am returning a select statement.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Klaus Nji
  • 18,107
  • 29
  • 105
  • 185
  • 1
    Related: [SQL Server: Can I comma delimit...](http://stackoverflow.com/questions/2046037/sql-server-can-i-comma-delimit-multiple-rows-into-one-column) – OMG Ponies Feb 26 '12 at 05:33
  • @OMG Ponies, thanks for related link. This would be even nice if I can get it to work in my case. – Klaus Nji Feb 26 '12 at 13:01

2 Answers2

3

Could it be this line?

fetch next from arrival_cursor;

It should be fetching into @arrival_time.

John Pick
  • 5,562
  • 31
  • 31
1

Here:

fetch next from arrival_cursor;

you need

fetch next from arrival_cursor into @arrival_time;
Oleg Dok
  • 21,109
  • 4
  • 45
  • 54