Solved

Insert Statement

Posted on 2009-07-09
6
219 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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