Select within Select in SQL query with VB.net

1

I have 2 tables:

  

-Article

     

-Articulo_Panio

The Article table has, among others, the Stock_Local field (which would be the stock that is physically in the business) and in the table Article_Panio are the items that are inside each cloth (that each seller has).

I need to add a field to the article table called " Stock_General " (the local Stock + the sum of each incurrence found in Article_Panel of the article itself).

I'm doing it in the following way,

    TablaArticulo.Columns.Add("Stock_General", Type.GetType("System.Int32"))
    For Each row As DataRow In TablaArticulo.Rows
        row("Stock_General") = CInt(buscarStockGeneral(row("IdArticulo")) + row("Stock_Local"))
    Next
  

and it works. But it is a very slow process (to the point that the debbugger hangs me (there are 10,000 records) I am sure that doing a SELECT   inside another the loading time would be shortened, but I do not know how to ..

Here's the complete function:

Public Sub LlenaTablaArticulo()
    Try
        conectarse()
        //Consulta SQL de la tabla Articulos con un INNER JOIN a Departamento
        Dim strSql As String = "Select IdArticulo, Codigo, Descripcion, Costo, Ganancia, PrecioFinal, Articulo.IdDepartamento, Departamento.Nombre as departamento, Proveedor, Stock_Local from Articulo inner join Departamento on Articulo.IdDepartamento=Departamento.IdDepartamento"
        Dim adaptador As New OleDb.OleDbDataAdapter(strSql, conexion)
        TablaArticulo = New DataTable
        adaptador.Fill(TablaArticulo)

        //Una vez llenada la TablaArticulo, le agrego el campo Stock_General y por cada fila verifico con la funcion BuscarStockGeneral si hay de ese articulo en los paños
        TablaArticulo.Columns.Add("Stock_General", Type.GetType("System.Int32"))
        For Each row As DataRow In TablaArticulo.Rows
            row("Stock_General") = CInt(buscarStockGeneral(row("IdArticulo")) + row("Stock_Local"))
        Next

        conexion.Close()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

End Sub

This is the SearchStockGeneral function

Function buscarStockGeneral(ByRef IdArticulo As String)
    Dim contador As Integer

    Try
        contador = CType(TablaArticulo_Panio.Compute("SUM(Cantidad)", "IdArticulo=" & IdArticulo), String)
    Catch ex As Exception
        contador = 0
    End Try

    Return contador

End Function

EDIT (FINAL SOLUTION)

So I leave the query using a SELECT inside another that adds the fields found with SUM () , and using the function IIF (ISNULL (), 0, SUM ()) to avoid null fields.

"Select IdArticulo, Codigo, Descripcion, Costo, Ganancia, PrecioFinal, Articulo.IdDepartamento, Departamento.Nombre as departamento, Proveedor, Stock_Local, (Stock_Local + (SELECT IIf(IsNull(sum(Cantidad)),0,sum(Cantidad)) FROM Articulo_Panio WHERE IdArticulo = Articulo.IdArticulo)) as Stock_General from Articulo inner join Departamento on Articulo.IdDepartamento=Departamento.IdDepartamento" 
    
asked by Gonzalo 24.06.2016 в 02:19
source

1 answer

2

Try the following query with her, you should get the total of the sum of the TablaArticulo_Panio table

Select IdArticulo, Codigo, Descripcion, Costo, Ganancia, PrecioFinal, 
Articulo.IdDepartamento, Departamento.Nombre as departamento, Proveedor,Stock_Local, 
(select sum(Cantidad) TablaArticulo_Panio where IdArticulo = a.idArticulo ) as totalArticuloPanio
from Articulo a  inner join Departamento d on a.IdDepartamento = d.IdDepartamento
    
answered by 24.06.2016 / 02:52
source