在 SQL Server 视图中使用 IF

发布于 2024-09-14 15:05:07 字数 373 浏览 2 评论 0原文

我在 SQL Server 中有一个视图,可以从一个架构版本转换为另一个架构版本。
目前,视图如下所示:

SELECT newValue AS oldValue  
FROM dbo.MyTable

问题是,在新模式中,newValue 不可为空,因此我们将其设置为 -1 以表示空字段,但在旧模式中,它可以为空。

我怎样才能做一些事情来达到以下效果:

SELECT  
(  
  IF( newValue > -1 )  
    newValue as oldValue  
  ELSE  
    NULL as oldValue
)  
FROM dbo.MyTable

I have a view in SQL server that translates from one schema version to another.
Currently, the view looks like this:

SELECT newValue AS oldValue  
FROM dbo.MyTable

The trouble is that, in the new schema, newValue is not nullable, so we set it to -1 to denote empty fields, but in the old schema, it was nullable.

How can I do something to the effect of:

SELECT  
(  
  IF( newValue > -1 )  
    newValue as oldValue  
  ELSE  
    NULL as oldValue
)  
FROM dbo.MyTable

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤檠 2024-09-21 15:05:07
SELECT  
  case when newValue > -1 then  
    newValue  
  else  
    NULL
  end as oldValue
FROM dbo.MyTable
SELECT  
  case when newValue > -1 then  
    newValue  
  else  
    NULL
  end as oldValue
FROM dbo.MyTable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文