首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

这句UPDATE的SQL该如何写

2012-02-16 
这句UPDATE的SQL该怎么写?1、表CompanyBgt的数据结构是这样的:fyearmonthbranchidcategoryidoborrb其中,fye

这句UPDATE的SQL该怎么写?
1、表   CompanyBgt   的数据结构是这样的:
fyearmonth   branchid   categoryid   oborrb  
其中,fyearmonth   branchid   categoryid为主健
=========================================================================
2、我需要把这张表中,fyearmonth从200704到200803的,channelid为“IT”的OBRORB数据全部替换成channelid是“DPIT”的数据   ?
=========================================================================
3、我试着写了一下,不知道是否正确,请各位看一下:
UPDATE   CompanyBgt   a
      SET   a.oborrb   =(select   oborrb   from   CompanyBgt   b   where  
            a.fyearmonth   =b.fyearmonth
            and   a.branchid=b.branchid
            and   a.categoryid=b.categoryid
      and     b.channelid   =   'DPIT '   and   a.channelid= 'IT ')
where   a.channelid= 'IT '   and   a.Fyearmonth> = '200704 '   and   a.Fyearmonth <= '200803

[解决办法]
可以用關聯寫


Update A Set OBRORB = B.OBRORB
From CompanyBgt A
Inner Join CompanyBgt B
On A.Fyearmonth = B.Fyearmonth And A.branchid = B.branchid And A.categoryid = B.categoryid
Where A.channelid = 'IT ' And B.channelid = 'DPIT ' And A.Fyearmonth Between '200704 ' And '200803 '
[解决办法]

update CompanyBgt set CompanyBgt.oborrb = c.oborrb from (select * from CompanyBgt b where b.channelid = 'DPIT ') c,CompanyBgt where
c.fyearmonth =CompanyBgt.fyearmonth
and c.branchid=CompanyBgt.branchid
and c.categoryid=CompanyBgt.categoryid
and CompanyBgt.channelid = 'IT ' and CompanyBgt.Fyearmonth between '200704 'and '200803 '

热点排行