/****** Object:  View [dbo].[V_PRC_FILIAL_RELACAO_DEPOSITOS]    Script Date: 05/08/2013 09:22:37 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[V_PRC_FILIAL_RELACAO_DEPOSITOS]'))
DROP VIEW [dbo].[V_PRC_FILIAL_RELACAO_DEPOSITOS]
GO
GO
--SELECT * FROM V_PRC_FILIAL_RELACAO_DEPOSITOS
CREATE VIEW V_PRC_FILIAL_RELACAO_DEPOSITOS AS
SELECT 
	DISTINCT  
	F.CD_EMP,
	F.NM_FANT, 
	F.CD_FILIAL, 
	'NAO' AS DEPOSITO_AUXILIAR,
	F.CD_FILIAL AS CD_DEPOSITO,
	D.CD_CTR_DEPO
FROM  
	PRC_FILIAL_DEPOSITO D INNER JOIN PRC_FILIAL F ON  
	D.CD_EMP      = F.CD_EMP AND 
	D.CD_FILIAL   = F.CD_FILIAL  
UNION ALL
SELECT 
	DISTINCT  
	F.CD_EMP,
	F.NM_FANT, 
	CD_FILIAL_DEPO_AUX AS CD_FILIAL, 
	'SIM' AS  DEPOSITO_AUXILIAR,
	PRC_FILIAL_DEPOSITO.CD_FILIAL AS CD_DEPOSITO,
	PRC_FILIAL_DEPOSITO.CD_CTR_DEPO
FROM  
	PRC_FILIAL_DEPOSITO_AUXILIAR D INNER JOIN PRC_FILIAL F ON  
	D.CD_EMP  = F.CD_EMP AND 
	D.CD_FILIAL_DEPO_AUX = F.CD_FILIAL 
	INNER JOIN PRC_FILIAL_DEPOSITO ON
	D.CD_EMP = PRC_FILIAL_DEPOSITO.CD_EMP AND
	D.CD_CTR_DEPO = PRC_FILIAL_DEPOSITO.CD_CTR_DEPO
