Here's what I need to do. For every Employee Id in a table Budget I need to run this function that I've created.
Begin
declare @Array varchar(1000),
@separator char(1),
@total int,
@p int,
@period int,
@separator_position int,
@array_value varchar (100),
@count int,
@output1 varchar;
--set nocount on
set @array = (select [period-budget] from [budget-detail] b
where b.[budget-year] = '2003' and class = 'CHG' and [emp-id] = @userId);
set @separator = ';';
set @p = 9--@pd; --this is the period in...
set @total = 0;
set @period = 0;
set @count = 1;
set @array = @array + @separator
while patindex('%' + @separator + '%' , @array) <> 0 and @count <= @p
begin
select @separator_position = patindex('%' + @separator + '%' , @array)
select @array_value = left(@array, @separator_position - 1);
-- @array_value holds the value of this element of the array
--select Array_Value = @array_value;
set @total = @array_value + @total;
if @count = @p set @period = @array_value;
-- This replaces what we just processed with and empty string
select @array = stuff(@array, 1, @separator_position, '')
set @count = @count + 1;
end
set @output1 = str(@total) + ',' + str(@period )
--set nocount off
return @output1
end
I have it as a user defined function, but haven't been able to get it to work.
Here is what I'd like
select emp-id, and then the output from the function
from budget-detail.
Please HELP!!
Thanks,
Dustin
Begin
declare @Array varchar(1000),
@separator char(1),
@total int,
@p int,
@period int,
@separator_position int,
@array_value varchar (100),
@count int,
@output1 varchar;
--set nocount on
set @array = (select [period-budget] from [budget-detail] b
where b.[budget-year] = '2003' and class = 'CHG' and [emp-id] = @userId);
set @separator = ';';
set @p = 9--@pd; --this is the period in...
set @total = 0;
set @period = 0;
set @count = 1;
set @array = @array + @separator
while patindex('%' + @separator + '%' , @array) <> 0 and @count <= @p
begin
select @separator_position = patindex('%' + @separator + '%' , @array)
select @array_value = left(@array, @separator_position - 1);
-- @array_value holds the value of this element of the array
--select Array_Value = @array_value;
set @total = @array_value + @total;
if @count = @p set @period = @array_value;
-- This replaces what we just processed with and empty string
select @array = stuff(@array, 1, @separator_position, '')
set @count = @count + 1;
end
set @output1 = str(@total) + ',' + str(@period )
--set nocount off
return @output1
end
I have it as a user defined function, but haven't been able to get it to work.
Here is what I'd like
select emp-id, and then the output from the function
from budget-detail.
Please HELP!!
Thanks,
Dustin