Solved

Insert Statement

Posted on 2009-07-09
6
212 Views
Last Modified: 2012-05-07
I'm trying to create a stored procedure that inserts "UserID" and "ReportID" into the "UserReports" table. IF the UserID and ReportID combination already exists then don't insert it.

Something like:
If not Exists @UserID +@ReportID then
 (
    Insert @UserID into the UserID column,
          @ReportID into the ReportID column,
)
else end

use UserReports table.
0
Comment
Question by:mattkovo
6 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 200 total points
ID: 24816781
You can try like this:
IF NOT EXISTS (SELECT 1 FROM UserReports WHERE UserID = @UserID AND ReportID = @ReportID)

BEGIN

INSERT INTO UserReports(UserID, ReportID) VALUES(@UserID, @ReportID)

END

Open in new window

0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 200 total points
ID: 24816783
CREATE PROC dbo.urProcName
@UserID INT,
@ReportID INT
AS
BEGIN
      SET NOCOUNT ON
      INSERT INTO UserReports(UserID, ReportID)
      SELECT @UserID , @ReportID
      WHERE NOT EXISTS (SELECT 1 FROM  UserReports WHERE UserID=@UserID  AND ReportID = @ReportID)
END      
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24816839
Agree with Aneesh's suggestion as well.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24816862
And using Aneesh's structure for procedure if you didn't already have that, here is the way using IF, but again Aneesh's works fine.
CREATE PROC dbo.urProcName 

@UserID INT, 

@ReportID INT 

AS 

BEGIN

      SET NOCOUNT ON;
 

      IF NOT EXISTS (SELECT 1 FROM UserReports WHERE UserID = @UserID AND ReportID = @ReportID)

          INSERT INTO UserReports(UserID, ReportID) VALUES(@UserID, @ReportID);

END   

Open in new window

0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 100 total points
ID: 24816944
Using "if not exist insert select" is less reliable than using "insert where not exists" because the first will execute as two statements and the second will execute as one.  I have run into problems where the record does not exists when the "if not exists" runs for two processes at exactly the same time but then the inserts both run.  This is especially a problem in high usage systems where lots of concurrent write operations are happening.

That is why I would use http:#a24816783
0
 

Author Closing Comment

by:mattkovo
ID: 31601764
Thanks guys!  I was originally going with mwvisa1 but decied to go with aneeshattingal:'s solution because of what BrandonGalderisi's statement. So I split up the points for everyone.  Thanks again!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now