有这样两个表:
表1 A
ppid  price
11          11.2
22        22.3
33          44
表2 B
ppid  ppcode
11      naa001
22      nbb001
33      ncc001请问如何用update 语句一次更新表1中 price的数值?
要把下面excel中的数值更新到对应的表1 price中:
naa001 33.3
nbb001 44.4
ncc001  55.5最终实现表1得到以下结果
ppid  price
11          33.3
22        44.4
33        55.5

解决方案 »

  1.   

    估计得修改表结构了
    把表b加一个price字段
      

  2.   

    要把下面excel中的数值更新到对应的表1 price中: 
    naa001 33.3 
    nbb001 44.4 
    ncc001  55.5先用查询导入一张表,导入数据库中,然后UPDATE B SET PRICE=C.PRICE FROM A,B,C WHERE A.PPID=B.PPID AND B.PPCODE=C.PPCODE
      

  3.   

    ---测试数据---
    if object_id('[A]') is not null drop table [A]
    go
    create table [A]([ppid] int,[price] numeric(3,1))
    insert [A]
    select 11,11.2 union all
    select 22,22.3 union all
    select 33,44
    if object_id('[B]') is not null drop table [B]
    go
    create table [B]([ppid] int,[ppcode] varchar(6))
    insert [B]
    select 11,'naa001' union all
    select 22,'nbb001' union all
    select 33,'ncc001'
     
    ---更新---
    update a
    set a.price=c.price
    from a,b,
    OpenRowSet('MICROSOFT.JET.OLEDB.4.0','EXCEL 8.0;HDR=YES;IMEX=2;DataBase=d:\test.xls',[sheet1$]) c
    where a.ppid=b.ppid and b.ppcode=c.ppcode---查询---
    select * from a
    ---结果---
    ppid        price 
    ----------- ----- 
    11          33.3
    22          44.4
    33          44.0(所影响的行数为 3 行)
     
      

  4.   

    --然后
    将Excel的数据导入SQL server :
    -- ======================================================
    SELECT * into newtable
    FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
      'Data Source="c:\book1.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...[Sheet1$]
    实例:
    SELECT * into newtable
    FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
      'Data Source="c:\Finance\account.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions
    -------------------------------------------------------------------------------------------------
      

  5.   

    --UP 树哥
    update a
    set a.price=c.price
    from a,b,
    OpenRowSet('MICROSOFT.JET.OLEDB.4.0','EXCEL 8.0;HDR=YES;IMEX=2;DataBase=d:\test.xls',[sheet1$]) c
    where a.ppid=b.ppid and b.ppcode=c.ppcode
      

  6.   

    不用,我之前朋友实现过,可以用土一点的办法。找到对应关系。直接更新就好。(naa001 ,nbb001,ncc001   ) (33.3 ,44.4,55.5) 我好久没怎么操作这些了, 忘记怎写了。帮忙弄下,谢谢。
      

  7.   

    谢谢 josy fredrickhu
    能再帮忙给点建议 请问如何用update 语句一次更新表1中 price的数值? 
      

  8.   

    update websiteprices set websiteprices.website_price=prices.website_price from prices,products,websitprices where prices.products_name=products.products_name and websiteprices.products_id=products.products_id and websiteprices.website_id=1
    执行此语句提示:
    #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from prices,products,websitprices where prices.products_name=products.products_n' at line 1
    怎么回事?