| Author |
Topic |
|
soniyakapoor03
Starting Member
11 Posts |
Posted - 2006-03-01 : 08:20:09
|
| i am new to sql serveri want to insert five rows continously in sql server database.my problem is like this .i am inserting user datails. i want to get the userid which is an identity column.with the userid i want to insert userid and roleid in another table.Roleid values 1 to 5. ie constant.an sql procedure is required. |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-03-01 : 08:25:52
|
| Can u give ur table structure, some sample data and what needs to be inserted into what ? |
 |
|
|
soniyakapoor03
Starting Member
11 Posts |
Posted - 2006-03-01 : 09:10:28
|
quote: Originally posted by Srinika Can u give ur table structure, some sample data and what needs to be inserted into what ?
first I need to insert userdetails into the user tablein that userid is an identity column.with the userid i need to insert into another table called UserRoles.here I had to take another value Role id. from Roles tableTables user columns-Userid(int,identity column),UserNameRoles columns-Roleid(int,identity column),RoleDescriptionuserroles columns--RoleID(int) , UserID(int).user TableUser id user name1- a2- d3- d4- f5- gRoles tablerole id role desc1- a2- a3- a4- a5- a6- a role id may increase in future.finally i want like this foreg UserId =1UserRoles tablesrole id UserId1- 12- 13- 14- 15- 16- 1 |
 |
|
|
under2811
Constraint Violating Yak Guru
366 Posts |
Posted - 2006-03-01 : 09:38:02
|
| hi..that is bit confusing....can you please explain it in easier way so that we all.. as well as you also understand the problem.......Papillon |
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-03-01 : 09:40:51
|
For the first 2 tables u can insert the sample data u give as followsInsert into [user] ([Userid], [UserName])Select 1, 'a' Union allSelect 2, 'd' Union allSelect 3, 'd' Union allSelect 4, 'f' Union allSelect 5, 'g' Insert into [Roles] ([Roleid], [RoleDescription])Select 1, 'a' Union allSelect 2, 'a' Union allSelect 3, 'a' Union allSelect 4, 'a' Union allSelect 5, 'a' Union allSelect 6, 'a' I can't understand the data of 3rd tableIs it to be filled with the combination of data in above 2 tables ?eg.roleid UserId1- 12- 13- 14- 15- 16- 11- 22- 23- 24- 25- 26- 21- 32- 33- 34- 35- 36- 31- 42- 4........ |
 |
|
|
soniyakapoor03
Starting Member
11 Posts |
Posted - 2006-03-01 : 10:02:46
|
quote: Originally posted by Srinika For the first 2 tables u can insert the sample data u give as followsInsert into [user] ([Userid], [UserName])Select 1, 'a' Union allSelect 2, 'd' Union allSelect 3, 'd' Union allSelect 4, 'f' Union allSelect 5, 'g' Insert into [Roles] ([Roleid], [RoleDescription])Select 1, 'a' Union allSelect 2, 'a' Union allSelect 3, 'a' Union allSelect 4, 'a' Union allSelect 5, 'a' Union allSelect 6, 'a' I can't understand the data of 3rd tableIs it to be filled with the combination of data in above 2 tables ?eg.roleid UserId1- 12- 13- 14- 15- 16- 11- 22- 23- 24- 25- 26- 21- 32- 33- 34- 35- 36- 31- 42- 4........
hi sorry for the confusion causedi am little tensed today is last day for project submission. I need to insert only in One table called UserRoles.Roles table is already loaded table.I am able to insert into UserTable.When i am creating a new user in USER Table it should insert the ( Roleid from Role table) and (userId from User table) into UserRoles table for eg new User id=10UserRoles TableRoleID UserID1 102 103 104 105 106 10 I want an sql procedure for taking UserID from User table and RoleID from Roles table and insert into UserRoles Table |
 |
|
|
soniyakapoor03
Starting Member
11 Posts |
Posted - 2006-03-01 : 10:03:28
|
quote: Originally posted by under2811 hi..that is bit confusing....can you please explain it in easier way so that we all.. as well as you also understand the problem.......Papillon
hi sorry for the confusion causedi am little tensed today is last day for project submission. I need to insert only in One table called UserRoles.Roles table is already loaded table.I am able to insert into UserTable.When i am creating a new user in USER Table it should insert the ( Roleid from Role table) and (userId from User table) into UserRoles table for eg new User id=10UserRoles TableRoleID UserID1 102 103 104 105 106 10 I want an sql procedure for taking UserID from User table and RoleID from Roles table and insert into UserRoles Table |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2006-03-01 : 10:12:23
|
| step 1: Add your user to you user table:Insert into Users (....) VALUES (....)Step 2: get the Identity inserted using the Scope_Identity() function:declare @id intset @id = scope_identity()step 3: Add the roles from your roles table into your UserRoles table:insert into UserRoles (UserID, RoleID)select @ID, RoleIDfrom RolesIf there are only certain default roles that you want to add (not all of them) then be sure to have a column in your Roles table to indicate this. I..e, something like this:insert into UserRoles (UserID, RoleID)select @ID, RoleIDfrom Roleswhere Roles.DefaultRole = 1 |
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-03-01 : 10:17:39
|
| The code should be:Insert Into UserRoles (RoleID, UserID )Select Roleid, UrInputValueForuserTbl from [Roles]UrInputValueForuserTbl has to be used in different ways depending on the situationeg1. If it is from the Front End, the SQL string needs to be prepared and sent to the DB (Not recommended)eg2. If it is from a Stored Procedure, the data coming as a passed parameter and that has to be taken in to the queryeg3. If it is from a trigger, the data has to be captured from "Inserted" |
 |
|
|
soniyakapoor03
Starting Member
11 Posts |
Posted - 2006-03-01 : 12:18:09
|
quote: Originally posted by jsmith8858 step 1: Add your user to you user table:Insert into Users (....) VALUES (....)Step 2: get the Identity inserted using the Scope_Identity() function:declare @id intset @id = scope_identity()step 3: Add the roles from your roles table into your UserRoles table:insert into UserRoles (UserID, RoleID)select @ID, RoleIDfrom RolesIf there are only certain default roles that you want to add (not all of them) then be sure to have a column in your Roles table to indicate this. I..e, something like this:insert into UserRoles (UserID, RoleID)select @ID, RoleIDfrom Roleswhere Roles.DefaultRole = 1
hi still problem exists i want to vary the values of RoleiD .RoleIDuserId is constantRoleiD should be like 1,2,3,4,5,6.i am confusedshall i have to use cursor |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2006-03-01 : 13:13:59
|
| No. the roles are being selected from your Roles table, as I described. Read up on how INSERT works in Books on-line, and also practice doing inserts from SELECT's (as I have shown you) to get a feel for it. |
 |
|
|
|