CREATE PROCEDURE dbo.DepartmentInsertAuto AS
/*
  NB20220126 CreatedDependencies Atomation 
  Ensure Access rights and department rights 4 AutoNight & AutoDay
*/
SET NOCOUNT ON

INSERT INTO PersonelDepartmentsXtab (UserIDNumber, FK_Department) 
SELECT UserIdNumber, Pk_Department FROM Personel, Departments WITH (NOLOCK) 
WHERE UserTypeNumber IN (3,4)  -- AutoDay/AutoNight
EXCEPT
SELECT UserIDNumber, FK_Department FROM PersonelDepartmentsXtab WITH (NOLOCK)

INSERT INTO PersonelAccess (UserIDNumber, AreaFunctionNumber, Access, AreaNumber) 
SELECT DISTINCT UserIDNumber, Number, 0, 10 FROM Personel, iter_intlist_to_table_No_Dummy('0,1,2,3,4,5,6,7,8,9') -- TSelectorAreaFunctions
WHERE UserTypeNumber IN (3,4)  -- AutoDay/AutoNight
EXCEPT
SELECT DISTINCT UserIDNumber, AreaFunctionNumber, 0, AreaNumber FROM PersonelAccess WITH (NOLOCK) 
WHERE AreaNumber = 10 AND AreaFunctionNumber Between 0 AND 9
