简体   繁体   中英

how to compare the values inside a table in sql

how to compare the values of same table(say for eg: Order table) each and every time the record get inserted , if the record with same values get inserted already in same table i should not insert the new record with same values. how to do that exactly in sql server 2008

If exists(select * from Order where key_column=@some_value)
print 'data already exists'
else
Insert into Order(columns) values (@some_value,...)

I'd suggest adding a unique index on the key columns...

ALTER TABLE mytable ADD UNIQUE INDEX myindex (keycolumn1, keycolumn2, ...);

That'd make it impossible to insert a duplicate by accident.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM