更新除NULL之外的数据库表

Bri*_*ton 0 sql sql-server-2005 sql-update

我有两个具有相同列的表

tbl_source (ID, Title)

tbl_dest (ID, Title)
Run Code Online (Sandbox Code Playgroud)

我想从tbl_source更新tbl_dest标题,其中dest和source中的id匹配.但是,如果源标题为null(或空白),我不想更新dest标题.

我有这个:

UPDATE    tbl_dest
SET              tbl_dest.Title =
      (SELECT     title
        FROM          tbl_source
        WHERE      tbl_dest.id = tbl_source.ID and tbl_source.title is not null)
Run Code Online (Sandbox Code Playgroud)

但它继续插入空值.

我该如何构建这样的查询?

我正在使用SQL Server 2005.

谢谢.

Jas*_*yon 5

使用内部联接......

Update tbl_dest
Set tbl_dest.Title = tbl_source.Title
From tbl_dest inner join tbl_source on tbl_dest.ID = tbl_source.ID
Where tbl_source.Title is not null and tbl_source.Title <> ''
Run Code Online (Sandbox Code Playgroud)