Hi. while Inserting values into table Rights i am getting the Current Auto increment if i have @Roles values="1,3,5,7,8," that will insert in as 1 3 5 7 8 individually in all the newly created rows in rights table
This is my Insert Sp
========================
ALTER PROC [C244570_hrms12].[h_AddRole]
(
@RoleName NVARCHAR(MAX),
@Roles NVARCHAR(MAX)
)
AS
DECLARE @RoleID INT,@StartingPos INT,@RecPos INT,@RightId INT
DECLARE @RecSet VARCHAR(500)
BEGIN
INSERT INTO ROLES(RoleName) VALUES(@RoleName)
SET @StartingPos=1
SET @RoleId=(SELECT IDENT_CURRENT('ROLES'))
WHILE @StartingPos<=LEN(@roles)
BEGIN
SELECT @RecPos=CHARINDEX(',',@roles,@StartingPos)
SELECT @RecSet=SUBSTRING(@roles,@StartingPos,@Recpos-@StartingPos)
SET @RightId=@RecSet
INSERT INTO Rights(RightId,RoleId)VALUES(@RightId,@roleId)
SELECT @StartingPos=@RecPos+1
END
END
While Updating the values how to handle the Id values of rights table i have tried the SP but not working please find any good soln...
Update SP
=============
ALTER PROC [C244570_hrms12].[h_UpdateRole]
(
@Id INT,
@RoleName NVARCHAR(MAX),
@Roles NVARCHAR(MAX)
)
AS
DECLARE @RoleId INT,@StartingPos INT,@RecPos INT,@RightId INT
DECLARE @RecSet VARCHAR(500),@Rid NVARCHAR(50)
BEGIN
UPDATE Roles SET RoleName=@RoleName WHERE Id=@Id
SET @StartingPos=1
SET @Rid=(SELECT ID FROM RIGHTS WHERE RoleId=@RoleId)
WHILE @StartingPos<=LEN(@roles)
BEGIN
SELECT @RecPos=CHARINDEX(',',@roles,@StartingPos)
SELECT @RecSet=SUBSTRING(@roles,@StartingPos,@RecPos-@StartingPos)
SET @RightId=@RecSet
UPDATE Rights SET RightId=@RightId,RoleId=@RoleId WHERE Id=@Rid
END
END