You might want to try some of the following queries to update your fields.when a request is modified you will have the current ID.So,SELECT (Max(SubTask) + 1) as NewSubTask FROM Tasks WHERE Id = @JobID
When a request is add you do not have a job id so you need to create onSELECT (Max(Id) + 1) as NewJobID FROM Tasks
You could easily wrap all of this in a stored procedureCREATE PROCEDURE sp_InsertJobTask (@JobID integer,@Col1 varchar,@Col2 varchar)-- i am assuming Col1 and Col2 to be extra columns that you might needif (@JobID Is NULL) begin --we don't have a current job so we need to add on INSERT INTO Tasks (Id, Col1, Col2) VALUES ((SELECT (Max(Id) + 1) as NewJobID FROM Tasks), Col1, Col2)end else begin INSERT INTO Tasks (Id, SubTask, Col1, Col2) VALUES (@JobID, (SELECT (Max(SubTask) + 1) as NewSubTask FROM Tasks WHERE Id = @JobID), Col1, Col2end
Got SQL?