Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Insert Statement

Posted on 2009-07-09
6
Medium Priority
?
222 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 800 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 800 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

618 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