--SELECT *
--FROM r_CloudBillQuery
UPDATE r_CloudBillQuery SET FFieldKeys = N'/*dialect*/  
SELECT  B.FNUMBER SRTYPE_ID,S1.FBILLNO AUDIT_ID,S2.FSEQ ERP_AUDIT_LINE,S2.FORDERNO SO_ID,FORDERSEQ SO_SEQ,C.FNUMBER CUST_ID,M.FNUMBER MTRL_ID   
	 ,FPLANDELIVERYDATE H_DATE,K.FNUMBER STORE_ID,CK.FNUMBER TOSTORE_ID,S2.FQTY QUANTITY,S2.FNOTE REMARK,(SELECT  A3.FAUXPTYNUMBER 
	 FROM T_BD_FLEXSITEMDETAILV A2 --ON A2.FID=S2.FAUXPROPID    
	  INNER JOIN T_BD_AUXPTYVALUEENTITY A3 ON A3.FAUXPTYID=A2.FF100001   
	  INNER JOIN T_BD_AUXPTYVALUE a1 on a1.FAUXPTYVALUEID=a3.FAUXPTYVALUEID  and a1.FMATERIALID=S2.FMATERIALID 
	  where  A2.FID=S2.FAUXPROPID 
	 ) MTRL_VER
	 ,s2.F_PKGR_DrawingNo ''CUST_DN''
	 ,CASE WHEN ISNULL(s2.FCustMatID,'''') = '''' THEN '''' ELSE (SELECT tt.fnumber FROM T_V_SAL_CUSTMATMAPPING tt WHERE s2.FCustMatID = tt.fid) END ''CUST_MTRL_ID''
  FROM T_SAL_DELIVERYNOTICE S1   
	  INNER JOIN T_SAL_DELIVERYNOTICEENTRY S2 ON S2.FID=S1.FID   
	  INNER JOIN T_BD_CUSTOMER C ON C.FCUSTID=S1.FCUSTOMERID   
	  INNER JOIN T_BD_MATERIAL M ON M.FMATERIALID=S2.FMATERIALID   
	  --INNER JOIN T_BD_FLEXSITEMDETAILV A2 ON A2.FID=S2.FAUXPROPID    
	  --INNER JOIN T_BD_AUXPTYVALUEENTITY A3 ON A3.FAUXPTYID=A2.FF100001   
	  --INNER JOIN T_BD_AUXPTYVALUE a1 on a1.FAUXPTYVALUEID=a3.FAUXPTYVALUEID  and a1.FMATERIALID=S2.FMATERIALID   
	  LEFT JOIN T_SAL_ORDER S ON S.FBILLNO=S2.FORDERNO   
	  LEFT JOIN T_BAS_BILLTYPE B ON B.FBILLTYPEID=S.FBILLTYPEID   
	  LEFT JOIN T_BD_STOCK K ON K.FSTOCKID=S2.FSHIPMENTSTOCKID   
	  LEFT JOIN T_BD_STOCK CK ON CK.FSTOCKID=S2.FDESTSTOCK
  WHERE 1=1 
	AND S1.FSALEORGID=@defaultorgid
	and S1.FDOCUMENTSTATUS=''C''
	AND S1.FCANCELSTATUS=''A''
	AND S1.FAPPROVEDATE>=''@D''
	--AND S1.FAPPROVEDATE>=''2023-05-27''
  ORDER BY S2.FID,S2.FSEQ	

'
WHERE FID = 18