
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[reg_equ_opcionAtributoDependiente]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_equ_opcionAtributoDependiente]
GO
CREATE PROCEDURE [reg_equ_opcionAtributoDependiente]
	@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],
		[opcionAtributoDependiente] AS [opcionAtributoDependiente],
		[opcionAtributoDependiente_codigo] AS [opcionAtributoDependiente_codigo],
		[id_opcionAtributoHijo] AS [id_opcionAtributoHijo],
		[id_opcionAtributoPadre] AS [id_opcionAtributoPadre]
	FROM [equ_opcionAtributoDependiente]
	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_equ_opcionAtributoDependiente]')) DROP VIEW [dbo].[view_equ_opcionAtributoDependiente]
GO

CREATE  VIEW [view_equ_opcionAtributoDependiente] AS
SELECT 	[equ_opcionAtributoDependiente].[id] AS [id],
		[equ_opcionAtributoDependiente].[uid] AS [uid],
		[equ_opcionAtributoDependiente].[eid] AS [eid],
		[equ_opcionAtributoDependiente].[id_usuario_modifico] AS [id_usuario_modifico],
		[equ_opcionAtributoDependiente].[id_usuario_creo] AS [id_usuario_creo],
		[equ_opcionAtributoDependiente].[fechaModificacion] AS [fechaModificacion],
		[equ_opcionAtributoDependiente].[fechaCreacion] AS [fechaCreacion],
		[equ_opcionAtributoDependiente].[active] AS [active],
		[equ_opcionAtributoDependiente].[opcionAtributoDependiente] AS [opcionAtributoDependiente],
		[equ_opcionAtributoDependiente].[opcionAtributoDependiente_codigo] AS [opcionAtributoDependiente_codigo],
		[equ_opcionAtributoDependiente].[id_opcionAtributoHijo] AS [id_opcionAtributoHijo],
[equ_opcionAtributo_hijo].[active] as [equ_opcionAtributo_active_hijo],
[equ_opcionAtributo_hijo].[opcionAtributo] as [equ_opcionAtributo_opcionAtributo_hijo],
[equ_opcionAtributo_hijo].[opcionAtributo_codigo] as [equ_opcionAtributo_opcionAtributo_codigo_hijo],
[equ_opcionAtributo_hijo].[orden] as [equ_opcionAtributo_orden_hijo],
[equ_opcionAtributo_hijo].[id_atributo] as [equ_opcionAtributo_id_atributo_hijo],
		[equ_opcionAtributoDependiente].[id_opcionAtributoPadre] AS [id_opcionAtributoPadre],
[equ_opcionAtributo_padre].[active] as [equ_opcionAtributo_active_padre],
[equ_opcionAtributo_padre].[opcionAtributo] as [equ_opcionAtributo_opcionAtributo_padre],
[equ_opcionAtributo_padre].[opcionAtributo_codigo] as [equ_opcionAtributo_opcionAtributo_codigo_padre],
[equ_opcionAtributo_padre].[orden] as [equ_opcionAtributo_orden_padre],
[equ_opcionAtributo_padre].[id_atributo] as [equ_opcionAtributo_id_atributo_padre],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [equ_opcionAtributoDependiente]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[equ_opcionAtributoDependiente].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[equ_opcionAtributoDependiente].[id_usuario_creo]
	INNER JOIN [equ_opcionAtributo] AS [equ_opcionAtributo_hijo] ON [equ_opcionAtributo_hijo].id=[equ_opcionAtributoDependiente].[id_opcionAtributoHijo]
	INNER JOIN [equ_opcionAtributo] AS [equ_opcionAtributo_padre] ON [equ_opcionAtributo_padre].id=[equ_opcionAtributoDependiente].[id_opcionAtributoPadre]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [equ_opcionAtributoDependiente].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
WHERE [equ_opcionAtributoDependiente].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_equ_opcionAtributoDependientesXusuario_modifico]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_opcionAtributoDependientesXusuario_modifico]
GO

CREATE PROCEDURE [sel_equ_opcionAtributoDependientesXusuario_modifico]
	@p_id_usuario_modifico INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_opcionAtributoDependiente]
	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_equ_opcionAtributoDependientesXusuario_creo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_opcionAtributoDependientesXusuario_creo]
GO

