SQLTeam.com | Weblogs | Forums

Finding Duplicate Values

Hi,

I'm just starting my SQL journey, it's all very foreign so be kind.

In our accounting system there is an events module that triggers particular rules. I'm attempting to create a trigger where when a purchase order is entered with the same @custorderno (po number) it triggers the alert.

any help would be very much appreciated.

SELECT @CUSTORDERNO
GROUP BY @CUSTORDERNO
HAVING COUNT (*) > 1

The following assumes that you have a table named YourCustomerOrdersTable and that there is a column in that table named CustOrderNo which has all the already existing CustOrderNo.

IF EXISTS
(
    SELECT
        *
    FROM
        YourCustomerOrdersTable
    WHERE
        CustOrderNo = @CustOrderNo
)
BEGIN
    -- do what you need to do here.
END