Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Insert rows


Insert rows

Author
Message
Karan_W
Karan_W
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 135
I have two tables user and user_role. Userid is primary filed into user table and foreign key in user_role table.
User_role has userid, roleid fileds.

There were some rows already into user table and corresponding to these userid fileld into user_role also.

User table example----
Userid loginid fname
1 a john
2 b karry

user_role example-----

user_id roleid
1 10
1 11
2 10
2 15

Later on I inserted some new rows into user tables ,
Now user table is like this----

user table---
Userid loginid fname
1 a john
2 b karry
3 america\c john
4 america\d karry

so i want that this new userid to be inserted into user_role table also with their already existing roleid.

Now the user_role table should like this---

user_id roleid
1 10
1 11
2 10
2 15
3 10
3 11
4 10
4 15

Please send me the query to work in sql server 2005.
LutzM
LutzM
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7011 Visits: 13559
You could use a subquery or CTE to get the roles per fname (including the user_id from the user_role table) and join that back to the user table. Based on that, insert the new values.

Please note that I decided against the option to provided the code snippet since I think you should give it a try based on the description. If you get stuck, post back your current query and what you're struggling with.



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
weitzera
weitzera
SSC Eights!
SSC Eights! (894 reputation)SSC Eights! (894 reputation)SSC Eights! (894 reputation)SSC Eights! (894 reputation)SSC Eights! (894 reputation)SSC Eights! (894 reputation)SSC Eights! (894 reputation)SSC Eights! (894 reputation)

Group: General Forum Members
Points: 894 Visits: 629
I'm afraid I don't quite follow exactly what you're getting stuck on. Is it how to get the keys back from the rows you just entered? There have been a number of posts and articles on this site discussing how to do that.



Dan Guzman - Not the MVP (7/22/2010)
All questions have to be prefaced by Server version and 'according to MS Docs' or 'my own personal opinion based on how much detail I felt like digging into at the time.'

Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14978 Visits: 38999
you need to decide that the new user is going to copy/inherit the roles of a specific user.
for example:

insert into user_role
select
3, --the userid of the new person who will receive/inherit the roles
r.roleid
from [role] r
where r.userid IN(SELECT userid from [user] u WHERE fname = 'john')--he was userid 1 in your example




Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

kinsey1275
kinsey1275
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 9
I realize that this doesn't directly answer your question, but why did you create new user ids for these users? Was there not a way to preserve their existing IDs and update other information about the users?
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47388 Visits: 44399
Please note: 3 year old thread.


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search