CREATE TABLE [dbo].[Tabla1](
[Id] [varchar](10) NOT NULL,
[Descripcion] [varchar](50) NOT NULL,
[Activo] [bit] NOT NULL,
[ParentId] AS (CONVERT([int],left([id],charindex('.',[id]+'.')-(1)),0))
)
##
CREATE PROCEDURE [dbo].[GENERAR_ID_COMPUESTO] @idNuevo AS INT
AS
BEGIN
DECLARE @id AS VARCHAR(20);
SET @id= CONVERT(VARCHAR(10), @idNuevo) + '.' + CONVERT(VARCHAR(20),
(
SELECT ISNULL(MAX(CONVERT(INTEGER, (CASE
WHEN(LEN(Id) - LEN(REPLACE(Id, '.', ''))) = 1
THEN SUBSTRING(Id, PATINDEX('%.%', Id) + 1, LEN(Id))
WHEN(LEN(Id) - LEN(REPLACE(Id, '.', ''))) = 2
THEN SUBSTRING(Id, PATINDEX('%.%', Id) + 1, PATINDEX('%.%', (SUBSTRING(Id, PATINDEX('%.%', Id) + 1, LEN(Id)))) - 1)
ELSE 0
END))), 1) + 1
FROM Tabla1
WHERE(CASE
WHEN(LEN(Id) - LEN(REPLACE(Id, '.', ''))) = 0
THEN Id
WHEN(LEN(Id) - LEN(REPLACE(Id, '.', ''))) > 0
THEN SUBSTRING(Id, 0, PATINDEX('%.%', Id))
ELSE 0
END) = @idNuevo
AND (CASE
WHEN(LEN(Id) - LEN(REPLACE(Id, '.', ''))) = 2
THEN REVERSE(SUBSTRING(REVERSE(Id), 0, PATINDEX('%.%', REVERSE(Id))))
ELSE 0
END) = 0
));
SELECT @id;
END;
##
--example
DECLARE @id as NUMERIC(18,0)
EXECUTE Ceres.dbo.SYNC_GENERAR_NAVEID 23, @id OUTPUT;
-- result: 23.3
--insert into Tabla1 (id) select @id
DECLARE @id as NUMERIC(18,0)
EXECUTE Ceres.dbo.SYNC_GENERAR_NAVEID 23, @id OUTPUT;
-- result: 23.4
--insert into Tabla1 (id) select @id
No hay comentarios:
Publicar un comentario