Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Update a field multiple times, but only if the field is empty

$
0
0

I am having trouble updating a field in my table based upon a set of 7 different rules. I have been scratching my head at this for a few days, and am not sure what exactly is wrong, but am sure there is something small causing me problems. My current code is:

BEGIN TRANSACTION
UPDATE MyTable	
		SET Field1 = 'Value1'
		WHERE svc = 'Y' AND Field1 = '' ;
COMMIT TRANSACTION
BEGIN TRANSACTION
UPDATE MyTable	
		SET Field1 = 'Value2'
		WHERE Dlv Is Null AND Cat = 'svc' AND Field1 = '';
COMMIT TRANSACTION
BEGIN TRANSACTION
UPDATE MyTable
		SET Field1 = 'Value2'
		WHERE (prod LIKE 'ABC%') OR (prod LIKE 'ABB%') AND Field1 = '';
COMMIT TRANSACTION
BEGIN TRANSACTION
UPDATE MyTable
		SET Field1 = 'Value3'
		WHERE Dlv = 'Elec' AND Field1 = '';
COMMIT TRANSACTION
BEGIN TRANSACTION
UPDATE MyTable
		SET Field1 = 'Value4'
		WHERE Ord = '0' AND Cat = 'Hdw' OR Cat = 'Bnd' AND Field1 = '';
COMMIT TRANSACTION
BEGIN TRANSACTION
UPDATE MyTable
		SET Field1 = 'Value5'
		WHERE Ord = '0' AND (NOT (Cat = 'Hdw')) AND (NOT (Cat = 'Bnd')) AND Field1 = '';
COMMIT TRANSACTION
BEGIN TRANSACTION
UPDATE MyTable	
		SET Field1 = 'Value4'
		WHERE (Ind = '1' AND Cat = 'Hdw') OR (Cat = 'Bnd') AND Field1 = '';
COMMIT TRANSACTION
BEGIN TRANSACTION
UPDATE MyTable
		SET Field1 = 'Value5'
		WHERE (Ind = '1' AND NOT (Cat = 'Hdw')) AND (NOT (Cat = 'Bnd')) AND Field1 = '';
COMMIT TRANSACTION
BEGIN TRANSACTION
UPDATE MyTable	
		SET Field1 = 'Value6'
		WHERE Ind = '0' AND Cat = 'Hdw' OR Cat = 'Bnd' AND Field1 = '';
COMMIT TRANSACTION
BEGIN TRANSACTION
UPDATE MyTable
		SET Field1 = 'Value7'
		WHERE (Ind = '0' AND NOT (Cat = 'Hdw')) AND (NOT (Cat = 'Bnd')) AND Field1 = '';
COMMIT TRANSACTION

My biggest problem seems to be that between the piece that updates Field1 as either Value4 or Value5 is that something is not right. I initially had this set up in MS Access, and so am using that as a comparison to check my final values once the field has been udpated. When I run in Access I get a total of 9,802 for Value6 and 14,107 for Value4, yet when I run the SQL code I get 19,548 for Value 6 and 4,478 for Value4.

I also get some differences for the other values, but nowhere near as large of a difference as with the 2 values above. I feel fairly confident that I have something not quite right with my syntax, but I cannot figure out exactly what it is. Field1 at the beginning of the query is inserted into the table as a blank value.

Thanks.



Viewing all articles
Browse latest Browse all 23857

Trending Articles