Solved

Conditional Stored procedures

Posted on 2009-06-30
7
217 Views
Last Modified: 2012-05-07
I am looking for a way to insert data into a table but if there is already a record there with the same employeeid or userid, I want it to be an update.  How would I do this in a stored procedure?  Thank you for your help ahead of time.
0
Comment
Question by:VBBRett
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24746047

create procedure testSp
@EmployeeID int
AS
BEGIN
SET NOCOUNT ON
INSERT INTO urTable  (EmployeeID)
SELECT @EmployeeID
WHERE NOT EXISTS (SELECT 1 FROM urTable WHERE EmployeeID = @EmployeeID )
IF @@ROWCOUNT = 0
  UPDATE urTable
 SET ColumnName = Value
 WHERE EmployeeID = @EmployeeID
END
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24746081
here you have a comparison of the 2 possible methods:
http://weblogs.sqlteam.com/mladenp/archive/2007/07/30/60273.aspx

I usually go with the  UPDATE + INSERT, and not with the EXISTS method...
0
 

Author Comment

by:VBBRett
ID: 24746274
angelll, can you give me an example of the update + insert conditional statement?
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24746410
you did not look into the link, did you?
the code is there.
0
 

Author Comment

by:VBBRett
ID: 24746549
Here is my insert statement.  How would I change this to a conditional insert statement, or update if there is a record?  See the code below:  Thanks!
USE [EmployeeS2]
GO
/****** Object:  StoredProcedure [dbo].[EmployeeS2Answers]    Script Date: 06/30/2009 11:48:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Me>
-- Create date: <06/25/09>
-- Description:	<This procedure enters question 
--information into EmployeeS2 Table in the
-- Employee database
-- =============================================
ALTER PROCEDURE [dbo].[EmployeeScreeningAnswers]
		@EmployeeId		int,
		@Question1		bit,
		@Question2		bit,
		@Question3		bit,
		@Question4		bit,
		@Question5		bit,
		@Question6		bit,	
		@Question7		bit,
		@Question8		bit,
		@Question9		bit,
		@Question10		bit,
		@Question11		bit,
		@Question12		bit,
		@Question13		bit,
		@Question14		bit,
		@Question15		bit,	
		@Question16		bit,		
		@LastUpdate		datetime,
		@LastUpdateBy	nvarchar
			
AS
BEGIN
 
	INSERT INTO [dbo].[EmployeeS2](EmployeeId, Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9,
Q10, Q11, Q12, Q13, Q14, Q15, Q16, LastUpdate, LastUpdateBy)
VALUES (@EmployeeId, @Question1, @Question2, @Question3, @Question4, @Question5, @Question6,
@Question7, @Question8, @Question9, @Question10, @Question11, @Question12, @Question13, 
@Question14, @Question15, @Question16, @LastUpdate, @LastUpdateBy)
 
END

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24746630
presuming employeeid is the primary key...
ALTER PROCEDURE [dbo].[EmployeeScreeningAnswers]
		@EmployeeId		int,
		@Question1		bit,
		@Question2		bit,
		@Question3		bit,
		@Question4		bit,
		@Question5		bit,
		@Question6		bit,	
		@Question7		bit,
		@Question8		bit,
		@Question9		bit,
		@Question10		bit,
		@Question11		bit,
		@Question12		bit,
		@Question13		bit,
		@Question14		bit,
		@Question15		bit,	
		@Question16		bit,		
		@LastUpdate		datetime,
		@LastUpdateBy	nvarchar
			
AS
BEGIN
UPDATE [dbo].[EmployeeS2]
     SET Question1 = @Question1
       , Question2 = @Question2
       ... etc ...
  WHERE EmployeeId = @EmployeeId
IF @@ROWCOUNT = 0
BEGIN 
	INSERT INTO [dbo].[EmployeeS2](EmployeeId, Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9,
Q10, Q11, Q12, Q13, Q14, Q15, Q16, LastUpdate, LastUpdateBy)
VALUES (@EmployeeId, @Question1, @Question2, @Question3, @Question4, @Question5, @Question6,
@Question7, @Question8, @Question9, @Question10, @Question11, @Question12, @Question13, 
@Question14, @Question15, @Question16, @LastUpdate, @LastUpdateBy)
END
END

Open in new window

0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 24751251
something like this

USE [EmployeeS2]
GO
/****** Object:  StoredProcedure [dbo].[EmployeeS2Answers]    Script Date: 06/30/2009 11:48:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Me>
-- Create date: <06/25/09>
-- Description:	<This procedure enters question 
--information into EmployeeS2 Table in the
-- Employee database
-- =============================================
ALTER PROCEDURE [dbo].[EmployeeScreeningAnswers]
		@EmployeeId		int,
		@Question1		bit,
		@Question2		bit,
		@Question3		bit,
		@Question4		bit,
		@Question5		bit,
		@Question6		bit,	
		@Question7		bit,
		@Question8		bit,
		@Question9		bit,
		@Question10		bit,
		@Question11		bit,
		@Question12		bit,
		@Question13		bit,
		@Question14		bit,
		@Question15		bit,	
		@Question16		bit,		
		@LastUpdate		datetime,
		@LastUpdateBy	nvarchar
			
AS
BEGIN
SET NOCOUNT 
INSERT INTO [dbo].[EmployeeS2](EmployeeId, Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9,
				Q10, Q11, Q12, Q13, Q14, Q15, Q16, LastUpdate, LastUpdateBy)
SELECT @EmployeeId, @Question1, @Question2, @Question3, @Question4, @Question5, @Question6,
		@Question7, @Question8, @Question9, @Question10, @Question11, @Question12, @Question13, 
		@Question14, @Question15, @Question16, @LastUpdate, @LastUpdateBy
WHERE NOT EXISTS (SELECT 1 FROM [dbo].[EmployeeS2] WHERE EmployeeID = @EmployeeId )
IF @@ROWCOUNT = 0 
	UPDATE [dbo].[EmployeeS2]
	SET Q1 = @Question1, 
	    Q2 = @Question2,...
	EmployeeID = @EmployeeId		
 
END 

Open in new window

0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

626 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