从具有多个值的另一个表插入表中

时间:2018-06-06 03:18:29

标签: sql sql-server insert

我有一个采购订单明细表,需要来自采购订单收据表的信息。我做完了

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
    )

我想要返回结果的表有这个数据;

enter image description here

如何在值中的一个select语句中添加多个字段?这是有效的,如果我只选择一个值,但不拉多个。

1 个答案:

答案 0 :(得分:2)

尝试将您的查询重新定义为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;