Question

I am using SQL and I have 4 tables.

  1. tbl_project(project_id is primary key)
  2. tbl_task (Contains task_id as primary key and project_id as foreign key)
  3. tbl_assignment (Contains assignment_id as primary key task_id as foreign key)
  4. tbl_document (Contains document_id as primary key and assignment_id as foreign key),

    • A project can have one or more tasks
    • A task can have one or more assignemnts
    • An assignemnt can have one or more documents

Now i need to write an stored procedure which will validate a project and returns the validation results, where project id will be passing as parameter.

  1. Need to check project having atleast one task.
  2. All the Task under this project is having atleast 1 assignment
  3. All the assignment under this project/task having atleast 1 document

I am having a temp table where I need to insert the validation errors like "Task - Task1 doesnt contains any assignment", "Assignemnt - Assignment2 doesnt contains any documnets " etc.

Is there any way to implement this logic without using Cursors

Was it helpful?

Solution

You don't need cursors for this type validation. Here are three queries that do this:

Projects with no tasks:

select p.*
from tbl_projects p left outer join
     tbl_tasks t
     on p.project_id = t.project_id
where t.project_id is null;

Tasks have at least one assignment:

select p.*
from tbl_tasks t left outer join
     tbl_assignments a
     on a.task_id = t.task_id
where a.task_id is null;

All assignments have at least one document:

select p.*
from tbl_assignments a left outer join
     tbl_documents d
     on a.assignment_id = d.assignment_id
where d.assignment_id is null;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top