| Author |
Topic |
|
Sudhindra
Starting Member
13 Posts |
Posted - 2008-12-30 : 01:08:25
|
| Hi, I have a table which has a AssignedtoID & AssignedbyID which are Pks, Other 3 tables have Depeartment,designation & Employee table contains name, designationName, DeparmentName for these Pks.Now I need to display in gridview like this:ASignedBy(name) Department Designation AssignedTo(name) Department DesignationI had a query writen like this & need to miodify further like this:-CREATE PROCEDURE EmpSelectVacApp_ByReqID --12 ( @ReqID int ) AS BEGIN SELECT dbo.VacationApprovalDt.AppReqID, dbo.VacationApprovalDt.ReqID, dbo.VacationApprovalDt.AssignTo, dbo.MstEmployee.EmpCode, (dbo.MstEmployee.FirstName +' '+ ISNULL(dbo.MstEmployee.MiddleName,' ') +' ' + dbo.MstEmployee.LastName) as FirstName, dbo.MstEmployee.DesigId, dbo.MstDesignation.DesigCode, dbo.MstDesignation.DesigName, dbo.MstEmployee.DeptId, dbo.MstDepartment.DeptCode, dbo.MstDepartment.DeptName,dbo.VacationApprovalDt.Status,dbo.VacationApprovalDt.Remarks,dbo.VacationApprovalDt.ModifiedBy FROM dbo.MstDepartment INNER JOIN dbo.MstEmployee ON dbo.MstDepartment.DeptId = dbo.MstEmployee.DeptId LEFT OUTER JOIN dbo.MstDesignation ON dbo.MstEmployee.DesigId = dbo.MstDesignation.DesigId Right OUTER JOIN dbo.VacationApprovalDt ON dbo.MstEmployee.EmpId = dbo.VacationApprovalDt.AssignTo where dbo.VacationApprovalDt.ReqID=@ReqID END Help would be really appreciated....Thanks in advance |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Sudhindra
Starting Member
13 Posts |
Posted - 2008-12-30 : 01:18:01
|
| VacationApprovalDt has AssignedToID, & AssignedByID is Modifiedby Column in that same table,ASsignedTo is Actually the Empcode & modifiedby is also the Employee code which is in MstEmployee.mstDesignation contains MstEmployee has only DesignationId whch is Pk of mstDesignation Table, MStEmployee has DepartmentID which is Pk of MstDepartment. So on that I need to join tables & get DesignationName (MstDesignation Table) & also DepartmentName (MstDepartment table),So my grid will look like this:AssignedBy Designation Department AssignedTo Designation Department(EmpCode) (His DesignationName) (His DepartmentName) (EmpCode) (His DesignationName) (His DepartmentName)MStEmployee:1)EmpCode2)NameMstDesignation:1)DsgnCode2)nameMStDepartment:1)DepCde2)NameVacationAppvlDt:1)AssignedTo2)ModifidedBy(AssignedBy) |
 |
|
|
Sudhindra
Starting Member
13 Posts |
Posted - 2008-12-30 : 01:19:54
|
| ohh Also VacationAppvlDt table has another Column1)ApprvlID(pk)2)requestID(ReqID)3)AssignedTo4)ModifidedBy(AssignedBy)& i am going to use RequestID to query to get the Modifiedby & Assigned to IDs & all other details |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-30 : 01:25:41
|
| [code]CREATE PROCEDURE EmpSelectVacApp_ByReqID --12 ( @ReqID int ) AS BEGIN SELECT v.ModifiedBy AS AssignBy,desg1.DesignationName,dept1.DepartmentName,v.AssignedTo,desg2.DesignationName,dept2.DepartmentNameFROM VacationApprovalDt vJOIN MStEmployee e1ON e1.EmpCode=v.ModifiedByJOIN MStEmployee e2ON e2.EmpCode=v.AssignedToJOIN MstDesignation desg1ON desg1.DesignationId=e1.DesignationIdJOIN MstDepartment dept1ON dept1.DepartmentID =e1.DepartmentID JOIN MstDesignation desg2ON desg1.DesignationId=e2.DesignationIdJOIN MstDepartment dept2ON dept2.DepartmentID =e2.DepartmentID WHERE v.RequestID=@ReqIDEND[/code] |
 |
|
|
Sudhindra
Starting Member
13 Posts |
Posted - 2008-12-30 : 01:39:50
|
| Hey visakh16, thanks the query works with a LEFT Outer join. I am getting the result.Thnak you so much. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-30 : 01:41:58
|
| welcome |
 |
|
|
Sudhindra
Starting Member
13 Posts |
Posted - 2008-12-30 : 01:44:12
|
| The only Problem is the 2nd last column in the result thats DesignationName is coming as NULL when i use left join. Without including "Left join" the query doesnt produce any result. |
 |
|
|
Sudhindra
Starting Member
13 Posts |
Posted - 2008-12-30 : 01:57:32
|
| Ohh SOrry I got it Thankyou, spellingmistake was causing it. Cant thank you enough |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-30 : 01:58:02
|
quote: Originally posted by Sudhindra The only Problem is the 2nd last column in the result thats DesignationName is coming as NULL when i use left join. Without including "Left join" the query doesnt produce any result.
that means you dont have any records in MstDesignation table that matches the DesignationId values in MStEmployee table corresponding to AssignedTo emp code value |
 |
|
|
|