我有一個簡單的任務(他們說!),我需要使用select
語句更新表列。如何使用具有正確標識的同一表中的select查詢進行更新?
類似下面:
所以我們可以說此表中,我對以前的pcsProduces
列不好的數據, 現在我想乘腔和heatcyclecount
,然後我想將pcsProduces
列更新爲適當的值。
問題是,我有成千上萬的記錄,如果有人可以通過展示如何使用簡單更新和選擇查詢來幫助我,我真的很感激。
我有一個簡單的任務(他們說!),我需要使用select
語句更新表列。如何使用具有正確標識的同一表中的select查詢進行更新?
類似下面:
所以我們可以說此表中,我對以前的pcsProduces
列不好的數據, 現在我想乘腔和heatcyclecount
,然後我想將pcsProduces
列更新爲適當的值。
問題是,我有成千上萬的記錄,如果有人可以通過展示如何使用簡單更新和選擇查詢來幫助我,我真的很感激。
剛剛火SQL更新命令,如:
update tablename set pcsProduces = cavities * heatcyclecount
你至少有2個選擇:
更新整個表的更新語句(WHERE子句是可選的):
update TbYourTable
set pcsProduces = cavities * heatsOrCyclecount
where pcsProduces != cavities * heatsOrCyclecount
使用計算列(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];
,如果你想簡單的更新通過(腔* heatcyclecount)乘你pcsProduces列,那麼就使用更新的tablename集pcsProduces =腔* heatcyclecount。 –
噢,上帝,謝謝你,這是最簡單的方法! –
我認爲這對你有幫助。 https://stackoverflow.com/questions/2334712/how-do-i-update-from-a-select-in-sql-server – Rajan