如何从存储过程内的临时表中填充数据表

时间:2014-08-22 14:17:01

标签: sql datatable temporary

我需要从存储过程中的临时表中填充数据表。问题是数据表没有填满。

如果删除临时表,则正确填充数据表

我的查询:

alter procedure sp_cop_prueba
as
begin
    SELECT PP.* 
    INTO #AUX_01
    FROM
       (SELECT 
           t1.DocEntry , 
           t1.docnum , 
           t1.folionum , 
           CASE
              WHEN T5.DistNumber IS NULL 
                THEN CASE
                       WHEN t1.U_FLEX_ENC01 IS NULL
                            OR t1.U_FLEX_ENC01 = '' 
                         THEN ISNULL((SELECT TOP 1 R5.DistNumber AS 'OP'
                                      FROM vyg.dbo.ODLN R1 
                                      INNER JOIN vyg.dbo.DLN1 R2 ON R1.DocEntry = R2.DocEntry
                                      LEFT JOIN (vyg.dbo.OITL R3 
                                      INNER JOIN vyg.dbo.ITL1 R4 ON R3.LogEntry = R4.LogEntry
                                      INNER JOIN vyg.dbo.OBTN R5 ON R5.SysNumber = R4.SysNumber
                                                                 AND R5.ItemCode = R3.ItemCode)
                                      ON R3.DocType = 15
                                         AND R3.DocEntry = R1.DocEntry
                                         AND R4.ItemCode = R2.ItemCode
                                      WHERE R1.DocType <> 'S'
                                        AND R2.BASETYPE = 13
                                        AND R2.ItemCode = t2.ItemCode
                                        AND R2.BaseRef = T1.DOCNUM) , '')
                         ELSE t1.U_FLEX_ENC01
                      END
              ELSE T5.DistNumber
           END AS 'OP' , 
           t2.ItemCode , 
           t2.Quantity AS 'CANT' , 
           YEAR(t1.DocDate) AS 'año' , 
           MONTH(t1.DocDate) AS 'mes' , 
           DAY(T1.DocDate) AS 'dia' , 
           CASE t2.Quantity
              WHEN 0 THEN 0
                  ELSE t2.LineTotal / t2.Quantity
           END AS 'venta_UNT' , 
           t2.StockPrice AS 'COST_STOCK' , 
           CASE
              WHEN t2.BaseType = 15 THEN 'FG'
                  ELSE CASE
                       WHEN T5.DistNumber IS NULL THEN 'FA'
                           ELSE 'FD'
                       END
           END AS 'TIPO'
        FROM
           vyg.dbo.oinv t1 
        INNER JOIN 
           vyg.dbo.INV1 t2 ON t1.DocEntry = t2.DocEntry --and  t2.BaseType<>15
        LEFT JOIN
           (vyg.dbo.OITL T3 
            INNER JOIN vyg.dbo.ITL1 T4 ON T3.LogEntry = T4.LogEntry
            INNER JOIN vyg.dbo.OBTN T5 ON T5.SysNumber = T4.SysNumber
                                       AND T5.ItemCode = T3.ItemCode)
          ON T3.DocType = CASE t2.BaseType
                             WHEN 15 THEN 15
                             WHEN 17 THEN 13
                          END
             AND T3.DocEntry = CASE t2.BaseType
                                  WHEN 15 THEN t2.BaseDocNum
                                  WHEN 17 THEN t1.DocEntry
                               END
             AND T4.ItemCode = t2.ItemCode
         WHERE 
            T1.DocType <> 'S' --and t2.BaseType<>15--and T5.DistNumber IN (SELECT OP FROM #LISTA_OP)
            --and YEAR(t1.DocDate)=@año and MONTH(t1.DocDate)=@mes
       ) PP;

   select * FROM #AUX_01

   DROP TABLE #AUX_01;
end

Public Function Detalle() As DataTable
    Dim sp As String = "sp_cop_prueba"
    Dim dt As DataTable = New DataTable()

    Dim cmd As OleDb.OleDbCommand = coneccion.objconexion.CreateCommand()
    cmd.CommandText = sp
    cmd.CommandType = CommandType.StoredProcedure

    Dim adap As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
    adap.SelectCommand = cmd

    adap.Fill(dt)

    Return dt
End Function

1 个答案:

答案 0 :(得分:1)

.. 开始     设置NOCOUNT     选择PP。* ..

并删除drop table line。