
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_ter_tercero_sector]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_ter_tercero_sector]
GO
CREATE PROCEDURE [reg_ter_tercero_sector]
	@p_id INT
AS
BEGIN
	SELECT 	[id] AS [id],
		[uid] AS [uid],
		[eid] AS [eid],
		[id_usuario_modifico] AS [id_usuario_modifico],
		[id_usuario_creo] AS [id_usuario_creo],
		[fechaModificacion] AS [fechaModificacion],
		[fechaCreacion] AS [fechaCreacion],
		[active] AS [active],
		[tercero_sector] AS [tercero_sector],
		[id_tercero] AS [id_tercero],
		[id_sector] AS [id_sector]
	FROM [ter_tercero_sector]
	WHERE	(id = @p_id)

END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[view_ter_tercero_sector]')) DROP VIEW [dbo].[view_ter_tercero_sector]
GO
CREATE  VIEW [view_ter_tercero_sector] AS
SELECT 	[ter_tercero_sector].[id] AS [id],
		[ter_tercero_sector].[uid] AS [uid],
		[ter_tercero_sector].[eid] AS [eid],
		[ter_tercero_sector].[id_usuario_modifico] AS [id_usuario_modifico],
		[ter_tercero_sector].[id_usuario_creo] AS [id_usuario_creo],
		[ter_tercero_sector].[fechaModificacion] AS [fechaModificacion],
		[ter_tercero_sector].[fechaCreacion] AS [fechaCreacion],
		[ter_tercero_sector].[active] AS [active],
		[ter_tercero_sector].[tercero_sector] AS [tercero_sector],
		[ter_tercero_sector].[id_tercero] AS [id_tercero],
		[ter_tercero_sector].[id_sector] AS [id_sector],
[ter_sector].[sector] as [ter_sector_sector],
[ter_sector].[sector_codigo] as [ter_sector_sector_codigo],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [ter_tercero_sector]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[ter_tercero_sector].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[ter_tercero_sector].[id_usuario_creo]
	INNER JOIN [ter_tercero] ON [ter_tercero].id=[ter_tercero_sector].[id_tercero]
	INNER JOIN [ter_sector] ON [ter_sector].id=[ter_tercero_sector].[id_sector]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [ter_tercero_sector].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_ter_tercero_sectoresXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercero_sectoresXusuario_modifico]
GO
CREATE PROCEDURE [sel_ter_tercero_sectoresXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_tercero_sector]
	WHERE ([id_usuario_modifico]= @p_id_usuario_modifico)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_ter_tercero_sectoresXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercero_sectoresXusuario_creo]
GO
CREATE PROCEDURE [sel_ter_tercero_sectoresXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_tercero_sector]
	WHERE ([id_usuario_creo]= @p_id_usuario_creo)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_ter_tercero_sectoresXtercero]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercero_sectoresXtercero]
GO
CREATE PROCEDURE [sel_ter_tercero_sectoresXtercero]
	@p_id_tercero INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_tercero_sector]
	WHERE ([id_tercero]= @p_id_tercero)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_ter_tercero_sectoresXsector]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercero_sectoresXsector]
GO
CREATE PROCEDURE [sel_ter_tercero_sectoresXsector]
	@p_id_sector INT
AS
BEGIN
	SELECT * 
	FROM [view_ter_tercero_sector]
	WHERE ([id_sector]= @p_id_sector)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_ter_tercero_sectores]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercero_sectores]
GO
CREATE PROCEDURE [sel_ter_tercero_sectores]
	
	@p_filtro as varchar (8000) = '1=1',
	@p_orden as varchar (8000) = 'id',
	@p_campos as varchar (8000) = '*' AS
BEGIN
exec ('SELECT '+ @p_campos +
' FROM [view_ter_tercero_sector]
WHERE (' + @p_filtro + ') ORDER BY ' + @p_orden)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[lis_ter_tercero_sectores]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_ter_tercero_sectores]
GO
CREATE PROCEDURE [lis_ter_tercero_sectores]
	AS
BEGIN
	SELECT id AS id,
[tercero_sector] AS [ter_tercero_sector]
	FROM [ter_tercero_sector]
	WHERE active = 1
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[upd_ter_tercero_sector]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_ter_tercero_sector]
GO
CREATE PROCEDURE [upd_ter_tercero_sector]
	@p_id int = null,
		@p_uid varchar(500) = null,
		@p_eid varchar(50) = null,
		@p_id_usuario_modifico int = null,
		@p_id_usuario_creo int = null,
		@p_fechaCreacion smalldatetime = null,
		@p_tercero_sector varchar(300) = null,
		@p_id_tercero int = null,
		@p_id_sector int = null
AS
BEGIN
		UPDATE [ter_tercero_sector]
		SET	[uid] = isnull(@p_uid,[uid]),
			[eid] = isnull(@p_eid,[eid]),
			[id_usuario_modifico] = isnull(@p_id_usuario_modifico,[id_usuario_modifico]),
			[id_usuario_creo] = isnull(@p_id_usuario_creo,[id_usuario_creo]),
			[fechaModificacion] = GETDATE(),
			[tercero_sector] = isnull(@p_tercero_sector,[tercero_sector]),
			[id_tercero] = isnull(@p_id_tercero,[id_tercero]),
			[id_sector] = isnull(@p_id_sector,[id_sector])
		WHERE (id = @p_id)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ins_ter_tercero_sector]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_ter_tercero_sector]
