-- Seleccionar base de datos
USE data_nexapps;
GO


DROP PROCEDURE IF EXISTS TJT.obtain_next_aichat_sesion_value;
GO
CREATE PROCEDURE TJT.obtain_next_aichat_sesion_value
    @id_usuario INT
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @nextSesion INT;
    DECLARE @save INT = 0;
    DECLARE @result NVARCHAR(200);

    BEGIN TRY
        SELECT @nextSesion = ISNULL(MAX(sesion), 0) + 1
        FROM TJT.aichat_mensajes
        WHERE id_usuario = @id_usuario;

        SET @save = 1;
        SET @result = 'Sesión obtenida correctamente';
    END TRY
    BEGIN CATCH
        SET @nextSesion = 0;
        SET @save = 0;
        SET @result = ERROR_MESSAGE();
    END CATCH;

    SELECT @save AS 'save', @result AS result, @nextSesion AS next_sesion;
END;
GO
