Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CONDITIONAL UPDATE 1

Status
Not open for further replies.

LonnieJohnson

Programmer
Apr 16, 2001
2,628
US
How do I write an update query that would update certain fields based on the value of other fields? For example I want to do something like this.

UPDATE CUSTOMERS (DIST_CODE, PACKING_NUM)

VALUES

'HERE IS WHERE I AM LOST

IF COUNTY = 'WY' THEN DIST_CODE = A1 AND PACKING_NUM = 8
IF COUNTY = 'SG' THEN DIST_CODE = A2 AND PACKING_NUM = 9
IF COUNTY = 'DG' THEN DIST_CODE = A3 AND PACKING_NUM = 10

I KNOW WHAT I WANT TO DO, JUST CAN GET THE SYNTAX TO AGREE WITH ME

ProDev, MS Access Applications
Visit me at ==> Contact me at ==>lonniejohnson@prodev.us

May God bless you beyond your imagination!!!
 
This is one way, but personally I dont like it, because it scans the whole table.

update Customers Set Dist_Code =
(Select Case County when 'WY' Then 'A1' when 'SG' then 'A2' when 'DG' then 'A3' else Dist_Code end)
,PACKING_NUM =
(Select Case County when 'WY' Then 8 when 'SG' then 9 when 'DG' then 10 else PACKING_NUM end)

I am looking for alternative
 
Does it have to all be in one update query?

Code:
BEGIN TRANSACTION Update_Records

UPDATE CUSTOMERS
  SET DIST_CODE = 'A1',
      PACKING_NUM = 8
  WHERE COUNTY = 'WY'

UPDATE CUSTOMERS
  SET DIST_CODE = 'A2',
      PACKING_NUM = 9
  WHERE COUNTY = 'SG'

UPDATE CUSTOMERS
  SET DIST_CODE = 'A3',
      PACKING_NUM = 10
  WHERE COUNTY = 'DG'

COMMIT TRANSACTION Update_Records

If you have an index on COUNTY, these queries ought to fly.

John
 
No alternative needed. It should be very fast as is. That's not to say that it couldn't be made faster by creating an index on County and possibly creating 3 seperate updates (if the data warrents), but c'mon... You can improve it slightly by eliminating the else clauses.
-Karl
 
john,

I'm liking your method. I had some other stuff going on with a CASE STATEMENT and it was getting complicated. The example I gave you guys was a scaled down version of the real deal. I actually had a little more complex where statement. This is what I did and it whips right through.

Code:
BEGIN TRANSACTION Update_Records

	UPDATE MASTERFILE
	  SET AONBR = 27,
	      AREAOFFICE = 'LAWRENCE'
	  WHERE COUNTY = 'DG' 
	AND 
	(AONBR <> 27 OR AREAOFFICE <> 'LAWRENCE' OR AONBR IS NULL OR AREAOFFICE IS NULL)
	
	UPDATE MASTERFILE
	  SET AONBR = 26,
	      AREAOFFICE = 'KANSAS CITY'
	  WHERE COUNTY = 'WY' 
	AND 
	(AONBR <> 26 OR AREAOFFICE <> 'KANSAS CITY' OR AONBR IS NULL OR AREAOFFICE IS NULL)

COMMIT TRANSACTION Update_Records

I needed this because I am going to move a lot of stuff that gets updated via an access program each night to the SQL Server.

Thanks Again




ProDev, MS Access Applications
Visit me at ==> Contact me at ==>lonniejohnson@prodev.us

May God bless you beyond your imagination!!!
 
UPDATE CUSTOMERS (DIST_CODE, PACKING_NUM)

VALUES

'HERE IS WHERE I AM LOST

IF COUNTY = 'WY' THEN DIST_CODE = A1 AND PACKING_NUM = 8
IF COUNTY = 'SG' THEN DIST_CODE = A2 AND PACKING_NUM = 9
IF COUNTY = 'DG' THEN DIST_CODE = A3 AND PACKING_NUM = 10

Do this:
Update Customers set Dist_Code = A1, Paking_Num =8
where County = WY
Update Customers set Dist_Code = A2, Paking_Num =9
where County = SG
Update Customers set Dist_Code = A3, Paking_Num =10
where County = DG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top