用一个提交插入多张表

时间:2018-09-13 11:05:17

标签: php sql-server

我有两个要同时提交的表,两个表都有唯一的AI ID。我将表A中的主键用作表B中的外键。有没有办法让我提交时可以从表A中获得新创建的ID,并在表B中将其用作外键?

我已经尝试过了,但是没有插入到第二个(结果)表中,

$result = sqlsrv_query($conn, "
        DECLARE @OutputTbl TABLE (ID INT)
        INSERT INTO Parts (PartNumber, Description, Manufacturer, TestDate, VendorDescription, ReceiptNo)
        OUTPUT INSERTED.PartID INTO @OutputTbl(ID)
        VALUES
        ('".$_POST['PartNumberSubmit']."','".$_POST['DescriptionSubmit']."','".$_POST['ManufacturerSubmit']."','".$_POST['TestDateSubmit']."','".$_POST['VendorDescriptionSubmit']."','".$_POST['ReceiptNoSubmit']."')
        DECLARE @Id INT
        SET @Id = (SELECT Id from @OutputTbl)
        INSERT INTO Results (Test, Result, PartID)
        VALUES ('Some', 'Thing', @Id)");

0 个答案:

没有答案