

IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[view_ter_contacto_sucursal]')) DROP VIEW [dbo].[view_ter_contacto_sucursal]
GO
CREATE  VIEW [view_ter_contacto_sucursal] AS
SELECT 	[ter_contacto_sucursal].[id] AS [id],
		[ter_contacto_sucursal].[uid] AS [uid],
		[ter_contacto_sucursal].[eid] AS [eid],
		[ter_contacto_sucursal].[id_usuario_modifico] AS [id_usuario_modifico],
		[ter_contacto_sucursal].[id_usuario_creo] AS [id_usuario_creo],
		[ter_contacto_sucursal].[fechaModificacion] AS [fechaModificacion],
		[ter_contacto_sucursal].[fechaCreacion] AS [fechaCreacion],
		[ter_contacto_sucursal].[active] AS [active],
		[ter_contacto_sucursal].[contacto_sucursal] AS [contacto_sucursal],
		[ter_contacto_sucursal].[id_contacto] AS [id_contacto],
[ter_contacto].[contacto] as [ter_contacto_contacto],
[ter_contacto].[contacto_codigo] as [ter_contacto_contacto_codigo],
[ter_contacto].[telefono] as [ter_contacto_telefono],
[ter_contacto].[Fax] as [ter_contacto_Fax],
[ter_contacto].[Email] as [ter_contacto_Email],
[ter_contacto].[Direccion] as [ter_contacto_Direccion],
[ter_contacto].[telefonoMovil] as [ter_contacto_telefonoMovil],
[ter_contacto].[id_tercero] as [ter_contacto_id_tercero],
[ter_contacto].[id_usuario] as [ter_contacto_id_usuario],
[ter_contacto].[cargo] as [ter_contacto_cargo],
[ter_contacto].[codigoAcceso] as [ter_contacto_codigoAcceso],
[ter_contacto].[id_cargoContacto] as [ter_contacto_id_cargoContacto],
[ter_contacto].[accesoActivo] as [ter_contacto_accesoActivo],
		[ter_contacto_sucursal].[id_sucursal] AS [id_sucursal],
[ter_sucursal].[sucursal] as [ter_sucursal_sucursal],
[ter_sucursal].[sucursal_codigo] as [ter_sucursal_sucursal_codigo],
[ter_sucursal].[contacto] as [ter_sucursal_contacto],
[ter_sucursal].[telefono] as [ter_sucursal_telefono],
[ter_sucursal].[Fax] as [ter_sucursal_Fax],
[ter_sucursal].[Email] as [ter_sucursal_Email],
[ter_sucursal].[Direccion] as [ter_sucursal_Direccion],
[ter_sucursal].[paraVenta] as [ter_sucursal_paraVenta],
[ter_sucursal].[paraSoporte] as [ter_sucursal_paraSoporte],
[ter_sucursal].[paraAlquiler] as [ter_sucursal_paraAlquiler],
[ter_sucursal].[id_tercero] as [ter_sucursal_id_tercero],
[ter_sucursal].[id_zona] as [ter_sucursal_id_zona],
[ter_sucursal].[id_usuario_asesor] as [ter_sucursal_id_usuario_asesor],
[ter_sucursal].[id_usuario_tecnico] as [ter_sucursal_id_usuario_tecnico],
[ter_sucursal].[id_sucursal] as [ter_sucursal_id_sucursal],
[ter_sucursal].[id_listaPrecio] as [ter_sucursal_id_listaPrecio],
[ter_sucursal].[id_estadoTercero] as [ter_sucursal_id_estadoTercero],
[ter_sucursal].[codigorecursiva] as [ter_sucursal_codigorecursiva],
		[empresaCodigo].[empresa] As [multiempresa]
FROM [ter_contacto_sucursal]
	INNER JOIN [seg_usuario] AS [seg_usuario_modifico] ON [seg_usuario_modifico].id=[ter_contacto_sucursal].[id_usuario_modifico]
	INNER JOIN [seg_usuario] AS [seg_usuario_creo] ON [seg_usuario_creo].id=[ter_contacto_sucursal].[id_usuario_creo]
	INNER JOIN [ter_contacto] ON [ter_contacto].id=[ter_contacto_sucursal].[id_contacto]
	INNER JOIN [ter_sucursal] ON [ter_sucursal].id=[ter_contacto_sucursal].[id_sucursal]
	INNER JOIN [gen_empresa] AS empresaCodigo ON [ter_contacto_sucursal].eid = [empresaCodigo].[codigo] and empresaCodigo.active=1
