if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetKartListCount]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetKartListCount]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE [dbo].[GetKartListCount] (@MODE int = 1) AS
begin
	DECLARE @Count int
	DECLARE @S nvarchar(1000), @Prm nvarchar(50), @UserName nvarchar(50)	
	
	set @UserName = current_user
	if (@MODE = 1)/*Штатные*/
	begin
	    	set @S = N'select @Count = count(*) FROM ['+@UserName+'].GKL_01'
		set @Prm = N'@Count int OUTPUT'
		exec sp_executesql @S,@Prm,@Count OUTPUT  
	end
	else
	if (@MODE = 2) /*Уволенные*/
	begin
		set @S = N'select @Count = count(*) FROM ['+@UserName+'].GKL_02'
		set @Prm = N'@Count int OUTPUT'
		exec sp_executesql @S,@Prm,@Count OUTPUT
	end
	else
	if (@MODE = 3)  /*Все*/
	begin       
		set @S = N'select @Count = count(*) FROM ['+@UserName+'].GKL_03'
		set @Prm = N'@Count int OUTPUT'
		exec sp_executesql @S,@Prm,@Count OUTPUT
	end
	else
		Set @Count = -1
RETURN @Count
end
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

GRANT  EXECUTE  ON [dbo].[GetKartListCount]  TO [public]
GO

