Solved

Insert Statement

Posted on 2009-07-09
6
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 60

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 60

Expert Comment

by:Kevin Cross
ID: 24816839
Agree with Aneesh's suggestion as well.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 60

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

739 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