
/*------ ATD 361575 ------*/
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE name = 'REFERENCIAITEM' and syscolumns.id IN (SELECT ID FROM SYSOBJECTS WHERE xtype='U' AND name='ITEMCLICOMPLEMENTO'))
ALTER TABLE ITEMCLICOMPLEMENTO ADD REFERENCIAITEM VARCHAR(30)
GO

IF EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE name = 'REFERENCIAITEM' and syscolumns.id IN (SELECT ID FROM SYSOBJECTS WHERE xtype='U' AND name='ITEMCLICOMPLEMENTO'))
UPDATE
ITEMCLICOMPLEMENTO SET ITEMCLICOMPLEMENTO.REFERENCIAITEM = P.REFERENCIA
FROM
PEDICLICAD P
JOIN ITEMCLICOMPLEMENTO I ON I.NUMPED = P.NUMPED
WHERE
P.REFERENCIA IS NOT NULL AND P.REFERENCIA != ''
GO