exec sp_executesql N'SELECT OUTACCT.FYEAR, OUTACCT.FPERIOD, Material.FMasterID, process.FENTRYID fprocessentryid, seq.FEntryId fseqentryid,
seq.FSEQ, CASE WHEN seq.FINDEXENTRYID = 32 THEN ''0'' ELSE seq.FINOUTINDEX END finoutindex, FOUTINSTOCKTYPE, '' '' fvoucherbillno,
seq.FBILLTYPEID fsrcbilltypeid, BillType_L.FName fsrcbilltype, FBILLFROMID, FBILLID, FBillEntryId, seq.FBILLNO, FBILLSEQID, bom.FNumber fbomid,
dim.FMtoNo fplantrack, dim.FPROJECTNO fitemnumber, seq.FBASEUNITID, unitL.FName funitid, ISNULL(batch.FNumber, '' '') flotno,
dim.FAuxPropId fassipropertyid, dim.FCARGOOWNERTYPE fownertype, N'' '' fseqno, seq.FQTY fbaseqty, FACCTGPRICE fprice,
CASE WHEN seq.FINDEXENTRYID = 32 THEN (0 - FACCTGAMOUNT) ELSE FACCTGAMOUNT END famount, acctgRange.FNumber facctgrangeno,
acctgRangeL.FName facctgrangename, dim.FAuxPropId faidpropertyid, FBillDate fbusinessdate, Material.Fnumber fmaterialno,
MaterialL.FName fmaterialname, ISNULL(matgruopL.FNAME, N'' '') fmaterialgroup, MaterialL.FSPECIFICATION fmodel,
OwnerOrgs.FNumber fownerno, OwnerOrgsL.FName fownername, StockOrgs.FNumber fstockorgno, StockOrgsL.FName fstockorgname,
stock.FNumber fstockno, stockL.FName fstockname, dim.FStockLocId fstockplaceid, bin.FNumber fstockplaceno, binL.FName fstockplacename,
enumItemL.FCAPTION fdocumentstatus, N'' '' faidpropertyname, seq.FTransferBizType, seq.FTransferDirect, stockdim.FStockState,
dim.FSTOCKSTATUSID, cald.FACCTSYSTEMID facctsystemid, cald.FFinOrgID facctorgid, cald.FACCTPOLICYID facctpolicyid, 0 fisvoucher,
seq.FTransferDirect, unit.FPRECISION fqtydigits, seq.FGiveAway, ''0'' fhasadjustbill, '' '' fadjustbillno, ISNULL(dept_L.FName, N'' '') fdeptid,
ISNULL(gory_L.FName, N'' '') fcategoryid, ''0'' forderby, seq.FISSETTLE, dim.FENTRYID finivdimeentryid, 0 ftargetcost, 0 fwarningrate, 0 fwarningrateup,
seq.FACCTGPRICESOURCE, seq.FACCTGPRICEDATE, seq.FACCTGVOUCHERDATE FROM T_HS_OutInStockSeq seq
INNER JOIN T_HS_InivStockDimension dim ON seq.FDimeEntryId = dim.FEntryId
LEFT OUTER JOIN T_BD_DEPARTMENT_L dept_L ON (seq.FDEPTID = dept_L.FDEPTID AND dept_L.FLOCALEID = 2052)
INNER JOIN T_HS_AcctgProcess process ON process.FEntryId = seq.FPROCESSENTRYID
INNER JOIN T_HS_StockDimension stockDim ON process.FDimeEntryId = stockDim.FEntryId
INNER JOIN T_HS_OUTACCTG outacct ON outacct.FID = process.FID INNER JOIN T_HS_CALDIMENSIONS cald ON outacct.FDIMENSIONID = cald.FDIMENSIONID
INNER JOIN T_HS_ACCTGRANGE acctgRange ON acctgRange.FACCTGRANGEID = stockDim.FAcctgRangeId LEFT OUTER JOIN T_HS_ACCTGRANGE_L acctgRangeL ON
(acctgRangeL.FACCTGRANGEID = stockDim.FAcctgRangeId AND acctgRangeL.FLOCALEID = 2052)
LEFT OUTER JOIN T_BAS_BILLTYPE_L BillType_L ON (BillType_L.FBILLTYPEID = seq.FBILLTYPEID AND BillType_L.FLOCALEID = 2052)
INNER JOIN t_bd_Material material ON material.fmaterialID = dim.fmaterialID INNER JOIN
(SELECT mats.FMATERIALID, matb.FCATEGORYID, matb.FBASEUNITID FROM T_BD_MATERIAL mat
INNER JOIN T_BD_MATERIALBASE matb ON mat.FMATERIALID = matb.FMATERIALID
INNER JOIN T_BD_MATERIAL mats ON mat.FMASTERID = mats.FMASTERID WHERE mat.FUSEORGID = 100399
UNION ALL SELECT matb.FMATERIALID, matb.FCATEGORYID, matb.FBASEUNITID FROM T_BD_MATERIALBASE matb
INNER JOIN T_BD_MATERIAL mats ON mats.FMATERIALID = matb.FMATERIALID
LEFT OUTER JOIN T_BD_MATERIAL mat ON (mats.FMASTERID = mat.FMASTERID AND mat.FUSEORGID = 100399)
WHERE (mat.FMATERIALID IS NULL)) materialbase ON materialBase.FMaterialID = material.FMaterialID
LEFT OUTER JOIN T_BD_MATERIALGROUP_L matgruopL ON (material.FMATERIALGROUP = matgruopL.FID AND matgruopL.FLOCALEID = 2052)
INNER JOIN T_BD_MATERIALCATEGORY gory ON materialBase.FCATEGORYID = gory.FCATEGORYID LEFT OUTER JOIN T_BD_MATERIALCATEGORY_L gory_L ON
(gory.FCATEGORYID = gory_L.FCATEGORYID AND gory_L.FLOCALEID = 2052) LEFT OUTER JOIN t_bd_Material_l materialL ON
(materialL.FMaterialID = Material.FMATERIALID AND MaterialL.FLOCALEID = 2052) LEFT OUTER JOIN T_ORG_ORGANIZATIONS StockOrgs
ON StockOrgs.FORGID = dim.FStockOrgId LEFT OUTER JOIN T_ORG_ORGANIZATIONS_L StockOrgsL ON (StockOrgsL.FORGID = StockOrgs.FORGID
AND StockOrgsL.FLOCALEID = 2052) LEFT OUTER JOIN T_ORG_ORGANIZATIONS OwnerOrgs ON OwnerOrgs.FORGID = dim.FCargoOwnerId
LEFT OUTER JOIN T_ORG_ORGANIZATIONS_L
OwnerOrgsL ON (OwnerOrgsL.FORGID = dim.FCargoOwnerId AND OwnerOrgsL.FLOCALEID = 2052)
LEFT OUTER JOIN T_BD_LOTMASTER batch ON batch.FLOTID = dim.FLOT LEFT OUTER JOIN t_BD_Stock stock ON stock.FStockId =
dim.FStockID LEFT OUTER JOIN t_BD_Stock_L stockL ON (stockL.FStockId = stock.FStockId AND stockL.FLOCALEID = 2052) LEFT OUTER JOIN T_BD_UNIT unit ON unit.funitid = seq.FBASEUNITID LEFT OUTER JOIN T_BD_UNIT_L
unitL ON (unitL.funitid = seq.FBASEUNITID AND unitL.FLOCALEID = 2052) LEFT OUTER JOIN T_BAS_FLEXVALUES bin ON bin.FID = dim.FSTOCKLOCID LEFT OUTER JOIN T_BAS_FLEXVALUES_L binL ON (binL.FID = bin.FID AND
binL.FLocaleID = 2052) LEFT OUTER JOIN t_eng_bom bom ON bom.FID = dim.FBOMID INNER JOIN T_META_FORMENUMITEM enumItem ON (enumItem.fid = ''868a81ef-bd35-4f98-a172-4e9293655411'' AND enumItem.FVALUE =
seq.FDocumentStatus) LEFT OUTER JOIN T_META_FORMENUMITEM_L enumItemL ON (enumItemL.FENUMID = enumItem.FENUMID AND enumItemL.FLOCALEID = 2052) WHERE (((((((((((cald.FACCTSYSTEMID = 1 AND cald.FFinOrgID =
100399) AND cald.FACCTPOLICYID = 1) AND OUTACCT.FYEAR = 2020) AND OUTACCT.FPERIOD = 5) AND (seq.FBILLNO >= ''B1WWI2004300011'')) AND (seq.FBILLNO <= ''B1WWI2004300011'')) AND (material.FNumber >=
''101010000-0626'')) AND (material.FNumber <= ''101010000-0626'')) AND (seq.FAcctgAmount >= -9999999999999)) AND (seq.FAcctgAmount <= 9999999999999)) AND seq.FDocumentStatus = ''C'') UNION ALL SELECT
OUTACCT.FYEAR, OUTACCT.FPERIOD, MAT.FMASTERID, 0 fprocessentryid, ADJENTRY.FENTRYID, ADJENTRY.FSEQ, ''0'' finoutindex, ''7'' foutinstocktype, '' '' fvoucherbillno, T0.FBILLTYPEID, BILLTYPE_L.FNAME
fsrcbilltype, ''HS_AdjustmentBill'' fbillfromid, T0.FID, ADJENTRY.FENTRYID, CONVERT(VARCHAR(8000), T0.FBILLNO), ADJENTRY.FSEQ, BOM.FNUMBER fbomid, DIM.FMTONO fplantrack, DIM.FPROJECTNO fitemnumber,
MAT_BASE.FBASEUNITID, UNITL.FNAME funitid, BATCH.FNUMBER flotno, DIM.FAUXPROPID fassipropertyid, DIM.FCARGOOWNERTYPE fownertype, N'' '' fseqno, ADJENTRY.FQTY, ADJENTRY.FPRICE, ADJENTRY.FADJUSTMENTAMOUNT famount, ACCTGRANGE.FNUMBER facctgrangeno, ACCTGRANGEL.FNAME facctgrangename, DIM.FAUXPROPID faidpropertyid, T0.FDATE fbusinessdate, MAT.FNUMBER fmaterialno, MAT_L.FNAME fmaterialname, ISNULL(matgruopL.FNAME, N'' '') fmaterialgroup, MAT_L.FSPECIFICATION fmodel, OWNERORGS.FNUMBER fownerno, OWNERORGSL.FNAME fownername, STOCKORGS.FNUMBER fstockorgno, STOCKORGSL.FNAME fstockorgname, STOCK.FNUMBER fstockno, STOCKL.FNAME fstockname, DIM.FSTOCKLOCID fstockplaceid, BIN.FNUMBER fstockplaceno, BINL.FNAME fstockplacename, ENUMITEML.FCAPTION fdocumentstatus, N'' '' faidpropertyname, '' '' ftransferbiztype, '' '' ftransferdirect, STOCKDIM.FSTOCKSTATE, DIM.FSTOCKSTATUSID, T0.FACCTGSYSTEMID, T0.FACCTORGID, T0.FACCTPOLICYID, 0 fisvoucher, '' '' ftransferdirect, UNIT.FPRECISION fqtydigits, ''0'' fgiveaway, ''0'' fhasadjustbill, '' '' fadjustbillno, N'' '' fdeptid, GORY_L.FNAME fcategoryid, ''0'' forderby, '' '' fissettle, DIM.FENTRYID finivdimeentryid, 0 ftargetcost, 0 fwarningrate, 0 fwarningrateup, ''0'' facctgpricesource, NULL facctgpricedate, NULL facctgvoucherdate FROM T_HS_AdjustmentBill T0 INNER JOIN T_HS_AdjustmentBillEntry ADJENTRY ON T0.FID = ADJENTRY.FID INNER JOIN T_HS_InivStockDimension DIM ON (((((((((((ADJENTRY.FSTOCKID = DIM.FSTOCKID AND ADJENTRY.FSTOCKLOCID = DIM.FSTOCKLOCID) AND ADJENTRY.FLOT = DIM.FLOT) AND ADJENTRY.FAUXPROPID = DIM.FAUXPROPID) AND ADJENTRY.FBOMID = DIM.FBOMID) AND ADJENTRY.FMTONO = DIM.FMTONO) AND ADJENTRY.FPROJECTNO = DIM.FPROJECTNO) AND ADJENTRY.FMATERIALID = DIM.FMATERIALID) AND ADJENTRY.FOWNERTYPEID = DIM.FCARGOOWNERTYPE) AND ADJENTRY.FOWNERID = DIM.FCARGOOWNERID) AND ADJENTRY.FSTOCKORGID = DIM.FSTOCKORGID) AND ADJENTRY.FSTOCKSTATUSID = DIM.FSTOCKSTATUSID) INNER JOIN T_HS_StockDimension STOCKDIM ON DIM.FDIMEENTRYID = STOCKDIM.FENTRYID INNER JOIN T_HS_ACCTGRANGE ACCTGRANGE ON (((((STOCKDIM.FACCTGRANGEID = ACCTGRANGE.FACCTGRANGEID AND ACCTGRANGE.FACCTGSYSTEMID = T0.FACCTGSYSTEMID) AND ACCTGRANGE.FACCTGORGID = T0.FACCTORGID) AND ACCTGRANGE.FACCTPOLICYID = T0.FACCTPOLICYID) AND ACCTGRANGE.FDOCUMENTSTATUS = ''C'') AND ACCTGRANGE.FFORBIDSTATUS = ''A'') INNER JOIN t_bd_Material MAT ON DIM.FMATERIALID = MAT.FMATERIALID INNER JOIN (SELECT mats.FMATERIALID, matb.FCATEGORYID,
matb.FBASEUNITID FROM T_BD_MATERIAL mat INNER JOIN T_BD_MATERIALBASE matb ON mat.FMATERIALID = matb.FMATERIALID INNER JOIN T_BD_MATERIAL mats ON mat.FMASTERID = mats.FMASTERID WHERE mat.FUSEORGID = 100399
UNION ALL SELECT matb.FMATERIALID, matb.FCATEGORYID, matb.FBASEUNITID FROM T_BD_MATERIALBASE matb INNER JOIN T_BD_MATERIAL mats ON mats.FMATERIALID = matb.FMATERIALID LEFT OUTER JOIN T_BD_MATERIAL mat ON
(mats.FMASTERID = mat.FMASTERID AND mat.FUSEORGID = 100399) WHERE (mat.FMATERIALID IS NULL)) mat_base ON DIM.FMATERIALID = MAT_BASE.FMATERIALID INNER JOIN T_BD_ACCOUNTPERIOD OUTACCT ON (T0.FDATE BETWEEN
OUTACCT.FPERIODSTARTDATE AND OUTACCT.FPERIODENDDATE) INNER JOIN T_FA_ACCTPOLICY T2 ON (OUTACCT.FID = T2.FACCTCALENDARID AND T2.FACCTPOLICYID = T0.FACCTPOLICYID) INNER JOIN T_META_FORMENUMITEM ENUMITEM ON
ENUMITEM.FID = ''868a81ef-bd35-4f98-a172-4e9293655411'' INNER JOIN T_BD_MATERIALCATEGORY GORY ON (MAT_BASE.FCATEGORYID = GORY.FCATEGORYID AND ENUMITEM.FVALUE = T0.FDOCUMENTSTATUS) LEFT OUTER JOIN
t_bd_Material_l MAT_L ON (DIM.FMATERIALID = MAT_L.FMATERIALID AND MAT_L.FLOCALEID = 2052) LEFT OUTER JOIN T_BD_MATERIALGROUP_L matgruopL ON (MAT.FMATERIALGROUP = matgruopL.FID AND matgruopL.FLOCALEID = 2052)
LEFT OUTER JOIN T_BAS_BILLTYPE_L BILLTYPE_L ON (BILLTYPE_L.FBILLTYPEID = T0.FBILLTYPEID AND BILLTYPE_L.FLOCALEID = 2052) LEFT OUTER JOIN t_eng_bom BOM ON BOM.FID = DIM.FBOMID LEFT OUTER JOIN T_BD_LOTMASTER
BATCH ON BATCH.FLOTID = DIM.FLOT LEFT OUTER JOIN T_HS_ACCTGRANGE_L ACCTGRANGEL ON (ACCTGRANGEL.FACCTGRANGEID = STOCKDIM.FACCTGRANGEID AND ACCTGRANGEL.FLOCALEID = 2052) LEFT OUTER JOIN T_ORG_ORGANIZATIONS
OWNERORGS ON OWNERORGS.FORGID = DIM.FCARGOOWNERID LEFT OUTER JOIN T_ORG_ORGANIZATIONS_L OWNERORGSL ON (OWNERORGSL.FORGID = DIM.FCARGOOWNERID AND OWNERORGSL.FLOCALEID = 2052) LEFT OUTER JOIN t_BD_Stock STOCK ON
STOCK.FSTOCKID = DIM.FSTOCKID LEFT OUTER JOIN t_BD_Stock_L STOCKL ON (STOCKL.FSTOCKID = STOCK.FSTOCKID AND STOCKL.FLOCALEID = 2052) LEFT OUTER JOIN T_ORG_ORGANIZATIONS STOCKORGS ON STOCKORGS.FORGID = DIM.FSTOCKORGID LEFT OUTER JOIN T_ORG_ORGANIZATIONS_L STOCKORGSL ON (STOCKORGSL.FORGID = STOCKORGS.FORGID AND STOCKORGSL.FLOCALEID = 2052) LEFT OUTER JOIN T_BAS_FLEXVALUES BIN ON BIN.FID = DIM.FSTOCKLOCID LEFT OUTER JOIN T_BAS_FLEXVALUES_L BINL ON (BINL.FID = BIN.FID AND BINL.FLOCALEID = 2052) LEFT OUTER JOIN T_META_FORMENUMITEM_L ENUMITEML ON (ENUMITEML.FENUMID = ENUMITEM.FENUMID AND ENUMITEML.FLOCALEID = 2052) LEFT OUTER JOIN T_BD_UNIT UNIT ON UNIT.FUNITID = MAT_BASE.FBASEUNITID LEFT OUTER JOIN T_BD_UNIT_L UNITL ON (UNITL.FUNITID = MAT_BASE.FBASEUNITID AND UNITL.FLOCALEID = 2052) LEFT OUTER JOIN T_BD_MATERIALCATEGORY_L GORY_L ON (GORY.FCATEGORYID = GORY_L.FCATEGORYID AND GORY_L.FLOCALEID = 2052) WHERE (((((((((((((T0.FBUSINESSTYPE = ''0'' OR T0.FISACCTGGENERATE = ''1'') AND T0.FACCTGSYSTEMID = @ACCTSYSID) AND T0.FACCTORGID = @ACCTORGID) AND T0.FACCTPOLICYID = @ACCTPOLICYID) AND OUTACCT.FYEAR = 2020) AND OUTACCT.FPERIOD = 5) AND (T0.FBILLNO >= @BEGINBILLNO)) AND (T0.FBILLNO <= @ENDBILLNO)) AND (MAT.FNUMBER >= @BEGINMATERIAL)) AND (MAT.FNumber <= @ENDMATERIAL)) AND (ADJENTRY.FADJUSTMENTAMOUNT >= @BEGINAMOUNT)) AND (ADJENTRY.FADJUSTMENTAMOUNT <= @ENDAMOUNT)) AND T0.FDocumentStatus = ''C'')',N'@ACCTSYSID int,@ACCTORGID int,@ACCTPOLICYID int,@BEGINBILLNO nvarchar(15),@ENDBILLNO nvarchar(15),@BEGINMATERIAL nvarchar(14),@ENDMATERIAL nvarchar(14),@BEGINAMOUNT decimal(13,0),@ENDAMOUNT decimal(13,0)',@ACCTSYSID=1,@ACCTORGID=100399,@ACCTPOLICYID=1,@BEGINBILLNO=N'B1WWI2004300011',@ENDBILLNO=N'B1WWI2004300011',@BEGINMATERIAL=N'101010000-0626',@ENDMATERIAL=N'101010000-0626',@BEGINAMOUNT=-9999999999999,@ENDAMOUNT=9999999999999