--  creacion de tabla--
if not exists(select * FROM INFORMATION_SCHEMA.TABLES  where Table_Name='seg_perfil_tercero')
EXEC _ini_creartabla 'seg_perfil_tercero', 1
go

-- creacion de columnas----

if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS  where Table_Name='seg_perfil_tercero' and column_name = 'id_perfil' )
begin
alter table [seg_perfil_tercero] add [id_perfil] int not NULL DEFAULT 0
end

if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS  where Table_Name='seg_perfil_tercero' and column_name = 'id_tercero' )
begin
alter table [seg_perfil_tercero] add [id_tercero] int not NULL DEFAULT 0
end


----  creacion de relaciones----

IF NOT  EXISTS (SELECT * FROM sys.foreign_keys WHERE parent_object_id = OBJECT_ID(N'[dbo].[seg_perfil_tercero]') AND object_id = OBJECT_ID(N'[dbo].[fk_seg_perfil_tercero_id_perfil]')  )
ALTER TABLE [seg_perfil_tercero] ADD CONSTRAINT
	[fk_seg_perfil_tercero_id_perfil] FOREIGN KEY
	([id_perfil]) REFERENCES [seg_perfil]	(id)
 
ALTER TABLE [dbo].[seg_perfil_tercero] CHECK CONSTRAINT [fk_seg_perfil_tercero_id_perfil]
GO

IF NOT  EXISTS (SELECT * FROM sys.foreign_keys WHERE parent_object_id = OBJECT_ID(N'[dbo].[seg_perfil_tercero]') AND object_id = OBJECT_ID(N'[dbo].[fk_seg_perfil_tercero_id_tercero]')  )
ALTER TABLE [seg_perfil_tercero] ADD CONSTRAINT
	[fk_seg_perfil_tercero_id_tercero] FOREIGN KEY
	([id_tercero]) REFERENCES [ter_tercero]	(id)
 
ALTER TABLE [dbo].[seg_perfil_tercero] CHECK CONSTRAINT [fk_seg_perfil_tercero_id_tercero]
GO

--- insertar en _columnas----------


IF NOT EXISTS(SELECT *FROM [_columnas] WHERE[tabla]='seg_perfil_tercero'
      AND [columna]='id_perfil' AND[mostrarEnGrilla]=1 AND[ordenGrilla]=5AND[requerido]=1 AND [tipo]='int' AND[defecto]=0 AND [tamano] =100)
BEGIN
INSERT INTO [_columnas]
([tabla] ,[columna] ,[mostrarEnGrilla],[ordenGrilla],[requerido] ,[tipo],[defecto],[tamano])
 VALUES
 ('seg_perfil_tercero' ,'id_perfil',1 ,5 ,1 ,'int' ,0 ,100)
END

IF NOT EXISTS(SELECT *FROM [_columnas] WHERE[tabla]='seg_perfil_tercero'
      AND [columna]='id_tercero' AND[mostrarEnGrilla]=1 AND[ordenGrilla]=5AND[requerido]=1 AND [tipo]='int' AND[defecto]=0 AND [tamano] =100)
BEGIN
INSERT INTO [_columnas]
([tabla] ,[columna] ,[mostrarEnGrilla],[ordenGrilla],[requerido] ,[tipo],[defecto],[tamano])
 VALUES
 ('seg_perfil_tercero' ,'id_tercero',1 ,5 ,1 ,'int' ,0 ,100)
END

