create procedure sistema.sp_guardarRolesUsuario
@numUsuario int,
@txtRoles varchar(2000),
@dbResponse varchar(300) output
as
begin
	set nocount on;
	declare @dbRol int,@dbLoop int,@dbPosicionActual int;
	
	delete from sistema.config_rolesUsuario where idUsuario = @numUsuario;
	if @txtRoles is not null
		begin
			set @dbLoop = 0;
			set @dbPosicionActual = CHARINDEX('@@@',@txtRoles);

			while(@dbLoop = 0)
				begin
					if @dbPosicionActual > 0
						begin
							set @dbRol = cast(SUBSTRING(@txtRoles,1,@dbPosicionActual -1) as int);
							set @dbLoop = 0;
						end
					else
						begin
							set @dbRol = CAST(@txtRoles as int);
							set @dbLoop = 1;
						end

					insert into sistema.config_rolesUsuario(idUsuario,idRol) values(@numUsuario,@dbRol);

					if @dbLoop = 0
						begin
							set @txtRoles = SUBSTRING(@txtRoles,@dbPosicionActual+3,LEN(@txtRoles));
							set @dbPosicionActual = CHARINDEX('@@@',@txtRoles);
						end
				end
		end

	set @dbResponse = 'success';
end