|
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 (
(comments are locked)
|
|
If you go with Melvyn's solution and you want access to the INSERTED and DELETED tables, you will soon find that these are not available to any stored procedure that you write. To get around this problem, you can populate a #table with the results of INSERTED (or DELETED). Your stored procedure can be designed to execute with the prerequisite that this #table exists. Alternatively, I'd keep it simple. Write an UPDATE followed by an INSERT based on JOINs to the magic "INSERTED" and "DELETED" tables. This model works well even when multi-row INSERT/DELETES are issued. E.g...
(comments are locked)
|
|
I think I would wrap the access in a stored procedure. It would then be quite easy to set the logic out so that it is nice and clear.
(comments are locked)
|
|
If you're using SQL Server 2008 then I would recommend using MERGE in a trigger, otherwise your If Exists Update Else Insert would be the way to go. This is assuming, of course, that you can't take Melvyn's suggestion for some reason, which would be the ideal scenario.
(comments are locked)
|

