SQL UPDATE 更新資料用法之多表查詢

2021-07-27 08:01:36 字數 4306 閱讀 8917

資料表2張,如:表a,表b

現在要求:更新a表的字段,但這些表是從b表來的,且a和b並沒有什麼關係等。

實現方式:採用update a set from b方式實現,a表和b表的字段很多,採用@變數方式比較麻煩。

具體**:

**************更新

if exists (select * from sysobjects where name = 『housingsalelistingterminatedeclare_update』 and schema_name(uid) = 『bdcclf』)

drop procedure bdcclf.housingsalelistingterminatedeclare_update;

gocreate procedure bdcclf.housingsalelistingterminatedeclare_update

@housingsalelistingterminateguid varchar(100),

@housingsalelistingguid varchar(100)–被撤銷的合同guid

with encryption

as set nocount on

update a

set

a.housingsalelistingguid = t.housingsalelistingguid,

a.housingverificationcode = t.housingverificationcode,

a.unitguid = t.unitguid,

a.propertyrightguid = t.propertyrightguid,

a.districtcode = t.districtcode,

a.listingcurrency = t.listingcurrency,

a.listingtotalprice = t.listingtotalprice,

a.listingunitprice = t.listingunitprice,

a.housetype_room = t.housetype_room,

a.housetype_hall = t.housetype_hall,

a.housetype_kitchen = t.housetype_kitchen,

a.housetype_toilet = t.housetype_toilet,

a.buildingarea = t.buildingarea,

a.decorationdegree = t.decorationdegree,

a.houseorientation = t.houseorientation,

a.totallayers = t.totallayers,

a.locationlayer = t.locationlayer,

a.housestructure = t.housestructure,

a.planningpurposes = t.planningpurposes,

a.housingproperty = t.housingproperty,

a.linker = t.linker,

a.linkermobile = t.linkermobile,

a.housingsaleentrustagreementguid = t.housingsaleentrustagreementguid,

a.housingsaleentrustagreementpractitionerguid = t.housingsaleentrustagreementpractitionerguid,

a.compoundguid = t.compoundguid,

a.compoundname = t.compoundname,

a.buildingyear = t.buildingyear,

a.compoundaddress = t.compoundaddress,

a.compoundlocationaddress = t.compoundlocationaddress,

a.obligeename = t.obligeename,

a.floorplan = t.floorplan,

a.landscapemap = t.landscapemap,

a.propertymanagementcompany = t.propertymanagementcompany,

a.propertymanagementfee = t.propertymanagementfee,

a.greeningrate = t.greeningrate,

a.volumeratio = t.volumeratio,

a.rentalflag = t.rentalflag,

a.supportingfacilities = t.supportingfacilities,

a.publishsource = t.publishsource,

a.publishdate = t.publishdate,

a.publishterm_start = t.publishterm_start,

a.publishterm_end = t.publishterm_end,

a.publishrefreshtime = t.publishrefreshtime,

a.readingcount = t.readingcount,

a.registertime=getdate(),

a.auditpasstime =null,

a.acceptancenumber =』n』

**from

(select

housingsalelistingguid,

housingverificationcode,

unitguid,

propertyrightguid,

districtcode,

listingcurrency,

listingtotalprice,

listingunitprice,

housetype_room,

housetype_hall,

housetype_kitchen,

housetype_toilet,

buildingarea,

decorationdegree,

houseorientation,

totallayers,

locationlayer,

housestructure,

planningpurposes,

housingproperty,

linker,

linkermobile,

housingsaleentrustagreementguid,

housingsaleentrustagreementpractitionerguid,

compoundguid,

compoundname,

buildingyear,

compoundaddress,

compoundlocationaddress,

obligeename,

floorplan,

landscapemap,

propertymanagementcompany,

propertymanagementfee,

greeningrate,

volumeratio,

rentalflag,

supportingfacilities,

publishsource,

publishdate,

publishterm_start,

publishterm_end,

publishrefreshtime,

readingcount

from bdcclf.bas_housingsalelisting

where housingsalelistingguid=@housingsalelistingguid

) as t,bdcclf.housingsalelistingterminatedeclare as a

where a.housingsalelistingterminateguid=@housingsalelistingterminateguid**

return @@error

go

sql update 多表更新

1.首先建立兩個表 sql語句如下 users 使用者表 create table users id bigint,departid bigint,name text depart 部門表 create table depart id bigint,departname text users表中的d...

sql update 特殊用法

1.count 更新 schoolbasicinfo 中存在 menbercount 字段記錄條數 vw shcoolbasic 做count查詢 update schoolbasicinfo setmenbercount select vw shcoolbasic.countnum from vw...

SQL Update多表聯合更新的方法

sql update多表聯合更新的方法 1 sqlite 多表更新方法 update t1 set col1 t2.col1 from table1 t1 inner join table2 t2 on t1.col2 t2.col2 這是乙個非常簡單的批量更新語句 在sqlserver中支援此語法...