IF NOT EXISTS(SELECT
				CD_FND_VERBA_TIPO
			  FROM
				CG_FND_VERBA_TIPO
			  WHERE
				CD_FND_VERBA_TIPO=0)
begin
	INSERT INTO CG_FND_VERBA_TIPO(CD_FND_VERBA_TIPO,
									DS_FND_VERBA_TIPO,
									CD_USU,
									DT_CAD,
									TIPO_ACAO_VERBA)
	SELECT 
		0 CD_FND_VERBA_TIPO,
		'NAO DEFINIDO' DS_FND_VERBA_TIPO,
		1,
		CONVERT(CHAR(10),GETDATE(),112) DT_CAD,
		0
end
go
IF NOT EXISTS (SELECT * FROM SYS.COLUMNS WHERE NAME = 'CD_FND_VERBA_TIPO' 
				AND OBJECT_ID = OBJECT_ID('CG_FND_VERBA'))
begin
ALTER TABLE dbo.CG_FND_VERBA ADD
	CD_FND_VERBA_TIPO int NULL

	ALTER TABLE dbo.CG_FND_VERBA ADD CONSTRAINT
		DF_CG_FND_VERBA_CD_FND_VERBA_TIPO DEFAULT 0 FOR CD_FND_VERBA_TIPO

	ALTER TABLE dbo.CG_FND_VERBA ADD CONSTRAINT
		FK_CG_FND_VERBA_CG_FND_VERBA_TIPO FOREIGN KEY
		(
		CD_FND_VERBA_TIPO
		) REFERENCES dbo.CG_FND_VERBA_TIPO
		(
		CD_FND_VERBA_TIPO
		) ON UPDATE  NO ACTION 
		 ON DELETE  NO ACTION 
end

go
UPDATE 
	CG_FND_VERBA 
SET
	CD_FND_VERBA_TIPO=0
WHERE
	CD_FND_VERBA_TIPO IS NULL 
