2012-08-09 78 views
0

我有以下的select語句:TSQL轉換SELECT語句來更新加入

SELECT projectid,documentid,revisionno,configurationid,variable45, 
     ISNULL(Variable45, (SELECT TOP 1 variable45 FROM pivot_table WHERE documentid = t.documentid and projectid=t.projectid 
     and configurationid=t.configurationid and cast(revisionno as int) < cast(t.revisionno as int) AND Variable45 is NOT NULL 
     ORDER BY projectid desc,documentid desc ,revisionno desc,configurationid desc)) as NewCol 
FROM pivot_table t; 

我試圖轉換爲更新stement下面的方式,但我得到錯誤的記錄進行更新。誰能幫我解決我的問題:

UPdate PIVOT_TABLE 
set variable45 = ((SELECT TOP 1 variable45 FROM pivot_table t WHERE t.documentid = documentid and t.projectid=projectid 
     and t.configurationid=configurationid and cast(t.revisionno as int) < cast(revisionno as int) AND Variable45 is NOT NULL 
     ORDER BY revisionno desc)) where Variable45 is NULL; 

DB:SQLExpress2008。

請指教。謝謝。

回答

0

好,我想通了:

UPdate pt 
    set pt.variable45 = ((SELECT TOP 1 t.variable45 FROM pivot_table t WHERE 
t.documentid = pt.documentid and t.projectid=pt.projectid and t.configurationid=pt.configurationid and cast(t.revisionno as int) < cast(pt.revisionno as int) AND t.variable45 is NOT NULL 
      ORDER BY revisionno desc)) from PIVOT_TABLE pt where pt.variable45 is NULL;