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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

only insert if record doesn't exist 1

Status
Not open for further replies.

gojohnnygogogogo

Programmer
May 22, 2002
161
GB
hello,

I have created an sp to insert a record, but how do I only insert records if they don't exist in the database. ?

CREATE PROCEDURE [sp_InsertLog]

@username varchar(50),
@link varchar(100),
@Param1 varchar(50),
@Param2 varchar(50),
@SearchType varchar(50)

AS

INSERT INTO tblLogs(Username, DateAdded, Link, Param1, Param2, SearchType)
Values (@username, GetDate(), @link, @Param1, @Param2, @SearchType)
GO


so
IF username <> @username AND Param1 <> @param1 AND Param2 <> @Param2 AND SearchType<> @SearchType THEN

run sp.

ELSE
don't run sp.

I think thats thew way to do it, but I don't know how to tpye it into an sp so that it will work.

cheers all.
 
Try This:
Code:
DECLARE @numfound INT

SELECT @numFound = count(*) FROM tblLogs 
WHERE userName = @userName
Code:
AND DATEDIFF (d,dateAdded, getDate) < 1
Code:
AND param1 = @param1
AND param2 = @param2
AND searchType = @searchType

IF @numFound > 0 INSERT INTO tblLogs(Username, DateAdded, Link, Param1, Param2, SearchType)
Values (@username, GetDate(), @link, @Param1, @Param2, @SearchType)

The code in red is optional (if comparing dates also)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top