CREATE PROCEDURE [sel_equ_opcionAtributoDependientesXusuario_creo]
	@p_id_usuario_creo INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_opcionAtributoDependiente]
	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_equ_opcionAtributoDependientesXopcionAtributoHijo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_opcionAtributoDependientesXopcionAtributoHijo]
GO

CREATE PROCEDURE [sel_equ_opcionAtributoDependientesXopcionAtributoHijo]
	@p_id_opcionAtributoHijo INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_opcionAtributoDependiente]
	WHERE ([id_opcionAtributoHijo]= @p_id_opcionAtributoHijo)
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_equ_opcionAtributoDependientesXopcionAtributoPadre]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_opcionAtributoDependientesXopcionAtributoPadre]
GO

CREATE PROCEDURE [sel_equ_opcionAtributoDependientesXopcionAtributoPadre]
	@p_id_opcionAtributoPadre INT
AS
BEGIN
	SELECT * 
	FROM [view_equ_opcionAtributoDependiente]
	WHERE ([id_opcionAtributoPadre]= @p_id_opcionAtributoPadre)
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_equ_opcionAtributoDependientes]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_opcionAtributoDependientes]
GO

CREATE PROCEDURE [sel_equ_opcionAtributoDependientes]
	
	@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_equ_opcionAtributoDependiente]
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_equ_opcionAtributoDependientes]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_equ_opcionAtributoDependientes]
GO

CREATE PROCEDURE [lis_equ_opcionAtributoDependientes]
	AS
BEGIN
	SELECT id AS id,
[opcionAtributoDependiente] AS [equ_opcionAtributoDependiente]
	FROM [equ_opcionAtributoDependiente]
	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_equ_opcionAtributoDependiente]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[upd_equ_opcionAtributoDependiente]
GO
CREATE PROCEDURE [upd_equ_opcionAtributoDependiente]
	@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_opcionAtributoDependiente varchar(300) = null,
		@p_opcionAtributoDependiente_codigo varchar(100) = null,
		@p_id_opcionAtributoHijo int = null,
		@p_id_opcionAtributoPadre int = null
AS
BEGIN
		UPDATE [equ_opcionAtributoDependiente]
		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(),
			[opcionAtributoDependiente] = isnull(@p_opcionAtributoDependiente,[opcionAtributoDependiente]),
			[opcionAtributoDependiente_codigo] = isnull(@p_opcionAtributoDependiente_codigo,[opcionAtributoDependiente_codigo]),
			[id_opcionAtributoHijo] = isnull(@p_id_opcionAtributoHijo,[id_opcionAtributoHijo]),
			[id_opcionAtributoPadre] = isnull(@p_id_opcionAtributoPadre,[id_opcionAtributoPadre])
		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_equ_opcionAtributoDependiente]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[ins_equ_opcionAtributoDependiente]
GO

CREATE PROCEDURE [ins_equ_opcionAtributoDependiente]
	@p_uid varchar(500),
		@p_eid varchar(50),
		@p_id_usuario_modifico int,
		@p_id_usuario_creo int,
		@p_opcionAtributoDependiente varchar(300),
		@p_opcionAtributoDependiente_codigo varchar(100),
		@p_id_opcionAtributoHijo int,
		@p_id_opcionAtributoPadre int
AS
BEGIN
	DECLARE @v_id int
		INSERT INTO [equ_opcionAtributoDependiente]
			([uid],
			[eid],
			[id_usuario_modifico],
			[id_usuario_creo],
			[fechaModificacion],
			[fechaCreacion],
			[opcionAtributoDependiente],
			[opcionAtributoDependiente_codigo],
			[id_opcionAtributoHijo],
			[id_opcionAtributoPadre])
		VALUES(	@p_uid,
			@p_eid,
			@p_id_usuario_modifico,
			@p_id_usuario_creo,
			GETDATE(),
			GETDATE(),
			@p_opcionAtributoDependiente,
			@p_opcionAtributoDependiente_codigo,
			@p_id_opcionAtributoHijo,
			@p_id_opcionAtributoPadre)
		SET @v_id = scope_identity()
UPDATE [equ_opcionAtributoDependiente] 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_equ_opcionAtributoDependiente]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[act_equ_opcionAtributoDependiente]
GO

CREATE PROCEDURE [act_equ_opcionAtributoDependiente]
	@p_active BIT,
	@p_id INT,
