CREATE PROCEDURE [dbo].[fetch_team_lead]
    @team_tech_id nvarchar(max) = '',
    @fields nvarchar(max) = '',
    @department_ids nvarchar(max) = '',
    @filterClause nvarchar(max) = ''
AS
    declare @select_query nvarchar(max) = '';
    SET @fields = iif(@fields = '','*',@fields);
    SET @select_query = 'select '+@fields+' from dim_users where is_team_lead = 1'
    IF  @team_tech_id != ''
        BEGIN
            SET @select_query = @select_query + ' and  (my_team_tech_id in (' + @team_tech_id + ') OR user_status = 0)'
        END
    IF @department_ids != ''
    	BEGIN
            SET @select_query = @select_query + ' and  my_team_dept_id in (' + @department_ids + ')'
        END
    IF @filterClause != ''
        BEGIN
            SET @select_query = @select_query + @filterClause
        end
    SET @select_query = @select_query + ' order by display_name asc'
   EXEC sp_executesql @select_query;
go

