

--Crear Tablas

if not exists(select * FROM INFORMATION_SCHEMA.TABLES  where Table_Name='doc_documento.proyecto_calidad')
EXEC _ini_creartabla 'doc_documento.proyecto_calidad', 1
go


if not exists(select * FROM INFORMATION_SCHEMA.TABLES  where Table_Name='pro_calidad')
EXEC _ini_creartabla 'pro_calidad', 1
go


if not exists(select * FROM INFORMATION_SCHEMA.TABLES  where Table_Name='pro_factorexito')
EXEC _ini_creartabla 'pro_factorexito', 1
go


if not exists(select * FROM INFORMATION_SCHEMA.TABLES  where Table_Name='pro_hito')
EXEC _ini_creartabla 'pro_hito', 1
go




--Creacion columnas

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

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

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

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

 
 --Relaciones
IF NOT  EXISTS (SELECT * FROM sys.foreign_keys WHERE parent_object_id = OBJECT_ID(N'[dbo].[pro_hito]') AND object_id = OBJECT_ID(N'[dbo].[FK_pro_hito_id_documento.proyecto]')  )
ALTER TABLE [pro_hito] ADD CONSTRAINT
	[FK_pro_hito_id_documento.proyecto] FOREIGN KEY
	([id_documento.proyecto]) REFERENCES [doc_documento.proyecto](id) 
	
ALTER TABLE [dbo].[pro_hito] CHECK CONSTRAINT [FK_pro_hito_id_documento.proyecto]
GO

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


if not exists(select * FROM INFORMATION_SCHEMA.TABLES  where Table_Name='pro_hito_aux')
create table [pro_hito_aux] (
uid varchar (500)
,[hito] varchar (300)
,[hito_codigo] varchar (100)

)


if not exists(select * FROM INFORMATION_SCHEMA.TABLES  where Table_Name='pro_calidad_aux')
create table [pro_calidad_aux] (
uid varchar (500)
,[calidad] varchar (300)
,[calidad_codigo] varchar (100)
)

if not exists(select * FROM INFORMATION_SCHEMA.TABLES  where Table_Name='pro_factorexito_aux')
create table [pro_factorexito_aux] (
uid varchar (500)
,[factorexito] varchar (300)
,[factorexito_codigo] varchar (100)

)

if not exists(select * FROM INFORMATION_SCHEMA.TABLES  where Table_Name='doc_documento.proyecto_calidad_aux')
create table [doc_documento.proyecto_calidad_aux] (
uid varchar (500)
,[documento.proyecto_calidad_calidad_calidad] varchar (300)
,[documento.proyecto_calidad_calidad_calidad_codigo] varchar (100)

)