Instagram
youtube
Facebook
Twitter

Add TRY...CATCH for Payment Insert Errors

Add TRY...CATCH for Payment Insert Errors

Payments Table:


Query Description:

This script inserts a new payment record using a TRY...CATCH block. If the insert is successful, it prints a success message. If an error occurs (e.g., data type mismatch), it catches and displays the error message. The issue was fixed by ensuring GETDATE() is properly cast to the DATE type to match the PaymentDate column.

SQL Query:

BEGIN TRY
    INSERT INTO Payments (PaymentID, CustomerID, Amount, Status, PaymentDate)
    VALUES (101, 2001, 1500.00, 'Completed', CAST(GETDATE() AS DATE));

    PRINT 'Payment inserted successfully.';
END TRY
BEGIN CATCH
    PRINT 'Error inserting payment:';
    PRINT ERROR_MESSAGE();
END CATCH;


Output: