leonardo-m
5/29/2018 - 4:38 PM

Uniion example

Tquery Union Example

public DataSet Consulta_NoSinc()
        {

             DataSet _Data = new DataSet();
            
             ObjRespuesta Respuesta;
             Respuesta = Globales.Conectarse();
             if (Respuesta.exito)
             {
                 TTransaction _Transaccion = TConexionSAP .CreaTransaccion();
                 TQuery _Query = new TQuery(_Transaccion.Conexion);
                 TUnion _Union = new TUnion(_Transaccion.Conexion);
                 SAPbobsCOM.Recordset recset;
                     recset = (SAPbobsCOM.Recordset)TConexionSAP.Compania.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
                 
                 TConjunto _Conjunto = new TConjunto("'OINV','OINVDN'");

                 

                 TJoin LeftJoin = new TJoin(TTipoJoin.Left, "@LDT_GTIR", "GTI");
                 LeftJoin.Where(new TField("GTI", "U_LDT_DocEntry"), OperadoresFiltros.Igual, new TField("DOC", "DocEntry"));
                 LeftJoin.Where(new TField("GTI", "U_LDT_TipoDoc"), OperadoresFiltros.En, _Conjunto);

                 TJoin LeftJoin2 = new TJoin(TTipoJoin.Left, "@LDT_GTIR", "GTI2");
                 LeftJoin2.Where(new TField("GTI2", "U_LDT_DocEntry"), OperadoresFiltros.Igual, new TField("ORN2", "DocEntry"));
                 LeftJoin2.Where(new TField("GTI2", "U_LDT_TipoDoc"), OperadoresFiltros.En, new TValue("'ORIN'"));

                 _Query.Selected(new TField("DocEntry"));
                 _Query.Selected(new TField("DocNum"));
                 _Query.Selected(new TField("DocDate"));
                 _Query.Selected(new TField("CardCode"));
                 _Query.Selected(new TValue("OINV", "TipoDoc"));
                 _Query.Selected(new TField("CardName"));
                 _Query.Selected(new TField("NumAtCard"));
                 _Query.Selected(new TField("DocTotal"));
                 _Query.Selected(new TField("GTI", "U_LDT_NumeroGTI"));
                 _Query.Selected(new TField("GTI", "U_LDT_ClaveNumericaGTI"));
                 _Query.Selected(new TField("GTI", "U_LDT_GTIMSG"));
                 _Query.Selected(new TField("U_LDT_FechaSinc"));
                 _Query.Selected(new TField("U_LDT_Estado"));
                 _Query.Selected(new TField("U_LDT_MsgError"));
                 _Query.Selected(new TField("U_LDT_IdCarga"));
                 _Query.Selected(new TField("GTI", "U_LDT_TipoDoc"));
                 _Query.From(new TFrom("OINV", "DOC"));
                 _Query.From(LeftJoin);
                 _Query.Where(new TField("GTI", "U_LDT_NumeroGTI"), OperadoresFiltros.Es_Nulo, new TValue("NULL"));


                 TQuery _Query2 = new TQuery(_Transaccion.Conexion);
                 _Query2.Selected(new TField("DocEntry"));
                 _Query2.Selected(new TField("DocNum"));
                 _Query2.Selected(new TField("DocDate"));
                 _Query2.Selected(new TField("CardCode"));
                 _Query2.Selected(new TValue("OINV", "TipoDoc"));
                 _Query2.Selected(new TField("CardName"));
                 _Query2.Selected(new TField("NumAtCard"));
                 _Query2.Selected(new TField("DocTotal"));
                 _Query2.Selected(new TField("GTI2", "U_LDT_NumeroGTI"));
                 _Query2.Selected(new TField("GTI2", "U_LDT_ClaveNumericaGTI"));
                 _Query2.Selected(new TField("GTI2", "U_LDT_GTIMSG"));
                 _Query2.Selected(new TField("U_LDT_FechaSinc"));
                 _Query2.Selected(new TField("U_LDT_Estado"));
                 _Query2.Selected(new TField("U_LDT_MsgError"));
                 _Query2.Selected(new TField("U_LDT_IdCarga"));
                 _Query2.Selected(new TField("GTI2", "U_LDT_TipoDoc"));
                 _Query2.From(new TFrom("ORIN", "ORN2"));
                 _Query2.From(LeftJoin2);
                 _Query2.Where(new TField("GTI2", "U_LDT_NumeroGTI"), OperadoresFiltros.Es_Nulo, new TValue("NULL"));

                 _Union.AddSql(_Query);
                 _Union.AddSql(_Query2);
                 _Union.Aplicar_Union_All = true;
                 if (_Union.Open())
                 {
                     recset.DoQuery(_Union.SQL);
                     if (recset.RecordCount > 0)
                     {

                         while (!recset.EoF)
                         {
                             string TipoDoc = (string)recset.Fields.Item("TipoDoc").Value;
                             string Llave = (string)recset.Fields.Item("DocEntry").Value.ToString();



                             recset.MoveNext();
                         }


                     }
                 }

                 return _Data;

             }
             else
             {
                 throw new Exception("No se pudo conectar :" + Respuesta.MsgError);
             }