看来朋友们没看懂问的,我重发一下,这回给数据
===============================
建了一个tablea表,
字段名   类型
----------------
aid    nchar(10)
-----------------
aname  nchar(50)
------------------自动生成数据
-------------DECLARE @I INTDECLARE @ENDID INTSELECT @I = 0SELECT @ENDID = 200  --在此处更改要插入的数据,重新插入之前要删掉所有数据WHILE @I <= @ENDIDBEGIN    INSERT INTO tablea    SELECT 'AB'+CAST(@I AS VARCHAR(10))+'EF','B-'+CAST(@I AS VARCHAR(10))    SELECT @I = @I + 1END=================
建了一个tableb表,
字段名   类型
----------------
bid    nchar(10)
-----------------
bname  nchar(10)
------------------
bcontext  nchar(80)
-------------------填加数据DECLARE @I INTDECLARE @ENDID INTSELECT @I = 300SELECT @ENDID = 600  --加300条哦WHILE @I <= @ENDIDBEGIN    INSERT INTO tableb    SELECT 'AB'+CAST(@I AS VARCHAR(10))+'EF','B-'+CAST(@I AS VARCHAR(10)),'iuuiok'    SELECT @I = @I + 1END------------------
好了,现在tablea中有了200条数据,tableb中有300数据,并且它们字段数不同(一个有2,一个有3个)我想实现用tableb中的第50-250条数据的bid 值 来 更新 tablea中的200条数据aid值
我自己试着写的 update tablea set aid = bid from tableb where bid in (select top 200 bid from tableb)
结果:所有的tablea中的aid字段的值是 'AB300EF'{这是tableb中bid字段中的第一个记录的值},我错哪了呢?为什么不是一对一的呢?这次应该我是说明白我的问题了,希望高手给解决一下。
这贴是50+100{100分在下贴中,是一个问题}
http://topic.csdn.net/u/20090408/23/882b15fd-23ec-467a-9259-9ec7fb471663.html?seed=452852627

解决方案 »

  1.   

    [[[注意]]]
    问题我重新表述了一下。
    一共150分了
    http://topic.csdn.net/u/20090409/10/2b7d793d-03e3-4ccd-84b4-30dc66455855.html?seed=1560818706
      

  2.   

    tablea和tableb的字段要通过关联才能对应 
      

  3.   


    update tablea set aid = bid from tableb where bid in (select top 200 bid from tableb) 
    -------------------
    主要是没有条件关联修改
    --增加辅助列
    alter table tablea add id int identity(1,1)
    alter table tableb add id int identity(1,1)
    go
    --修改数据
    update tablea set aid=b.bid from tablea a,tableb b
    where a.id=(b.id+50) and  a.id between 51 and 251
    and bid between 1 and 200
    go
    --删除辅助列
    alter table tablea drop column id
    alter table tableb drop column id
      

  4.   


    --增加辅助列
    alter table tablea add id int identity(1,1)
    alter table tableb add id int identity(1,1)
    go
    --修改数据
    update tablea set aid=b.bid from tablea a,tableb b
    where a.id=(b.id+50) and  a.id between 51 and 251
    and b.id between 1 and 200  --上面这里打错了。
    go
    --删除辅助列
    alter table tablea drop column id
    alter table tableb drop column id
      

  5.   


    update A set A.aid= from tablea,
    (select * from (select top 200 *,row_number() over(order by bid desc) rank from tableb) 
    where rank>50)B where A.aid=B.bid
      

  6.   

    2005还不简单吗?直接使用ROW_NUMBER
      

  7.   


    --或者利用你的数据中的规律。update tablea set aid = b.bid from tablea a,tableb b 
    where replace(replace(a.aid,'ab',''),'ef','')=replace(replace(b.bid,'ab',''),'ef','')-350
    and replace(replace(b.bid,'ab',''),'ef','')>=350
    select * from tablea
      

  8.   

    ;WITH Liang AS
    (
        SELECT *,rnk=ROW_NUMBER() OVER(ORDER BY GETDATE())
        FROM tablea
    )
    UPDATE Liang SET aid=b.bid
    FROM (
        SELECT
            *,rnk=ROW_NUMBER() OVER(ORDER BY GETDATE())
        FROM tableB
    ) AS B
    WHERE B.rnk BETWEEN 50 AND 250
        AND Liang.rnk=B.rnk
      

  9.   

    我先吃饭,我打算用ROW_NUMBER()来关联{既然必须关联},即aid的ROW_NUMBER()与bid的ROW_NUMBER(){选出来的200条的ROW_NUMBER()},然后……我先吃饭,我还没吃早饭哟
      

  10.   

    在大家的帮助下,我写出了以下SQL,达到我的要求效果,但效率真是不高啊:SQL Server 分析和编译时间: 
       CPU 时间 = 0 毫秒,占用时间 = 13 毫秒。
    表 'TableA'。扫描计数 1,逻辑读取 204 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
    表 'Worktable'。扫描计数 0,逻辑读取 0 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
    表 'TableB'。扫描计数 1,逻辑读取 9 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。SQL Server 执行时间:
       CPU 时间 = 15 毫秒,占用时间 = 5 毫秒。(200 行受影响)
    SQL Server 分析和编译时间: 
       CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。
    希望哪位高手再给说一下,我这个SQL还有能优化的地方没
      

  11.   

            SET STATISTICS io ON
            SET STATISTICS time ON
            go
             ---你要测试的sql语句
    update tablea set tablea.aid=c.bid from (select top 200 bid ,row_number() over(order by bid desc) newidb from tableb) c,
    (select aid,row_number() over(order by aid asc) newida from tablea) tablea where newida=newidb
            go
            SET STATISTICS profile OFF
            SET STATISTICS io OFF
            SET STATISTICS time OFF
      

  12.   

    貌似子查询(最后一个括号里的查询)中没有where条件限制
      

  13.   

    update tablea set aid=
    (
    select bid 
    from tableb 
    where 
    cast(stuff(bname,1,2,'') as int)-
    cast(stuff(aname,1,2,'') as int)
    =289
    )简单明了
      

  14.   

    update tablea set aid = bid from tableb where bid in (select top 200 bid from tableb) ------------------- 主要是没有条件关联修改 --增加辅助列 alter table tablea add id int identity(1,1) alter table tableb add id int identity(1,1) go --修改数据 update tablea set aid=b.bid from tablea a,tableb b where a.id=(b.id+50) and a.id between 51 and 251 and bid between 1 and 200 go --删除辅助列 alter table tablea drop column id alter table tableb drop column id 
      

  15.   

                 kan kan
      

  16.   

                 kan kan
      

  17.   

    hiatus没有用过2005!
    定下!学习!