See the snippet below. Personally I'd prefer to create two procedures - one for update and one for insert and then call them in IF ELSE conditions into the third stored procedure (keep your T-SQL code modular).
Main Topics
Browse All TopicsHello All,
I'm trying to build an update or insert SP, but seem to be getting a little lost with this, I think the SP I'm trying to build below is fairly self explanotory, but let me know if you need more detail.
grateful if you can highlight what I have done wrong, or highlight a better way of doing this.
Many thanks
G
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Sorry, better like this
CREATE PROCEDURE [dbo].[usp_tokendeduction]
@AdvertID smallint,
@SiteID smallint
AS
DECLARE @views int
SELECT @views=SUM([Views]) +1
FROM JobBoards.View
WHERE AdvertID = @AdvertID
IF (@views IS NOT NULL) BEGIN
UPDATE JobBoards.View
SET [Views] = @views
Where AdvertID = @AdvertID
END ELSE BEGIN
INSERT INTO JobBoards.View(AdvertID, SiteID, [Views])
VALUES(@AdvertID, @SiteID, 1)
END
GO
@pcelba: Why would you put a single DML into an explicit transaction? It is a transaction already.
Also, when playing with explcit transactions wouldn't it be better to provide some error handling?
However, you may be right about the table name. I called it JobBoards, cause I can't figure out how one could name a table XXX.View ("view" is a T-SQL keyword and it is reaaaally bad practice to use keywords in object identifiers).
It is not single DML. The SP is testing the row existence first and then decides about UPDATE/INSERT. The transaction should avoid duplicate row inserting in a case when no PK is defined on JobBoards.View yet.
I agree with you about the table naming (but it could be a view) and error handling. Some nondefault transaction isolation level should be defined also.
The PK definition would probably be easier than transaction but again it needs error handling (I know, the possibility of error is very low).
And sorry for jumping in so late - I have been playing with some locking and it caused the delay.
Business Accounts
Answer for Membership
by: pivarPosted on 2009-09-07 at 12:15:23ID: 25276813
Hi,
Is this what you want?
/peter
CREATE PROCEDURE [dbo].[usp_tokendeduction]
@AdvertID smallint,
@SiteID smallint
AS
DECLARE @views int
SELECT @views=SUM([Views]) +1
FROM JobBoards.View
WHERE AdvertID = @AdvertID
IF (@views IS NOT NULL)
Begin
UPDATE JobBoards.View
SET [Views] = @views
Where AdvertID = @AdvertID
Else
INSERT INTO JobBoards.View(AdvertID, SiteID, [Views])
VALUES(@AdvertID, @SiteID, 1)
GO