Insert correlative to an ID field in SQL

1

I'm currently making an export with this Script:

/* FromTable  1:Otrntransaction, 2:OtrnTrnTaxAmount, 3:Marckup  */ SELECT '1' AS FromTable, T1.ID_Trn AS IdDiarioTrans, T1.PostingDate AS Fecha, T2.AccountNo AS Folio, 
                         CASE SubFolio WHEN 0 THEN 'A' WHEN 1 THEN 'B' WHEN 2 THEN 'C' WHEN 3 THEN 'D' END AS SubFolio, T5.RoomNo AS Habitacion, T1.Reference AS Referencia, ID_TrnCode AS IdTipoTransaccion, 
                         T3.ShortName AS TipoTransaccion, T3.Name AS ShortName, T1.AmountPur AS Monto, T1.Comment AS Comentario
FROM            otrnTransaction AS T1 LEFT OUTER JOIN
                         oaccAccount AS T2 ON T2.ID_Account = T1.ID_Account LEFT OUTER JOIN
                         prCode AS T3 ON T3.ID_Code = T1.ID_TrnCode LEFT OUTER JOIN
                         rmRoom AS T5 ON T5.ID_Room = T1.ID_Room
WHERE T2.AccountNo = 34339
UNION ALL
SELECT        '2' AS FromTable, T1.ID_Trn AS IdDiarioTrans, T1.PostingDate AS Fecha, T2.AccountNo AS Folio, CASE SubFolio WHEN 0 THEN 'A' WHEN 1 THEN 'B' WHEN 2 THEN 'C' WHEN 3 THEN 'D' END AS SubFolio, 
                         T5.RoomNo AS Habitacion, T1.Reference AS Referencia, T0.ID_TaxCode AS IdTipoTransaccion, T3.ShortName AS TipoTransaccion, T3.Name AS ShortName, T0.Amount AS Monto, T1.Comment AS Comentario
FROM            otrnTrnTaxAmount AS T0 LEFT OUTER JOIN
                         otrnTransaction AS T1 ON T0.ID_Trn = T1.ID_Trn LEFT OUTER JOIN
                         acTrnCodeGLDepPL AS T4 ON T4.ID_TrnCode = T0.ID_TaxCode AND T4.ID_DepartmentalPL = T1.ID_DepartmentalPL LEFT OUTER JOIN
                         oaccAccount AS T2 ON T2.ID_Account = T1.ID_Account LEFT OUTER JOIN
                         prCode AS T3 ON T3.ID_Code = T0.ID_TaxCode LEFT OUTER JOIN
                         rmRoom AS T5 ON T5.ID_Room = T1.ID_Room
WHERE T2.AccountNo = 34339
UNION ALL
SELECT        '3' AS FromTable, T1.ID_Trn AS IdDiarioTrans, T1.PostingDate AS Fecha, T2.AccountNo AS Folio, CASE SubFolio WHEN 0 THEN 'A' WHEN 1 THEN 'B' WHEN 2 THEN 'C' WHEN 3 THEN 'D' END AS SubFolio, 
                         T5.RoomNo AS Habitacion, T1.Reference AS Referencia, T1.ID_MarkupCode AS IdTipoTransaccion, T3.ShortName AS TipoTransaccion, T3.Name AS ShortName, T1.AmountMarkup AS Monto, 
                         T1.Comment AS Comentario
FROM            otrnTransaction AS T1 LEFT OUTER JOIN
                         oaccAccount AS T2 ON T2.ID_Account = T1.ID_Account LEFT OUTER JOIN
                         prCode AS T3 ON T3.ID_Code = T1.ID_TrnCode LEFT OUTER JOIN
                         acTrnCodeGLDepPL AS T4 ON T4.ID_TrnCode = T1.ID_MarkupCode LEFT OUTER JOIN
                         rmRoom AS T5 ON T5.ID_Room = T1.ID_Room
WHERE        (T1.ID_MarkupCode IS NOT NULL) AND T2.AccountNo = 34339
ORDER BY SubFolio

Resulting in the following records:

I explain the operation quickly, the query returns the transactions of an account in the system with which I work (hotel system). According to the operation of said system, transactions are stored in a table (OtrnTransaction) and transaction taxes are saved in another table (OtrnTrnTaxAmount). So to rebuild the entire account I do 2 UNIONS to extract it completely. So far so good

The drawback is that the original transactions are stored by means of an ID and the taxes that are housed in another table are stored by the same ID, according to the following image:

When I try to export this information, it gives me an error because in the database where I want to deposit the information, it does not allow duplicates in the IdDiarioTrans field, so I need to add a correlation 1, 2, 3 for each transaction so that it does not the ID is repeated.

I do not know if I gave myself to understand well, I hope you can help me.

    
asked by Gio Gómez 07.06.2018 в 19:40
source

1 answer

0

I think it is not an answer, I would always like to know how to do it if it is useful for me in the future but what I did in the end was to create a CUSTOM table with an auto-increment, insert the view data there and then from there to export them to the other database, exporting in the ID field, instead of the ID of the transaction, the autoincrement that I configured.

    
answered by 07.06.2018 в 20:33