SQL Update statement (MS ACCESS)
I executed the following statement but it only updates the price rows of all except Assasin creed (blank value). Also, the prices in the other rows in my table which are not defined below ironically was cleared (blank value). It seems to be working normally if i update 2 products though. What could be wrong?
(line breaks for display-wrapping only)
"UPDATE Products SET Price = IIf(Product = 'Crysis Wars'," +
CrysisWarsInput.Text开发者_StackOverflow +
", IIf(Product = 'Far Cry 2'," + FarCry2Input.Text +
", IIf(Product = 'Day of Defeat Source'," + DODSourceInput.Text +
", IIf(Product = 'GTA 4'," + GTA4Input.Text +
", IIf(Product = 'Asassins Creed'," + AssassinsCreedInput.Text + ")))))";
Maybe because you mis-spelt "Assasins Creed"?
Either set a WHERE clause, or add Price
as the default value in the last IIF
"UPDATE Products SET Price = " +
" IIf(Product = 'Crysis Wars'," + CrysisWarsInput.Text +
", IIf(Product = 'Far Cry 2'," + FarCry2Input.Text +
", IIf(Product = 'Day of Defeat Source'," + DODSourceInput.Text +
", IIf(Product = 'GTA 4'," + GTA4Input.Text +
", IIf(Product = 'Assasins Creed'," + AssassinsCreedInput.Text + ", Price)))))";
- the other rows in my table which are not defined below
That's what a WHERE
clause is for, to restrict which records to update.
"UPDATE Products SET Price = " +
" IIf(Product = 'Crysis Wars'," + CrysisWarsInput.Text +
", IIf(Product = 'Far Cry 2'," + FarCry2Input.Text +
", IIf(Product = 'Day of Defeat Source'," + DODSourceInput.Text +
", IIf(Product = 'GTA 4'," + GTA4Input.Text +
", " + AssassinsCreedInput.Text + ")))) +
" WHERE Product in ('Crysis Wars','Far Cry 2','Day of Defeat Source'," +
" 'GTA 4','Assasins Creed')";
You can also use a SWITCH statement instead of multiple IIFs.
Re assasins creed - probably a typo. Look very carefully at the value in the cell and in your query - for example, is it assassin vs assassins?
Re the others (not in the query); you need to add a "where" clause, otherwise it applies to every row in the table; that is how SQL works... I'm guessing an IN filter would be best here. Fr example
where Product in ('some title', 'another title', ...)
精彩评论