GO
CREATE PROCEDURE [ins_ter_tercero_sector]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_tercero_sector varchar(300),
		@p_id_tercero int,
		@p_id_sector int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [ter_tercero_sector]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[tercero_sector],
			[id_tercero],
			[id_sector])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_tercero_sector,
			@p_id_tercero,
			@p_id_sector)
		SET @v_id = scope_identity()
UPDATE [ter_tercero_sector] set [uid] = [uid] + '_' + convert(varchar(20),@v_id) where [id]=@v_id
SELECT @v_id as id
RETURN @v_id
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[act_ter_tercero_sector]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_ter_tercero_sector]
GO
CREATE PROCEDURE [act_ter_tercero_sector]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [ter_tercero_sector]
	SET active=@p_active,
	[id_usuario_modifico] = isnull(@p_id_usuario_modifico,[id_usuario_modifico]),
	[fechaModificacion] = GETDATE()
	WHERE	(id = @p_id)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_ter_tercero_sector_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_ter_tercero_sector_m]
GO
CREATE PROCEDURE [reg_ter_tercero_sector_m]
	@p_id INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT 	[id] AS [id],
		[uid] AS [uid],
		[eid] AS [eid],
		[id_usuario_modifico] AS [id_usuario_modifico],
		[id_usuario_creo] AS [id_usuario_creo],
		[fechaModificacion] AS [fechaModificacion],
		[fechaCreacion] AS [fechaCreacion],
		[active] AS [active],
		[tercero_sector] AS [tercero_sector],
		[id_tercero] AS [id_tercero],
		[id_sector] AS [id_sector]
	FROM [ter_tercero_sector]
	WHERE	(id = @p_id)
	AND	(eid LIKE @p_eid+'%')

END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_ter_tercero_sectoresXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercero_sectoresXusuario_modifico_m]
GO
CREATE PROCEDURE [sel_ter_tercero_sectoresXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_tercero_sector]
	WHERE ([id_usuario_modifico]= @p_id_usuario_modifico)
	AND	(eid LIKE @p_eid+'%')
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_ter_tercero_sectoresXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercero_sectoresXusuario_creo_m]
GO
CREATE PROCEDURE [sel_ter_tercero_sectoresXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_tercero_sector]
	WHERE ([id_usuario_creo]= @p_id_usuario_creo)
	AND	(eid LIKE @p_eid+'%')
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_ter_tercero_sectoresXtercero_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercero_sectoresXtercero_m]
GO
CREATE PROCEDURE [sel_ter_tercero_sectoresXtercero_m]
	@p_id_tercero INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_tercero_sector]
	WHERE ([id_tercero]= @p_id_tercero)
	AND	(eid LIKE @p_eid+'%')
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_ter_tercero_sectoresXsector_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercero_sectoresXsector_m]
GO
CREATE PROCEDURE [sel_ter_tercero_sectoresXsector_m]
	@p_id_sector INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_ter_tercero_sector]
	WHERE ([id_sector]= @p_id_sector)
	AND	(eid LIKE @p_eid+'%')
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_ter_tercero_sectores_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_ter_tercero_sectores_m]
GO
CREATE PROCEDURE [sel_ter_tercero_sectores_m]
	@p_eid as varchar (50)='',
	@p_filtro as varchar (8000) = '1=1',
	@p_orden as varchar (8000) = 'id',
	@p_campos as varchar (8000) = '*' AS
BEGIN
exec ('SELECT '+ @p_campos +
' FROM [view_ter_tercero_sector]
WHERE  (eid like '''+@p_eid+'%'+''' ) AND (' + @p_filtro + ') ORDER BY ' + @p_orden)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[lis_ter_tercero_sectores_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_ter_tercero_sectores_m]
GO
CREATE PROCEDURE [lis_ter_tercero_sectores_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[tercero_sector] AS [ter_tercero_sector]
	FROM [ter_tercero_sector]
	WHERE active = 1
	AND	(eid LIKE @p_eid+'%')
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO
----------------------

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[aud_ter_tercero_sector]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_ter_tercero_sector]
GO
CREATE PROCEDURE [aud_ter_tercero_sector]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [ter_tercero_sector].id as id_tabla,
	[ter_tercero_sector].id_usuario_modifico as id_usuario_modifico,
	[ter_tercero_sector].id_usuario_creo as id_usuario_creo,
	[ter_tercero_sector].fechaModificacion as fechaModificacion,
	[ter_tercero_sector].fechaCreacion as fechaCreacion,
	[ter_tercero_sector].active as active,
	[ter_tercero_sector].[tercero_sector] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [ter_tercero_sector]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[ter_tercero_sector].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[ter_tercero_sector].[id_usuario_creo]
	where ' + @p_filtro)
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON
GO

----------------------
print 'ter_tercero_sector'
-----------------------------------16/07/2013 10:06:27 a.m.