Solved

Evaluating values

Posted on 2012-03-23
4
240 Views
Last Modified: 2012-06-27
@ProdCode		VARCHAR(10),
@Qty			INT,
@RequestedBy	VARCHAR(15),
@Site			VARCHAR(20),
@WHouse		VARCHAR(15),
@Aisle		VARCHAR(10),
@Bay			VARCHAR(10),
@PallettNo		VARCHAR(10),
@InOut		VARCHAR(5),
@Comments		VARCHAR(255)

AS

DECLARE @Rowcount INT
	
SET NOCOUNT ON;

INSERT INTO Warehouse.Movements
(ProdCode, Qty, MoveDate, RequestedBy, [Site],WHouse,Aisle,Bay, PallettNo, InOut,
Comments)
VALUES(@ProdCode,@Qty,GETDATE(), @RequestedBy,@Site,@WHouse,@Aisle,@Bay, @PallettNo,
@InOut,@Comments)

UPDATE Warehouse.Products
SET Warehouse.Products.Quantity = Warehouse.Products.Quantity + @Qty
WHERE Warehouse.Products.ProdCode = @Prodcode

UPDATE Warehouse.Locations
SET Warehouse.Locations.Qty = Warehouse.Locations.Qty + @Qty
WHERE ProdCode = @ProdCode
AND [Site] = @Site
AND WHouse = @WHouse
AND Aisle = @Aisle
AND Bay = @Bay
AND PallettNo = @PallettNo

Open in new window


Hi,

I use the code above to insert into one table and update 2 others.  Before the second update, I would like to be able to check for existing rows in the Locations table. If a row does not exist, to be able to insert it. Otherwise update it.
So if Prodcode <> @Prodcode
AND Site <> @Site
AND Warehouse <> @WHouse
AND Aisle <>@aisle
AND Bay <> @Bay
AND PallettNo <> @PalletNo
If all of these result in no match, then I would like to Insert the details. If a match is found, to update accordingly.

Is there a way to do this in my SP. Any guidance would be appreciated.
Thanks
0
Comment
Question by:Zbiebu
  • 2
  • 2
4 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 37757640
Easiest is to just do the UPDATE, and if no row was found, then do the INSERT:

UPDATE Warehouse.Locations
SET Warehouse.Locations.Qty = Warehouse.Locations.Qty + @Qty
WHERE ProdCode = @ProdCode
AND [Site] = @Site
AND WHouse = @WHouse
AND Aisle = @Aisle
AND Bay = @Bay
AND PallettNo = @PallettNo

IF @@ROWCOUNT = 0
    INSERT INTO Warehouse.Locations (...)
    VALUES(...)



If @@ROWCOUNT is 0, that means no row was found to UPDATE; that is, the row did not exist, so you INSERT it.
0
 

Author Closing Comment

by:Zbiebu
ID: 37757757
That's great. Thanks
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37757787
You're welcome.

NOTE though:

The check of @@ROWCOUNT must come IMMEDIATELY after the UPDATE; NO other statements can occur in between.

For example, this would not be valid:

UPDATE Warehouse.Locations
SET Warehouse.Locations.Qty = Warehouse.Locations.Qty + @Qty
WHERE ProdCode = @ProdCode
AND [Site] = @Site
AND WHouse = @WHouse
AND Aisle = @Aisle
AND Bay = @Bay
AND PallettNo = @PallettNo

SET @error = @@ERROR --<<-- resets @@ROWCOUNT, UPDATE value is now LOST

IF @@ROWCOUNT = 0 --<<-- will always be 1 because of the SET
    INSERT INTO Warehouse.Locations (...)
    VALUES(...)


So, think of the IF @@ROWCOUNT as being directly paired with UPDATE.
0
 

Author Comment

by:Zbiebu
ID: 37757821
Well noted. Thanks again.
0

Featured Post

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Following an example - removing duplicate strings 4 67
Query to Add Late Tolerance 10 82
Help Required 2 46
SQL Server 2005 database messed up. Can it be fixed? 4 37
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

679 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