I have the following composable dml and I cant seem to set it working. Could someone help me point out where I’ve gone wrong?
INSERT INTO order_kit (kit_no, order_id)
SELECT
X.kit_no, 10089
FROM
(
MERGE INTO kit AS tgt
USING
(SELECT TOP 2 kit_no
FROM kit rk, batch rb
WHERE rk.study_id = 35 AND
rk.batch_id = rb.batch_id AND
rb.depot_id = 16 AND
rk.treatment = 1 AND
rk.status = 1 AND
rb.status = 1 AND
rb.expiry > CAST(FLOOR(CAST(getdate() AS float)) AS datetime)
) AS src ON tgt.kit_no = src.kit_no
WHEN MATCHED THEN
UPDATE SET status = 6 OUTPUT $action as action, kit_no) AS X
Invalid column name ‘kit_no’. (*on the last line)
The target table ‘order_kit’ of the INSERT statement cannot be on either side of a (primary key, foreign key) relationship when the FROM clause contains a nested INSERT, UPDATE, DELETE, or MERGE statement. Found reference constraint ‘FK_order_kit_order’.
i managed to resolved this by using