0

6我有两个表如下,TABLEA:

MEMBER_ID   CLIENT_ID   TENTATIVE_ID    TENTATIVE_START_DATE
1   65239   26  6/15/2012
2   63693   NULL    NULL
3   5549    NULL    NULL
4   85452   NULL    NULL
5   77898   11  6/15/2012
6   93119   21  6/15/2012
7   7876    26  6/20/2012
8   27572   26  6/21/2012
9   15524   26  6/21/2012
10  39465   8   6/21/2012
11  10143   26  6/23/2012
12  72828   NULL    NULL

表 B:

TENTATIVE_ID    LAST_AUTO_ASSIGNED_ON
26  6/15/2012
11  6/16/2012
21  6/13/2012
27  6/20/2012
28  6/22/2012
29  6/25/2012
8   6/26/2012
21  6/24/2012

情况是,我需要根据最小 LAST_AUTO_ASSIGNED_ON 值使用表 B 中的 TENTATIVE_ID 更新表 A 中的空值,并且每次分配 id 时,都会使用表 B 中该 id 的当前日期时间更新 LAST_AUTO_ASSIGNED_ON。

通过这种方式,我们循环遍历表 B 中的所有暂定 ID,并将它们分配给表 A。

我不允许使用光标。我怎么能做到这一点?

谢谢!

4

1 回答 1

1

更新 Tablea set tablea.tentative_id = (select top 1 tableb.tentative_id from tableb order by Last_auto_assigned_on, tentative_id), Tentative_start_date = getdate() from tablea where tablea.tentative_id 为空

更新tableb set last_auto_assigned_on = getdate() where tentative_id = (select top 1 tableb.tentative_id from tableb order by Last_auto_assigned_on, tentative_id)

于 2012-06-27T20:02:23.710 回答