I am trying to clone user permissions. The permissions are driven from a table with only 2 columns, userid and groupid. Both are foreign keys.
Is it possible to insert multiple rows based on criteria? Let's say:
USERID GROUPID
1 A
1 B
1 C
1 D
I would like to insert rows to give USER 2 the same GROUPS as USER 1.
Does this get me close?
INSERT INTO ide_usergroup_assoc (userid, groupid)
VALUES ('USERID I PROVIDE', (SELECT ide_usergroup_assoc.groupid from ide_usergroup_assoc WHERE ide_usergroup_assoc.userid = 'USERID TO BE CLONED'))
insert into ide_usergroup_assoc (userid, groupid)
select 2, groupid
from ide_usergroup_assoc
where userid = 1
Related
Using Oracle SQL, I am trying to insert into table A based on select from table B, but I am not sure how to achieve this, since the select is returning more than one row.
INSERT INTO A
VALUES
(
SELECT id FROM B WHERE status = 'APPROVED',
'Hardcoded-Value'
);
Table B:
id
status
1
APPROVED
2
DECLINED
3
APPROVED
Based on that insert, I want to achieve following:
Table A:
Column A
Column B
1
Hardcoded-Value
3
Hardcoded-Value
You can use a const in the select list
INSERT INTO A(colA, colB)
SELECT id, 'Hardcoded-Value'
FROM B
WHERE status = 'APPROVED'
We have the following table:
DECLARE #UserGroup TABLE (UserId int, GroupId int)
insert into #UserGroup values (1,1) -- User 1 belongs to Group 1
insert into #UserGroup values (1,2) -- User 1 belongs to Group 2
insert into #UserGroup values (2,1) -- User 2 belongs to Group 1
insert into #UserGroup values (2,2) -- User 2 belongs to Group 2
insert into #UserGroup values (3,1) -- User 3 belongs to Group 1
insert into #UserGroup values (4,4) -- User 4 belongs to Group 4
We can see that User 1 and User 2 belong to the same two groups Group 1 and Group 2. User 3 belongs to Group 1 only and User 4 belongs to Group 4. What I'm trying to achieve should look like something below:
Key(group combination) | User Id
(Group1,Group2) | Either User 1 or User 2
(Group1) | User 3 only
(Group4) | User 4 only
Basically, for each combination of groups we have users for, I want to select ANY user from that group. Just to give more context, I need to select one user from a combination of groups to do some testing related to security since security is set for groups and not users.
I believe the following query does the job:
;WITH groupKeysAndUsers as
(
select
STRING_AGG(ug1.GroupId, ',') WITHIN GROUP (ORDER BY ug1.GroupId) as GroupKey,
ug1.UserId
from #UserGroup ug1
group by ug1.UserId
)
select GroupKey, min(UserId) -- I use min to select any user from the grouping set
from groupKeysAndUsers
group by GroupKey
I wonder if there is a better way(faster and/or easier to consume) to achieve the same result without using STRING_AGG.
I have multiple records in Select statement, I want to insert these records in two different tables based on Primary keys.
I have three tables Roles, MenuMaster and MenuChild as under
Roles:
- RoleID(PK) RoleName
- 1 Sales
- 2 Marketing
- 3 IT
MenuMaster:
- MID(PK) MFormName RoleID(FK)
- 1 Orders 1
- 2 SMM 2
- 3 Help 1
MenuChild:
- CID(PK) FormName, MID(FK)
- 1 NewOrder.aspx 1
- 2 RepeatOrder.aspx 1
- 3 Advertise.aspx 2
I want to create a new merge Role like '4, Sales & Marketing' and copy all records of RoleID=1 and RoleID=2 from MenuMaster to 'Sales & Marketing' RoleID and copy MenuChild record against new Inserted record MenuID
Create the role and use a temp table to populate the other tables:
INSERT INTO dbo.Roles (RoleName)
VALUES ('Sales & Marketing')
SELECT
'Sales & Marketing' as RoleName,
mm.MenuId,
mm.MFormName,
SCOPE_IDENTITY() AS PK
INTO #tmp
FROM dbo.MenuMaster mm
WHERE mm.RoleId in (1,2)
INSERT INTO dbo.MenuMaster (MFormName, RoleId)
SELECT t.MFormName, t.PK from #tmp t
UPDATE #tmp SET PK = SCOPE_IDENTITY()
INSERT INTO dbo.MenuChild(FormName, MenuId)
SELECT t.MFormName, t.PK from #tmp t
This is a little ugly because I'm not sure I completely understood your requirement. You can also use a CTE, but after the first INSERT into MenuMaster, you will need to create another CTE
i have a table with 2 fields. How can i insert those 2 fields from result of 2 sql result.
insert into access (user,page)
(select id as user from users where id =5,
select pagename as page from pages where id =10)
There is no relation between 2 tables . i dont think i can join .
insert into access ("user", page) values
( (select id as user from users where id =5),
(select pagename as page from pages where id =10)
)
insert into access (user,page)
select users.id as user,
pages.pagename as page
from users,pages
where users.id = 5
and pages.id = 10
OK I have a table that has two columns, userID and courseID. It is used to assign training courses to a user. It looks like this:
userid courseid
0 1
0 3
0 6
1 1
1 4
1 5
so user 0 is assigned to courses 1,3,6 and user 1 is assigned to 1, 4 5
anyways I need to take every user that is assigned to 6 and create a new row that has that userid and courseid 11, basically assigning every user who is currently assigned to 6 to also be assigned to 11
for some reason (I did not create this database) both rows are marked as primary keys, and some statements I have tried have thrown an error because of this, what the heck is the deal?
oh maybe it is because there are a few users that are already assigned to 11 so it is choking on those maybe?
please help
Insert Into TableName (userID, courseID)
Select userID, 11 From TableName Where courseID=6;
Also, I'm a bit confused by your comment that both are primary keys. Both rows can be part of the primary key or both can be Unique keys but they cannot both be a primary key. As far as errors go, it is probably because the query tried to insert rows that were duplicates of already existing rows. To eliminate this possibility you could do this:
Insert Into TableName (userID, courseID)
Select userID, 11 From TableName Where courseID=6
AND (userID not in (Select userID From TableName Where courseID=11))
Depending on your database this could work too:
INSERT OR IGNORE INTO TableName (userID, courseID)
SELECT userID, 11 FROM TableName WHERE courseID=6;
Anyway, there you go.
insert into TableName (userId, courseId)
select userId, 11
from TableName
where courseId = 6
and not exists (
select 1
from TableName nested
where nested.userId = TableName.UserId
and nested.courseId = 11
)
Selects all users that are assigned to courseId 6 but are not yet assigned to courseId 11 and inserts a new record into the table for them for courseId 11.
This should help:
INSERT
INTO [table]
(
userid,
courseid
)
SELECT userid,
11
FROM [table]
WHERE courseid = 6
AND userid NOT IN
(SELECT userid
FROM [table]
WHERE courseid = 11
);
This will select all users in course 6 not in course 11 and add them with course 11 to the table.