简体   繁体   中英

Insert into table from another table with multiple values

I have a Purchase order detail table which needs information from Purchase order Receipt table. I have done

INSERT INTO CRD_InvoiceDetails
    (
     TranID
    ,Ln
    ,Line_Type
    ,Description
    ,Price
    ,Price_inclGST
    ,Quantity
    ,Line_Total
    ,GST_code
    ,GST_percent
    ,GST_Amount
    ,gl_code
    ,CRD_itemadjusted
    ,CRD_plantno
    ,PO_Number
    )
VALUES  (
     @CRDTranID
    ,(select PO_Line from PO_OrderDetails where PO_Number = @PO)
    ,'R'
    ,(select Description from PO_OrderDetails where PO_Number = @PO)
    ,(select unit_cost from PO_OrderDetails where PO_Number = @PO)
    ,(select PriceIncl from PO_OrderDetails where PO_Number = @PO)
    ,(select qty_ord from PO_OrderDetails where PO_Number = @PO)
    ,(select PriceIncl from PO_OrderDetails where PO_Number = @PO)
    ,(select GST_code from PO_OrderDetails where PO_Number = @PO)
    ,@GSTPercent
    ,(select GST_value from PO_OrderDetails where PO_Number = @PO)
    ,@PO
    ,@AccCode
    ,NULL
    ,NULL
    )

The table I want to return results from has this data;

enter image description here

how do I add multiple fields from one select statement in values? this works if I only choose one value, but does not pull multiple.

Try rephrasing your query as an INSERT INTO ... SELECT :

INSERT INTO CRD_InvoiceDetails (
    TranID,
    Ln,
    Line_Type,
    Description,
    Price,
    Price_inclGST,
    Quantity,
    Line_Total,
    GST_code,
    GST_percent,
    GST_Amount,
    gl_code,
    CRD_itemadjusted,
    CRD_plantno,
    PO_Number
)
SELECT
    @CRDTranID,
    PO_Line,
    'R',
    Description,
    unit_cost,
    PriceIncl,
    qty_ord,
    PriceIncl,
    GST_code,
    @GSTPercent,
    GST_value,
    @PO,
    @AccCode,
    NULL,
    NULL
FROM PO_OrderDetails
WHERE PO_Number = @PO;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM