I apologize if the title for the question doesn't match what I am asking what to do. I am not sure how to describe what I am trying to accomplish.
I have the following tables in my database:
Table customers
customer_id (PK, int, NN)
customer_name (varchar(100), NN)
Table jobs
job_id (PK, int, NN)
job_number (varchar(10), NN)
job_description (varchar(MAX), NN)
customer_id (FK, int, NN)
Table job_statuses
status_id (PK, int, NN)
status_description (varchar(50), NN)
Table job_status_logs
log_id (PK, int, NN)
job_id (FK, int, NN)
status_id (FK, int, NN)
log_date (date, NN)
Now I am trying to program a procedure where I pass it a Job Number and it does the following:
1) Checks if the Job Number exists within the database, if not, return an error message.
2) If the Job Number exists return the following columns from the following tables:
jobs: job_id, job_number, job_description
customers: customer_name
job_status_logs: log_date (WHERE status_description is 'Open') & log_date (WHERE status_description is 'Closed')
I am mainly having an issue with the very last part, as I am unsure how to form the query looking for the last two points of data (the ones from the job_status_logs
table).
EDIT: This is currently what I have for the procedure
CREATE PROCEDURE dbo.GetJobData(
@JobNum VARCHAR(10),
@response NVARCHAR(500) OUTPUT
) AS
BEGIN
DECLARE @JobID INT
SELECT @JobID = job_id
FROM jobs
WHERE job_number = @JobNum
IF @JobID IS NULL
BEGIN
SET @response = 'Cannot find data for Job "' + @JobNum + '". Job does not exist.'
RETURN
END
SELECT j.job_id AS [ID], j.job_number AS [Job], j.job_description AS [Description], c.customer_name AS [Customer]
FROM jobs AS j INNER JOIN
customer AS c ON c.customer_id = j.customer_id
WHERE j.job_id = @JobID
END
GO
As stated above, I do not know how to set up the query to grab the data I am looking for from the job_status_logs
table.