Click here to Skip to main content
14,875,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can we use insert command of SQL for inserting the data in tables having many to many relationships. Can it be done by saparate queries or we done it with the one query. as there are three tables.

1 solution

There's only one insert. You take the id of the user and the id of the role. But, I'd do it in a stored proc that does two things. The first is, check if that pair of values is in the table already. If it is, don't try to insert it again. If it's not, then insert it.
codegeekalpha 14-Aug-11 21:37pm
Can u give some example ??? thzx
codegeekalpha 14-Aug-11 21:52pm
Is it like this.. select userid, groupid from role where username=christian else insert userid,groupid in role where username=christian and rolename=admin
Christian Graus 14-Aug-11 22:33pm
No. Not even close. You want to do a select count to find out if a record exists, and you do NOT want to store the username or the role name, you want to store IDs.

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