---- IsInSubworkTypes ----
  -- проверяет, является ли @SubworkTypeID подработой для @WorkTypeID (в Справочнике_подработ)
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[IsInSubworkTypes]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[IsInSubworkTypes]
GO
create function IsInSubworkTypes(@SubworkTypeID integer, @WorkTypeID integer)
returns bit
as
begin 
  declare @result bit      
  declare @CurWorkTypeID integer
  set @result = 0
  declare Cur cursor for select Код_вида_работы from Справочник_подработ where код_вида_подработы = @SubworkTypeID
  open Cur
  fetch next from Cur into @CurWorkTypeID
  while (@result = 0) AND (@@fetch_status = 0) AND (@WorkTypeID <> @CurWorkTypeID)
  begin
    set @result = dbo.IsInSubworkTypes(@CurWorkTypeID,@WorkTypeID)
    fetch next from Cur into @CurWorkTypeID
  end
  if @WorkTypeID = @CurWorkTypeID set @result = 1
  return @result
end
