How to do it in Trigger ?

Hello gentlemen,Ladies

I have two tables Quantity and Purchases. When I purchase a product, first I have to check either it's old product or new. If it's Old means I have purchased before and its details already saved in the Database in Quantity Table so it will update the AvailableQuantity field only in the Quantity Table. Else if it's a new Product it must add new row in the Quantity Table with all details of the product.

I hope it's clear for all

I need your comment. How to do it with Trigger ( IF Exist Update else Insert ) or give me another solution.

more ▼

asked Oct 21, 2009 at 11:02 AM in Default

Mohamed gravatar image

34 3 3 3

Which version of SQL Server are you dealing with? There are better options available in the newer versions.
Oct 21, 2009 at 11:09 AM Benjamin
you have to provide more information like table DDL, sample data and required result
Oct 22, 2009 at 12:50 AM Squirrel 1
(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

Something like this. Use a procedure instead of a trigger

If exists(select * from products where product_name=@product_name)
Update Quantity
set ....
where product_name=@product_name
insert into products(columns)
select @columns
more ▼

answered Oct 22, 2009 at 03:57 AM

Madhivanan gravatar image

1.1k 1 3 6

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Oct 21, 2009 at 11:02 AM

Seen: 542 times

Last Updated: Oct 21, 2009 at 11:02 AM