【问题标题】:sql server update field if condition is met如果满足条件,则 sql server 更新字段
【发布时间】:2013-11-22 10:45:51
【问题描述】:

我正在做一个项目,我必须更改表格定义,但我需要保留已经存在的数据,但我不知道如何完成。

实际的表结构如下

[IDDocumento (int)]
[IDTipoDocumento (int)]
[IDEntidade (int)]
[Data (datetime)]
[Designacao (varchar 100)]
[TotalIliquido (money)]
[TotalImposto (money)]
[TotalRetencoes (money)]
[TotalLiquido (money)]
[Anulado (bit)]

我需要在这里做的是删除“Anulado”列并添加 3 个新列,维护存储的数据,如果“Anulado”为 TRUE,则为其中一个新列赋值

我拥有的是

ALTER TABLE dbo.Documentos
ADD
IDEstadoDocumento int null,
DataVencimento datetime null,
ValorAberto decimal(18,5) not null CONSTRAINT DF_Documentos_ValorAberto DEFAULT 0;

//HERE IS WHERE I NEED TO CHECK IF 'ANULADO' IS TRUE OR NOT AND CHANGE 'IDEstadoDOCUMENTO' TO 3 WHEN IT IS

ALTER TABLE dbo.Documentos
DROP COLUMN Anulado;

我应该怎么做?

【问题讨论】:

  • 需要使用纯sql吗?

标签: sql sql-server sql-update alter


【解决方案1】:

我简单的更新就足够了

UPDATE dbo.Documentos SET IDEstadoDocumento = 3 WHERE Anulado = 1

【讨论】:

  • 哦,我的……当然!大声笑今天早上没有考虑直:D谢谢
猜你喜欢
  • 2016-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-24
  • 1970-01-01
相关资源
最近更新 更多