2015-09-28 56 views
-1
Create Table Active1 (Name Varchar(20), ID int IDENTITY(1,1)) 

Select * from Active1 

NAME     ID      
-------------------- ---------------------- 
vj     102      
vj     103      
vj     104      
vj     105      
vj     106      
swami    108      
swami    109 

需要輸出對於
如何獲得ID字段中的最大值並將該值設置爲同名(列)值,動態可能嗎?

vj 106 
vj 106 
vj 106 
vj 106 
vj 106 
vj 106 
vj 106 
vj 106 
vj 106 
swami 108 
swami 109 

(動態獲取和更新值)

Update Active1 SET ID=(select MAX(ID) from Active1 where Name = (select NAME from Active1) group by name HAVING count(name) > 1); 

Update Active1 SET ID=(select MAX(ID) from Active1) where name= 'select NAME from Active1' where name= 'select NAME from Active1'; 
+0

[我怎麼問** *好* question?](http://stackoverflow.com/help/how-to-ask) –

回答

0

這樣做:

 update Active1 t set t.ID = 
      (select MAX(a.ID) from Active1 a where a.name = 'vj') 
     where t.name = a.name ; 
+0

名稱字段輸入值給Dy namically –

相關問題