Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sujithukvl@gmail.com
Starting Member
22 Posts |
Posted - 2011-03-22 : 03:15:29
|
I have three tables:• Role• Permission• RolePermission (link table with just role id and permissionid)I have a stored procedure that updates role tableIf there are associated permissions to the role,I also have to updateRolePermission table too.If I am passing permissionIds associated with the role to the update stored procedure, as a comma separated varchar (4000) I can use the function dbo.Split to split them good.A bad query I can write isUPDATE [ROLE] SET ProductId=@ProductId, [Name]=@NameWHERE RoleId=@RoleIdDELETE FROM RolePermission WHERE RoleId=@RoleIdINSERT INTO RolePermission SELECT @RoleId, P.ItemsFROM dbo.Split(@PermisionIds, ',') PBut I would like to add only those ids new in @PermisionIds and not in the tableAnd delete all PermisionIds from table which are not in @PermisionIds.How can i do this without using IN and NOT IN |
|
matty
Posting Yak Master
161 Posts |
Posted - 2011-03-22 : 04:42:24
|
[code]--get new Id's in @PermisionIdsSELECT p.ItemsFROM dbo.Split(@PermisionIds,',') pLEFT JOIN RolePermissions rp ON p.Items = rp.PermissionId AND rp.RoleId = @RoleIdWHERE rp.PermissionId IS NULL--Get Id's from table which are not in @PermisionIdsSELECT rp.PermissionIdFROM RolePermissions rpLEFT JOIN dbo.Split(@PermisionIds,',') p ON p.Id = rp.PermissionId WHERE rp.RoleId = @RoleIdAND p.Items IS NULL[/code] |
 |
|
sujithukvl@gmail.com
Starting Member
22 Posts |
Posted - 2011-03-23 : 10:49:54
|
still i need to use IN and NOT in ,right? |
 |
|
|
|
|