@p_id_usuario_modifico int = null
AS
BEGIN
	UPDATE [equ_opcionAtributoDependiente]
	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_equ_opcionAtributoDependiente_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[reg_equ_opcionAtributoDependiente_m]
GO

CREATE PROCEDURE [reg_equ_opcionAtributoDependiente_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],
		[opcionAtributoDependiente] AS [opcionAtributoDependiente],
		[opcionAtributoDependiente_codigo] AS [opcionAtributoDependiente_codigo],
		[id_opcionAtributoHijo] AS [id_opcionAtributoHijo],
		[id_opcionAtributoPadre] AS [id_opcionAtributoPadre]
	FROM [equ_opcionAtributoDependiente]
	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_equ_opcionAtributoDependientesXusuario_modifico_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_opcionAtributoDependientesXusuario_modifico_m]
GO

CREATE PROCEDURE [sel_equ_opcionAtributoDependientesXusuario_modifico_m]
	@p_id_usuario_modifico INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_opcionAtributoDependiente]
	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_equ_opcionAtributoDependientesXusuario_creo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_opcionAtributoDependientesXusuario_creo_m]
GO

CREATE PROCEDURE [sel_equ_opcionAtributoDependientesXusuario_creo_m]
	@p_id_usuario_creo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_opcionAtributoDependiente]
	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_equ_opcionAtributoDependientesXopcionAtributoHijo_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_opcionAtributoDependientesXopcionAtributoHijo_m]
GO

CREATE PROCEDURE [sel_equ_opcionAtributoDependientesXopcionAtributoHijo_m]
	@p_id_opcionAtributoHijo INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_opcionAtributoDependiente]
	WHERE ([id_opcionAtributoHijo]= @p_id_opcionAtributoHijo)
	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_equ_opcionAtributoDependientesXopcionAtributoPadre_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_opcionAtributoDependientesXopcionAtributoPadre_m]
GO

CREATE PROCEDURE [sel_equ_opcionAtributoDependientesXopcionAtributoPadre_m]
	@p_id_opcionAtributoPadre INT,
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT *
	FROM [view_equ_opcionAtributoDependiente]
	WHERE ([id_opcionAtributoPadre]= @p_id_opcionAtributoPadre)
	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_equ_opcionAtributoDependientes_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[sel_equ_opcionAtributoDependientes_m]
GO

CREATE PROCEDURE [sel_equ_opcionAtributoDependientes_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_equ_opcionAtributoDependiente]
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_equ_opcionAtributoDependientes_m]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[lis_equ_opcionAtributoDependientes_m]
GO

CREATE PROCEDURE [lis_equ_opcionAtributoDependientes_m]
	@p_eid as varchar(50) = ''
AS
BEGIN
	SELECT id AS id,
[opcionAtributoDependiente] AS [equ_opcionAtributoDependiente]
	FROM [equ_opcionAtributoDependiente]
	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_equ_opcionAtributoDependiente]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[aud_equ_opcionAtributoDependiente]
GO

CREATE PROCEDURE [aud_equ_opcionAtributoDependiente]
	@p_filtro as varchar (8000) = '1=1'
AS
BEGIN
exec(	'SELECT [equ_opcionAtributoDependiente].id as id_tabla,
	[equ_opcionAtributoDependiente].id_usuario_modifico as id_usuario_modifico,
	[equ_opcionAtributoDependiente].id_usuario_creo as id_usuario_creo,
	[equ_opcionAtributoDependiente].fechaModificacion as fechaModificacion,
	[equ_opcionAtributoDependiente].fechaCreacion as fechaCreacion,
	[equ_opcionAtributoDependiente].active as active,
	[equ_opcionAtributoDependiente].[opcionAtributoDependiente] as campoPrincipal,
	[seg_usuario_creo].usuario as usuarioCreo,
	[seg_usuario_modifico].usuario as usuarioModifico

	FROM [equ_opcionAtributoDependiente]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[equ_opcionAtributoDependiente].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[equ_opcionAtributoDependiente].[id_usuario_creo]
	where ' + @p_filtro)
END
GO

SET QUOTED_IDENTIFIER OFF 
GO

SET ANSI_NULLS ON
GO


----------------------
print 'equ_opcionAtributoDependiente'
-----------------------------------31/01/2020 8:32:07