Quantcast
Channel: VBForums - Database Development
Viewing all articles
Browse latest Browse all 2918

SQL Server - Dynamically create left joins to unknown # of tables

$
0
0
I have a sproc that right now is hardcoded to support a join to 4 tables like so:

Code:

LEFT JOIN xtblJobProjectEmployeeRoles rolePA on (rolePA.Control = Jobs.relProjectControl and rolePA.Role = 1)
LEFT JOIN xtblJobProjectEmployeeRoles revw on (revw.Control = Jobs.relProjectControl and revw.Role = 2)
LEFT JOIN xtblJobProjectEmployeeRoles tech on tech.Control = Jobs.relProjectControl and tech.Role = 3
LEFT JOIN xtblJobProjectEmployeeRoles SE on SE.Control = Jobs.relProjectControl and SE.Role = 4

We have the concept of a project and there are certain roles to a project, such as 1 being the PA (project administrator), 2 is the reviewer, 3 is the tech and 4 is the SE (system engineer). At this time, there are only 4 roles, but we want to be able to not be limited and in the future maybe there will be a 5th role which will need a 5th join. So this table would contain all our defined roles:

Name:  xtblProjectRoles as of 6-3-22.jpg
Views: 21
Size:  25.0 KB

Prior to the LEFT JOIN's in the sproc is where the data are used so this would have to be dynamic too:
Code:

SELECT        rolePA.Employee AS EmpAssignedPA,
        revw.Employee AS EmpReviewTechnicalLead,
        tech.Employee AS EmpSystemTechnician,
        SE.Employee AS EmpSystemEngineer,

Being a stronger C# developer than sql, I am thinking I could write this into a query that I dynamically created and ran rather than a stored procedure, but wanted to get people's opinions on alternative ways to accomplish this. Thanks!

Thanks!
Attached Images
 

Viewing all articles
Browse latest Browse all 2918

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>