Here's the deal.Objective:I have a database with a table with user info and other table with possible user roles for the application and a table linking the user to the role.Rules:Any user can be in multiple roles; ie: User1 can be of role 'Advertiser' as well as the role of 'Seller'. Only the admin (myself) can make these changes. I'm trying to write a stored procedure (sproc) to compare which roles the user has and the new roles to have. So I figure to cycle thru each possible role (like 7). If the user is part of the role in the current cycle and is not part of the new role sent, then delete that role. And ifthe user is not part of the current role in cycle and is to be part of it the add that urole to the user. (I think that last line was somewhat confusing. Ok well, let's see some code.Here are the tables:CREATE TABLE [dbo].[User] ( [ID] [int] IDENTITY (1, 1) NOT NULL , [UserID] uniqueidentifier ROWGUIDCOL NOT NULL , [UserName] [varchar] (16) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [UserPassword] [varchar] (16) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [UserEmail] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [UserEmailTypeID] [int] NOT NULL , [UserEmailVerify] [bit] NOT NULL , [UserLastLogin] [datetime] NOT NULL , [UserRegistered] [datetime] NOT NULL , [UserLastModified] [datetime] NOT NULL , [UserLastModifiedBy] [int] NOT NULL ) ON [PRIMARY]GOCREATE TABLE [dbo].[UserType] ( [UserTypeID] [int] IDENTITY (1, 1) NOT NULL , [UserType] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [UserLoginPage] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [UserTypeLastModified] [datetime] NOT NULL , [UserTypeLastModifiedBy] [int] NOT NULL ) ON [PRIMARY]GOCREATE TABLE [dbo].[UserTypes] ( [UserTypesID] [int] IDENTITY (1, 1) NOT NULL , [UserID] [int] NOT NULL , [UserTypeID] [int] NOT NULL ) ON [PRIMARY]GOALTER TABLE [dbo].[User] WITH NOCHECK ADD CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED ( [ID] ) ON [PRIMARY] GOALTER TABLE [dbo].[UserType] WITH NOCHECK ADD CONSTRAINT [PK_UserType] PRIMARY KEY CLUSTERED ( [UserTypeID] ) ON [PRIMARY] GOALTER TABLE [dbo].[UserTypes] WITH NOCHECK ADD CONSTRAINT [PK_UserTypes] PRIMARY KEY CLUSTERED ( [UserTypesID] ) ON [PRIMARY] GOALTER TABLE [dbo].[User] ADD CONSTRAINT [FK_User_EmailType] FOREIGN KEY ( [UserEmailTypeID] ) REFERENCES [dbo].[EmailType] ( [EmailTypeID] )GOALTER TABLE [dbo].[UserTypes] ADD CONSTRAINT [FK_UserTypes_User] FOREIGN KEY ( [UserID] ) REFERENCES [dbo].[User] ( [ID] ), CONSTRAINT [FK_UserTypes_UserType] FOREIGN KEY ( [UserTypeID] ) REFERENCES [dbo].[UserType] ( [UserTypeID] )GO
Now for the sprocs:CREATE procedure proc_AddUserTypes @UserID int, @UserTypeID int, @UserTypesID int outputas declare @ErrorCode int select @ErrorCode = @@Error declare @TransactionCountOnEntry int if @ErrorCode = 0begin select @TransactionCountOnEntry = @@TranCount begin transaction insert UserTypes ( UserID, UserTypeID ) values ( @UserID, @UserTypeID ) select @UserTypesID = @@Identity, @ErrorCode = @@Errorendif @@TranCount > @TransactionCountOnEntrybegin if @ErrorCode = 0 commit transaction else rollback transactionendreturn @ErrorCodeGOCREATE procedure proc_DeleteUserTypes @UserTypesID intas declare @ErrorCode int select @ErrorCode = @@Error declare @TransactionCountOnEntry int if @ErrorCode = 0begin select @TransactionCountOnEntry = @@TranCount begin transaction delete from UserTypes where UserTypes.UserTypesID = @UserTypesID select @ErrorCode = @@Errorendif @@TranCount > @TransactionCountOnEntrybegin if @ErrorCode = 0 commit transaction else rollback transactionendreturn @ErrorCodeGOCREATE procedure proc_UpdateUser @ID int, @Password varchar(16), @Email varchar(255), @EmailTypeID int, @LastModifiedBy int, @UserTypeID varchar(600)as declare @ErrorCode int select @ErrorCode = @@Error declare @TransactionCountOnEntry int if @ErrorCode = 0begin select @TransactionCountOnEntry = @@TranCount begin transaction update [User] set [User].UserPassword = @Password, [User].UserEmail = @Email, [User].UserEmailTypeID = @EmailTypeID, [User].UserLastModified = getdate(), [User].UserLastModifiedBy = @LastModifiedBy where [User].ID = @ID select @ErrorCode = @@Errorend if @ErrorCode = 0begin exec @ErrorCode = proc_ProcessUserTypes @ID, @UserTypeIDendif @@TranCount > @TransactionCountOnEntrybegin if @ErrorCode = 0 commit transaction else rollback transactionendreturn @ErrorCodeGO
Ok, here is some more info. The '@UserTypeID' in 'proc_UpdateUser' get's sent the list if roles in CSV format whic I then parse in the 'proc_ProcessUserTypes' sproc. Here it is:CREATE procedure proc_ProcessUserTypes @ID int, @UserTypeID varchar(600)as declare @ErrorCode int select @ErrorCode = @@Error declare @TransactionCountOnEntry int declare @CurUserTypeID int declare @Pos int if @ErrorCode = 0begin select @UserTypeID = ltrim(rtrim(@UserTypeID))+ ',' select @Pos = charindex(',', @UserTypeID, 1) if replace(@UserTypeID, ',', '') <> '' begin while @Pos > 0 begin select @CurUserTypeID = ltrim(rtrim(left(@UserTypeID, @Pos - 1))) if @CurUserTypeID <> '' begin --NEED HELP HERE --Add --exec @ErrorCode = proc_AddUserTypes @CurUserTypeID --Delete --exec @ErrorCode = proc_DeleteUserTypes @CurUserTypeID end select @UserTypeID = right(@UserTypeID, len(@UserTypeID) - @Pos) select @Pos = charindex(',', @UserTypeID, 1) end endendif @@TranCount > @TransactionCountOnEntrybegin if @ErrorCode = 0 commit transaction else rollback transactionendreturn @ErrorCodeGOI noted above where i need the help. I'd like to cycle thru the possible values and do two checks, if they are already part of the role or not and if they are to be assigned the role or not. Any questions? Not clear? Please ask. Thanks.PS: Sorry if this is sooo long.- RoLY roLLs