Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Verify Uniqueness of row inserts

Status
Not open for further replies.
Jun 9, 2006
159
US
would like to verify that the record I'm going to insert is going to unique based on selected columns.

Here is my query:


Code:
INSERT into stats
 (statcode,dateandtime,userid,friendid) 
values 
  (@statcode,@dateandtime,@userid,@friendid)

I want to insert only as long as there is not a row that already contains this duplicate

- userid
- friendid
- statcode

unless that friendId is not = -1, as that is my null marker

Just for refence this is the code I'm calling this procdue from (c#)


Code:
public static void AddStat(int userId, StatType statType)
{
... //do insert
}

public enum StatType
    {
        Login = 0,
        View = 1,
     }

I'm tryign to build a stats engine and want to only record unique hits. What is the best way to accomplish this is SQL?

Thanks!!!


Shawn Molloy
Seattle, WA
 
What database engine are you using?

One method would be to create a unique index on the userid, friendid and statcode columns.

Denny
MCSA (2003) / MCDBA (SQL 2000) / MCTS (SQL 2005) / MCITP Database Administrator (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top