Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

how to choose proper tableName from multiple tables for creating join query

$
0
0

I want to create join  query dynamically which contains following tables.(Tables contains large amount of data suppose 100 records)

Table 1)Usertable:     Which contains userid, userfirstname, userlastname

Table 2)grouptable:  Which contains  groupid, groupname, groupdescription

Table 3)usergroup : (contains relation between usertable and grouptable)

Table 4)groupRoles:  which contains relation between groupid and roleid(any  random column)

Case1:

But If I want to find Groupid is present/common  in which tables it gives result like this

Query:

I have 1 form which accepts only userid.

By using userid we have To find:   Userid,Userfirstname, Userlastname and groupname

Problem:

But the problem is while creating dynamic query for join clause

As in case 1 there are 2 options of tables either grouproles or usergroup

So how I will come to know which table to select from these 2 options so that it takes me to the grouptable via relational table which is usergroup table in my case

As in my case I have to find Userid,Userfirstname, Userlastname and groupname

I can get the Userid,Userfirstname and Userlastname from usertable

But,to get the groupname column which is present in  grouptable we have to take usergroup table for relation to get the join query.

So how we can come to know which table to select from either of  tableNames (as in case1 ) dynamically, so that we can get the GroupName column from query

Thank you.


Viewing all articles
Browse latest Browse all 4871

Trending Articles