Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
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 enter image description here:

TableName | ColumnName
-------------|------------------
1) GroupRoles | Groupid
2) UserGroup |Groupid

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 dynamically 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.
Posted

1 solution

hello friend please review below script, i have created temporary table based on your description, and using that table structure i also added select query which is select your desired fields from tables, you need to modified this to your actual data structure..

I have excluded GROUP ROLES in join, if you need to select any role based on that, then you can add that too in selection..

SQL
DECLARE @UserTable AS TABLE (
	USERID INT,
	USERFIRSTNAME VARCHAR(10),
	USERLASTNAME VARCHAR(10)
)
DECLARE @GROUPTABLE AS TABLE (
	GROUPID INT,
	GROUPNAME VARCHAR(10),
	GROUPDESCRIPTION VARCHAR(20)
)
DECLARE @USERGROUP  AS TABLE (
	USERID INT,
	GROUPID INT
)
DECLARE @GROUPROLES  AS TABLE (	
	GROUPID INT,
	ROLEID INT
)

INSERT INTO @UserTable VALUES(1,'ABC1','XYZ1')
INSERT INTO @UserTable VALUES(2,'ABC2','XYZ2')
INSERT INTO @UserTable VALUES(3,'ABC3','XYZ3')

INSERT INTO @GROUPTABLE VALUES(1,'GROUP1','GROUP1 DESC')
INSERT INTO @GROUPTABLE VALUES(2,'GROUP2','GROUP2 DESC')

INSERT INTO @USERGROUP VALUES (1,1)
INSERT INTO @USERGROUP VALUES (2,2)
INSERT INTO @USERGROUP VALUES (3,1)

INSERT INTO @GROUPROLES VALUES(1,1)
INSERT INTO @GROUPROLES VALUES(2,2)



SELECT UserTable.USERID,UserTable.USERFIRSTNAME,UserTable.USERLASTNAME,GroupTable.GROUPNAME 
FROM @UserTable AS UserTable
INNER JOIN @USERGROUP AS UserGroupTable ON UserGroupTable.USERID = UserTable.USERID
INNER JOIN @GROUPTABLE AS GroupTable ON GroupTable.GROUPID = UserGroupTable.GROUPID
--INNER JOIN @GROUPROLES AS GroupRoles ON GroupTable.GROUPID = GroupRoles.GROUPID 
WHERE UserTable.USERID = 1


Output is like this...

-- by passing userid as 1
USERID	USERFIRSTNAME	USERLASTNAME	GROUPNAME
1	ABC1	            XYZ1	GROUP1


-- by passing userid as 3
USERID	USERFIRSTNAME	USERLASTNAME	GROUPNAME
3	ABC3	            XYZ3	GROUP1


-- by passing userid as 2
USERID	USERFIRSTNAME	USERLASTNAME	GROUPNAME
2	ABC2	            XYZ2	GROUP2
 
Share this answer
 
Comments
Alia Sharma 11-Mar-14 5:43am    
hello Tejas,Thank u very much for your reply.I got your query.
But my actual query is something that should decide from either of tables from case1 which table to select according to query requirements.
Is there any way to decide dynamically which table will take us to our desire query.
Tejas Vaishnav 11-Mar-14 5:50am    
you need to query output like UserID, FirstName, LastName and GroupName.. am i right?
your table UserGroup is having relation between your user table and group table, so if you need to select user info as well as your group info then you need to use that table..
from your description i don't thing there is no any direct relation between your user table, and GROUPROLES table, so how you can use that table in your select query..?
Alia Sharma 11-Mar-14 6:02am    
hello friend,thank u so much for your replies.I really appreciate your efforts.

But my main concern is that "how I can dynamically decide from query or some code that which table will be suitable for my query from the result of case1".

As in case1 two tables i.e. GROUPROLES and USERGROUP are having the similar columnName (groupid).And Groupname is present in Grouptable and its relation is in usergroup which can be used to create the join query.

I know the query that I have to use which table but I want to know it dynamically that which tableName will be useful for me to create join query i.e.which table is appropriate for query from either of two tables which is present in case1.
Tejas Vaishnav 11-Mar-14 6:38am    
Ok, your both table having userid that what i am asking for any relation with Grouproles and usergroup to usertable.

you can select any of the table either group roles or usergroup based on your condition like if you need to know only user details with just group name then use usergroup table, but if you need to check rights also then use Grouproles table.

if you are going to display different views of your web app /website like normal user have this type of view, admin has different view then use group roles table to check rights also.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900