 
/****** Object:  View [dbo].[V_PRC_FILIAL_DEPOSITO]    Script Date: 02/03/2014 16:50:33 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[V_PRC_FILIAL_DEPOSITO]'))
DROP VIEW [dbo].[V_PRC_FILIAL_DEPOSITO]
GO
 
/****** Object:  View [dbo].[V_PRC_FILIAL_DEPOSITO]    Script Date: 02/03/2014 16:50:34 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO 

CREATE VIEW [dbo].[V_PRC_FILIAL_DEPOSITO]
AS
SELECT DISTINCT 
	 DEPOS.CD_EMP,
	 DEPOS.CD_CTR_DEPO,
	 DEPOS.CD_FILIAL,
	 DEPOS.FLAG_DEPO,
	 DEPOS.CD_FILIAL_DEPO
FROM 
(SELECT  
	A.CD_EMP,
	A.CD_CTR_DEPO,
	A.CD_FILIAL,
	1 FLAG_DEPO,
	CD_FILIAL CD_FILIAL_DEPO
FROM 
	PRC_FILIAL_DEPOSITO A
UNION ALL
SELECT
	B.CD_EMP,
	B.CD_CTR_DEPO,
	B.CD_FILIAL,
	CASE WHEN B.CD_FILIAL = A.CD_FILIAL THEN 
		1 ELSE 0  END FLAG_DEPO,
	A.CD_FILIAL CD_FILIAL_DEPO
FROM 
	PRC_FILIAL_DEPOSITO_PRC_FILIAL  B
	INNER JOIN PRC_FILIAL_DEPOSITO A ON
	B.CD_CTR_DEPO = A.CD_CTR_DEPO) DEPOS
	INNER JOIN PRC_FILIAL LJ ON 
	DEPOS.CD_EMP    = LJ.CD_EMP AND
	DEPOS.CD_FILIAL = LJ.CD_FILIAL  
WHERE
	LJ.STS_FILIAL =0

GO


