begin tran
declare @billFullNR varchar(max) = 'P00086/16'
declare @invoiceFullNR varchar(max) = 'FV00055/05/16'
declare @username varchar(max) = 'xxxxx.xxxxxxx'
declare @billID uniqueidentifier = (select invoiceID from document.invoice where invoiceFullNR = @billFullNR)
declare @invoiceID uniqueidentifier = (select invoiceID from document.invoice where invoiceFullNR = @invoiceFullNR)
declare @billLineID uniqueidentifier = (select invoiceLineID from document.invoiceLine where invoiceID = @billID and invoiceOrdinalNumber = 1 and [status] >= 0)
declare @invoiceLineID uniqueidentifier = (select invoiceLineID from document.invoiceLine where invoiceID = @invoiceID and invoiceOrdinalNumber = 1 and [status] >= 0)
--usuwanie starych powiazan , bez usuniecia bedzie blad
select * from [document].[documentRelation] where invoiceid =@invoiceID
select * from [document].[documentRelation] where invoiceid =@billID
delete from [document].[documentRelation] where invoiceid =@invoiceID and documentrelationtype =4
delete from [document].[documentRelation] where invoiceid =@billID and documentrelationtype =5
insert into [document].[documentRelation]
(
[documentRelationID],[invoiceID],[invoiceID2],[documentRelationType], addDate, addUsername
)
select newID(), @invoiceID, @billID, 4, GETUTCDATE(), @username union all
select newID(), @billID, @invoiceID, 5, GETUTCDATE(), @username
update document.invoiceLine set sourceInvoiceLineID = @billLineID, invoiceRelationType = 2 where invoiceLineID = @invoiceLineID
update document.invoice set fulltextcat=0 where invoiceid=@invoiceid
rollback tran