2017-09-26 30 views
0

我有一個簡單的任務(他們說!),我需要使用select語句更新表列。如何使用具有正確標識的同一表中的select查詢進行更新?

類似下面:

Test table

所以我們可以說此表中,我對以前的pcsProduces列不好的數據, 現在我想乘腔和heatcyclecount,然後我想將pcsProduces列更新爲適當的值。

問題是,我有成千上萬的記錄,如果有人可以通過展示如何使用簡單更新和選擇查詢來幫助我,我真的很感激。

+1

,如果你想簡單的更新通過(腔* heatcyclecount)乘你pcsProduces列,那麼就使用更新的tablename集pcsProduces =腔* heatcyclecount。 –

+0

噢,上帝,謝謝你,這是最簡單的方法! –

+0

我認爲這對你有幫助。 https://stackoverflow.com/questions/2334712/how-do-i-update-from-a-select-in-sql-server – Rajan

回答

2

剛剛火SQL更新命令,如:

update tablename set pcsProduces = cavities * heatcyclecount 
1

你至少有2個選擇:

  1. 更新整個表的更新語句(WHERE子句是可選的):

    update TbYourTable 
    set pcsProduces = cavities * heatsOrCyclecount 
    where pcsProduces != cavities * heatsOrCyclecount 
    
  2. 使用計算列(MS SQL語法)

    create table [TbYourTable] 
    (
        [Id]    int  identity(1,1) not null 
    , [domainS]   int      not null 
    , [tStations]   int      not null 
    , [itemNo]   int      not null 
    , [defaultCavities] int      not null 
    , [missingCavities] int      not null 
    , [cavities]   int      not null 
    , [heatsOrCyclecount] int      not null 
    , [shift]    nvarchar (max)    null 
    , [pcsProduces]  as ([cavities] * [heatsOrCyclecount]) persisted not null -- peristed clause is optional 
    
    , constraint [PK_TbYourTable] primary key nonclustered 
        (
         [Id] asc 
        ) 
    
    ) on [primary]; 
    
相關問題