
--REF_ASISTENCIAS -> Ver si poner AÑO/IDSiniestroTesis (?)
-- SELECT CAST('<t>' + REPLACE('Name1~Name2~Name3' , '~','</t><t>') + '</t>' AS XML).value('/t[2]','varchar(50)')
-- Actualizar campo de la columna de Siniestro -- --- MIRAR 18_script_cliente_tesis.sql
INSERT INTO asurnet.dbo.SINIESTROS
(SIN_Id, SIN_Cuerpo,SIN_Descripcion,SIN_IdSiniestro,SIN_IdFechaSiniestro,SIN_DanosMateriales,
 SIN_Atestados,SIN_Victima,SIN_Consorcio,SIN_ReclamacionJA)
SELECT A.SIN_Id,A.SIN_Cuerpo,A.SIN_Descripcion,CAST('<t>' + REPLACE(C.IDTSS, '/','</t><t>') + '</t>' AS XML).value('/t[2]','varchar(50)') as idSiniestro,CAST('<t>' + REPLACE(C.IDTSS, '/','</t><t>') + '</t>' AS XML).value('/t[1]','varchar(50)') as fechaIdSiniestro,A.SIN_DanosMateriales,A.SIN_Atestado,A.SIN_Victimas,A.SIN_Consorcio,A.SIN_ReclamacionJA
FROM vsegbas.dbo.SINIESTROS A
         LEFT JOIN asurnet.dbo.SINIESTROS B ON A.SIN_Id = B.SIN_Id
         INNER JOIN (
    SELECT * FROM tssseg0001.dbo.IDTraspasos
    WHERE TableID = 'Siniestros'
) C ON A.SIN_Id = C.IdTras
WHERE B.SIN_Id IS NULL;


UPDATE A
SET
    A.SIN_Id = B.SIN_Id,
    A.SIN_Cuerpo = B.SIN_Cuerpo,
    A.SIN_IdSiniestro = B.idSiniestro,
    A.SIN_Descripcion = B.SIN_Descripcion,
    A.SIN_IdFechaSiniestro = B.fechaIdSiniestro,
    A.SIN_DanosMateriales = B.SIN_DanosMateriales,
    A.SIN_Atestados = B.SIN_Atestado,
    A.SIN_Victima = B.SIN_Victimas,
    A.SIN_Consorcio = B.SIN_Consorcio,
    A.SIN_ReclamacionJA = B.SIN_ReclamacionJA
    FROM asurnet.dbo.SINIESTROS A
         INNER JOIN(
    SELECT A.SIN_Id,A.SIN_Cuerpo,A.SIN_Descripcion,CAST('<t>' + REPLACE(C.IDTSS, '/','</t><t>') + '</t>' AS XML).value('/t[1]','varchar(50)') as fechaIdSiniestro,CAST('<t>' + REPLACE(C.IDTSS, '/','</t><t>') + '</t>' AS XML).value('/t[2]','varchar(50)') as idSiniestro,A.SIN_DanosMateriales,A.SIN_Atestado,A.SIN_Victimas,A.SIN_Consorcio,A.SIN_ReclamacionJA
    FROM vsegbas.dbo.SINIESTROS A
             LEFT JOIN asurnet.dbo.SINIESTROS B ON A.SIN_Id = B.SIN_Id
             INNER JOIN (
        SELECT * FROM tssseg0001.dbo.IDTraspasos
        WHERE TableID = 'Siniestros'
    ) C ON A.SIN_Id = C.IdTras
) B ON A.SIN_Id = B.SIN_Id