Re: SQL Question: Update Syntax
Posted in 1998
M00n321 wrote: >>How can I update table1.flag with the value of table2.flag >>where table1.value=table2.value >> > >UPDATE table1 > SET flag = > ( SELECT flag > FROM table2 > WHERE table2.value = table1.value) >WHERE 1 = 1; #all records > >You will want to make sure that both table1 and table2 are indexed on value >unless these tables are very small (row count). > > It's wrong (may be, but I'm sorry I didn't read the initial message). 1. For "all records" you needn't the "WHERE 1=1". 2. As you have written the statement you will set the table1.flag to NULL for value's not in table2. To set only the values in table2 you should rewrite the statement as: UPDATE table1 SET flag = ( SELECT flag FROM table2 WHERE table2.value = table1.value) WHERE value IN ( SELECT table2.value FROM table2 ) Kind Regards, Octav -- Octav Chiriac Phone: (373) 2 21 20 96 NetInfo S.R.L. Fax: (373) 2 21 36 59 Chisinau (373) 2 24 00 83 Moldova, Republic of mailto:com@netinfo-moldova.com