﻿IF EXISTS(SELECT * FROM sysobjects WHERE name = 'V_SYNC_STKMiscellaneous')--单引号里面为视图名称
	DROP VIEW V_SYNC_STKMiscellaneous--视图名称
GO
CREATE VIEW V_SYNC_STKMiscellaneous AS
SELECT 
	t0.FID
	,t0.FBILLNO
	--,t1.FDECLAENTCREDITCODE
	--,t1.FDECLAENTCUSCODE
	--,t1.FDECLAENTNAME
	--,t1.FENTCREDITCODE
	--,t1.FENTCUSCODE
	--,t1.FENTNAME
	,t1e.FID AS LEDGERID
	,t1e.FENTRYID AS BookNumId
	,t1e.FBOOKNUM
	,t1e.FCUSTOMSCODE
	,t0.FDATE
	,t0.FSTATUS AS SyncStatus
	,t0e.FSEQ
	,'QTRK' AS storageType
	,t0e.FINSTOCKDATE
	,t2.FNUMBER AS materialCd
	,t2_l.FNAME AS materialDesc
	,t0e.FQTY AS storageCnt
	,t3.FNUMBER AS calcUnit
	,t0e.FHSCODE AS hscode
	,t0e.FINVENTORY as inventory
	,t0e.FINVENTORYORDERNO as inventoryOrderNo
	,t4.FNUMBER as warehouseCd
	,t5.FNUMBER as warehousePosiCd
	,'' AS bookSn 
	,t7.FNAME AS inputMan
	,ROW_NUMBER() OVER(PARTITION BY t0e.FID,t1e.FENTRYID ORDER BY t0e.FSEQ ASC) AS itemNo
FROM T_STK_MISCELLANEOUS t0
	INNER JOIN T_STK_MISCELLANEOUSENTRY t0e on t0.FID = t0e.FID
	INNER JOIN T_BD_LEDGERSTOCK t1es on t1es.FLEDGERSTOCKID = t0e.FSTOCKID
	INNER JOIN T_BD_LEDGERENTRY t1e on t1e.FENTRYID = t1es.FENTRYID
	--INNER JOIN T_BD_LEDGER t1 on t1.FID = t1e.FID
	INNER JOIN T_BD_MATERIAL t2 on t2.FMATERIALID = t0e.FMATERIALID
	INNER JOIN T_BD_MATERIAL_L t2_l on t2_l.FMATERIALID = t2.FMATERIALID AND t2_l.FLOCALEID = 2052
	INNER JOIN T_BD_UNIT t3 on t3.FUNITID = t0e.FUNITID
	INNER JOIN T_BD_STOCK t4 on t4.FSTOCKID = t0e.FSTOCKID
	LEFT JOIN T_BAS_FLEXVALUESDETAIL t6 on t6.FID = t0e.FSTOCKID
	LEFT JOIN T_BAS_FLEXVALUESENTRY t5 ON t5.FENTRYID = t6.FF100001 OR t5.FENTRYID = t6.FF100002
	INNER JOIN T_SEC_USER t7 on t7.FUSERID = t0.FCREATORID
WHERE t0.FDOCUMENTSTATUS = 'C' AND t1.FDOCUMENTSTATUS = 'C'