I'm trying to insert some mock payment info into a dev database with this query:
INSERT
INTO
Payments(Amount)
VALUES(12.33)
WHERE
Payments.CustomerID = '145300';
How can adjust this to execute? I also tried something like this:
IF NOT EXISTS(
SELECT
1
FROM
Payments
WHERE
Payments.CustomerID = '145300'
) INSERT
INTO
Payments(Amount)
VALUES(12.33);
8条答案
按热度按时间bbuxkriu1#
I think you are trying to do an update statement (set amount = 12.33 for customer with ID = 145300)
Else if you are trying to insert a new row then you have to use
Or if you want to combine both command (if customer exists do update else insert new row)
nkcskrwz2#
It sounds like having the customerID already set. In that case you should use an update statement to update a row. Insert statements will add a completely new row which can not contain a value.
dgsult0t3#
If you want to insert new rows with the given CustomerID
else if you already have payment for the customer you can do:
yv5phkfx4#
Do you want to perform update;
hjqgdpho5#
Better solution and without risk of deadlocks:
eiee3dmh6#
i do inserts into a table if the record doesn't exist this way. may not be entirely what is after but it may be helpful
8i9zcol27#
Ok, looks like I actually need to just do an insert into the Payments table that has the correct CustomerID, as there are currently no Payments with that CustomerID, so I cannot update it.
I ran
INSERT INTO Payments (CustomerID, Amount, PaymentTypeID) Values ('145300', 24.99, 8);
and thenSELECT * FROM Payments WHERE Payments.CustomerID = '145300';
to confirm and we're in business. Thanks everyone!hfyxw5xn8#
Try to update as below if you want to modify existing row,