
--Crear Tablas

if not exists(select * FROM INFORMATION_SCHEMA.TABLES  where Table_Name='seg_registroPeticion')
BEGIN
EXEC _ini_creartabla 'seg_registroPeticion', 1
END

-----------------------------------CREACION COLIUMNAS---------------

if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS  where Table_Name='seg_registroPeticion' and column_name = 'urlServicio' )
begin
alter table [seg_registroPeticion] add [urlServicio] varchar (1000)not NULL DEFAULT 0
end

if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS  where Table_Name='seg_registroPeticion' and column_name = 'modulo' )
begin
alter table [seg_registroPeticion] add [modulo] varchar (100)not NULL DEFAULT 0
end

if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS  where Table_Name='seg_registroPeticion' and column_name = 'aplicacion' )
begin
alter table [seg_registroPeticion] add [aplicacion] varchar (100)not NULL DEFAULT 0
end

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

if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS  where Table_Name='seg_registroPeticion' and column_name = 'mensajeRespuesta' )
begin
alter table [seg_registroPeticion] add [mensajeRespuesta] varchar (1000) not NULL DEFAULT 0
end

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


----------------------------------------INSERCION COLUMNAS------------------------------------------

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

IF NOT EXISTS(SELECT *FROM [_columnas] WHERE[tabla]='seg_registroPeticion'
      AND [columna]='modulo' 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
 ('seg_registroPeticion' ,'modulo',1 ,5 ,1 ,'varchar' ,0 ,100)
END

IF NOT EXISTS(SELECT *FROM [_columnas] WHERE[tabla]='seg_registroPeticion'
      AND [columna]='aplicacion' 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
 ('seg_registroPeticion' ,'aplicacion',1 ,5 ,1 ,'varchar' ,0 ,100)
END

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

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

IF NOT EXISTS(SELECT *FROM [_columnas] WHERE[tabla]='seg_registroPeticion'
      AND [columna]='id_usuario' 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_registroPeticion' ,'id_usuario',1 ,5 ,1 ,'int' ,0 ,100)
END

-----------------------------------------------CREACION DE RELACIONES-----------------------------------------------------------------


IF NOT  EXISTS (SELECT * FROM sys.foreign_keys WHERE parent_object_id = OBJECT_ID(N'[dbo].[seg_registroPeticion]') AND object_id = OBJECT_ID(N'[dbo].[fk_registroPeticion_usuario_id_usuario]')  )
begin
ALTER TABLE [seg_registroPeticion] ADD CONSTRAINT
	[fk_registroPeticion_usuario_id_usuario] FOREIGN KEY
	--nombre columna llave foranea--------tabla relacionada---------llave primaria------------
	([id_usuario]) REFERENCES [seg_usuario]	(id)
 
ALTER TABLE [dbo].[seg_registroPeticion] CHECK CONSTRAINT [fk_registroPeticion_usuario_id_usuario]
end
