IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[TrainingSignIn]') AND type = 'P')
DROP PROCEDURE [dbo].[TrainingSignIn]
GO

CREATE proc [dbo].[TrainingSignIn]( 

@str nvarchar(max),
@ordid bigint
)
as
begin
declare @strone nvarchar(200),
@index nvarchar(10)=','
declare @Train table(sfz nvarchar(20),cs int)
while @str > ''
begin
set @index = CHARINDEX(',',@str)
if(@index>0)
begin
set @strone =substring(@str,1,@index-1)
set @str = substring(@str,@index+1,LEN(@str))
end
else 
begin
set @strone =@str
set @str = ''
end
insert into @Train(sfz) values(@strone)
end

 update order_48_105 set ord2_sqdcs = 0  where ord2_ordid  = @ordid
 
update r
set r.ord2_sqdcs = n.c
from order_48_105  r,
(select sfz,COUNT(1) c from @Train group by sfz) n
where r.ord2_sfz = n.sfz and r.ord2_ordid = @ordid

select 'ok' result 


end