Error in setting a value in sql server trigger I'm trying to make a trigger that checks which staff member have the least amount of assigned tasks, before adding the task, and assign it to that member.
What I came up with was something like this:

CREATE TRIGGER trgInsteadOfInsert ON helpCase
INSTEAD OF Insert
AS
declare 
 @helpCaseId int,
 @problemTypeId int,
 @problemDescribtion varchar(200) ,
 @staffId int,
 @problemSolution varchar(200),
 @operatingSystemTypeId int,
 @applicationTypeId int,
 @helpCaseOpenDate datetime,
 @helpCaseclosedDate datetime;

select @helpCaseId=i.helpCaseId from inserted i;
select @problemTypeId=i.problemTypeId from inserted i;
select @problemDescribtion=i.problemDescribtion from inserted i;

set @staffId = (select Min(ID) frOM (SELECT staffId as ID, COUNT (*) FROM helpCase where helpCaseClosedDate IS NULL GROUP BY staffId)) ;

select @problemSolution=i.problemSolution from inserted i;
select @operatingSystemTypeId=i.operatingSystemTypeId from inserted i;

select @helpCaseOpenDate=i.helpCaseOpenDate from inserted i;
select @helpCaseclosedDate=i.helpCaseclosedDate from inserted i;



BEGIN 

Insert into helpCase values(@helpCaseId,@problemTypeId,@problemDescribtion,@staffId,@problemSolution,@operatingSystemTypeId,@helpCaseOpenDate,@helpCaseclosedDate);
END


but I'm getting the following error for the line that I'm setting the value of @staffId in : 

Incorrect syntax near ')'. Expecting AS,ID or QOUTED_ID.


Any idea what should I do!?
You have a derived table in the from clause without an alias.

This will give you the same error:

select *
from (
     select *
     from YourTable
     );


Add an alias to the derived table.

select *
from (
     select *
     from YourTable
     ) as T;


Note: Your trigger will fail if you insert more than one row at a time. The trigger is executed once per insert statement, not once per row.