Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Update, or insert only when necessary

Author  Topic 

overbored
Starting Member

12 Posts

Posted - 2002-07-15 : 19:04:36
Hi, I'm new to SQL, and I'm wondering how to write this stored procedure. Let's say I have a table called Books with fields:

- b_id
- b_title
- b_year

and that I only want this book collection to contain the latest editions of each book. That means for this procedure, I'll want to INSERT all new book titles, but UPDATE whenever the title is already there.

Any help would be appreciated, thanks a lot.

JustinBigelow
SQL Gigolo

1157 Posts

Posted - 2002-07-15 : 20:15:41
Use an if then statement to determine whether or not you should issue an Update statement or an Insert statement. I'm doing the samething (pretty much) with on a site that uses a shopping cart.


CREATE proc sprInsertIntoCart
@CartID uniqueidentifier,
@ProdID varchar(15),
@Quantity int
as
set nocount on
if exists(select prodid from cartitems where cartid like @CartID and ProdID like @ProdID)
Begin
update CartItems
set Quantity = Quantity + @Quantity
where ProdID like @ProdID
and CartID like @CartID
End
else
insert into CartItems (CartID, ProdID, Quantity)
values (@CartID, @ProdID, @Quantity)


hth,
Justin

Go to Top of Page
   

- Advertisement -