SQL Server Performance

Prevent duplicate value to insert into a table

Discussion in 'General Developer Questions' started by subrata.bauri, Apr 22, 2011.

  1. subrata.bauri New Member

    Dear Sir,
    I have written a INSERT Trigger to insert records from Table1(Col1,Col2) to Table2 (Col1,Col2). I want to prevent duplicate value to insert into the column Col1 in the Table2. How Can I do this ?
    Thanks.
  2. Adriaan New Member

    Use DISTINCT, and use NOT EXISTS correlated subquery. Search Books Online for the
  3. subrata.bauri New Member

    I want to do this thru Code. I want to check that a.no_of_item is already exist in the column no_of_item of the pur_gi_header table.

    Original Code given below
    CREATE trigger [sub_contracting_order]
    on
    dbo.pur_gi_header
    for insert
    as

    BEGIN
    If EXISTS(select 'X' from inserted a where a.po_category = 'SER' AND a.no_of_item = '0' )

    BEGIN
    EXEC pur_msg_rtr '02999200052', 'For Service Orders, Machine No is Mandatory' , 'I'

    END
    END

Share This Page