SQL Update语句(MS ACCESS)

时间:2011-02-13 08:53:46

标签: c# sql database ms-access

我执行了以下声明,但它只更新除Assasin信条之外的所有价格行(空白值)。此外,我的表格中其他行中未明确定义的价格被清​​除(空白值)。如果我更新2个产品,它似乎正常工作。可能有什么不对?

(仅用于展示包装的换行符)

"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 = 'Asassins Creed'," + AssassinsCreedInput.Text + ")))))";

2 个答案:

答案 0 :(得分:0)

也许是因为你错误地拼写了“Assasins Creed”? 设置WHERE子句,或在最后一个IIF中添加Price作为默认值

"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)))))";
  • 我表格中未定义的其他行

这是WHERE子句的用途,用于限制要更新的记录。

"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')";

您也可以使用SWITCH statement代替多个IIF。

答案 1 :(得分:0)

Re assasins信条 - 可能是一个错字。仔细查看单元格和查询中的值 - 例如,它是刺客与刺客吗?

重新启动其他人(不在查询中);你需要添加一个“where”子句,否则它适用于表中的每一行;这就是SQL的工作方式......我猜这里的IN过滤器最好。 Fr示例

where Product in ('some title', 'another title', ...)