表:student
stu_id  stu_name   stu_addr             stu_age
0301001 王立     中国江苏@         750
0301002 胡锦涛      宁都青塘          675
0301004 毛泽东      湖南韶关          500
0301003 李天师       上海*         450表:stubak
id      name      addr          age
0301001 王立 中国江苏@ 750
0301002 胡锦涛 宁都青塘          675
0301004 毛泽东 湖南韶关          500
0301003 李天师 上海*         450 用存储过程实现:stubak表的age的值等于student的stu_age加上stubak表的age值。如何实现???结果如下:
id      name     addr            age
0301001 王立 中国江苏@ 1500
0301002 胡锦涛 宁都青塘          1350
0301004 毛泽东 湖南韶关          1000
0301003 李天师 上海*          900

解决方案 »

  1.   


    select m.stu_id , m.stu_name , m.stu_addr , m.stu_age + n.stu_age
    from student m , stubak  n
    where m.stu_id = n.stu_id
      

  2.   

    --1select m.stu_id , m.stu_name , m.stu_addr , m.stu_age + n.stu_age
    from student m , stubak  n
    where m.stu_id = n.stu_id--2select stu_id , stu_name , stu_addr , sum(stu_age) stu_age from
    (
    select * from student
    union all
    select * from stubak 
    ) t
    group by stu_id , stu_name , stu_addr
      

  3.   

    哪个作为变量?如果是表做变量,用动态SQL.
      

  4.   

    楼上的大哥,我要用存储过程来实现并且要设计变量。。如下面。。alter proc okok
    as
    declare @aa int,@bb int
    select @aa=stu_age from student 
    select @aa=age from stubak
    update stubak set stu_sum=@aa+@bb 
      

  5.   

    用表student的stu_age作为变量...
    用存储过程如何实现???
      

  6.   

    alter proc okok 
    as
    declare @aa int
    select @aa=stu_age from student,stubak where student.stu_id=stubak.id
    update stubak set age=age+@aa
    我写了一个。用存储过程的。
    但是select @aa=stu_age from student,stubak where student.stu_id=stubak.id
    这一句有多条记录,我想一笔一笔记录给@aa,但是不知道怎么赋值???
    结果就不对了
    求求大哥们谢谢
      

  7.   

    请大家看一下我这个存储过程怎么算法有错误。。
    alter proc okok 
    as 
    declare @aa int 
    select @aa=stu_age from student,stubak where student.stu_id=stubak.id 
    update stubak set age=age+@aa 
    谢谢
      

  8.   


    update stubak set age=age+@aa --这里你没有写更新的where条件会把所有的记录行都更新了的
      

  9.   


    alter proc okok 
    as
    update stubak set age=stubak.age+student.age from stubak,student  where stubak.id=student.id
      

  10.   

    我要用存储过程如何实现???
    alter proc okok 
    as 
    declare @aa int 
    select @aa=stu_age from student,stubak where student.stu_id=stubak.id 
    update stubak set age=age+@aa where student.stu_id=stubak.id 请问我写的这个存储过程怎么错了?如何修改??
      

  11.   


    你那个select给@aa赋值的语句,它的值只是最后一个age的值,前面的值被覆盖了,也就是说,@aa的值并不是数组,只是最后一个age的值,而且你这样写没有必要,一定要这样用变量赋值的话,那大概得用游标了。
      

  12.   


    create table student
    (
    stu_id nchar(10),
    stu_name nchar(10),
    stu_addr nchar(10),
    stu_age int
    )
    create table stubak
    (
    id nchar(10),
    [name] nchar(10),
    addr nchar(10),
    age int
    )
    ----------------------------------------
    insert into student
    select '0301001', '王立','中国江@',750 union all
    select '0301002', '胡涛','都青塘',675 union all
    select '0301003', '毛,'湖南韶',500 union all
    select '0301004', '李,'上海*',450 
    ------------------------------------
    insert into stubak
    select '0301001', '王立','中国江@',750 union all
    select '0301002', '胡涛','都青塘',675 union all
    select '0301003', '毛','湖南韶',500 union all
    select '0301004', '李天','上海*',450
    ----------------------------------------------------
    create procedure AddAge
    as
    update stubak  set
    stubak.age=stubak.age+B.stu_age
    from student B
    where stubak.id=B.stu_idexec AddAge
    -------------------------------------------------
    0301001    王立         中国江@       1500
    0301002    胡涛         都青塘        1350
    0301003    毛          湖南韶        1000
    0301004    李天         上海*        900 
      

  13.   


    create table student
    (
    stu_id nchar(10),
    stu_name nchar(10),
    stu_addr nchar(10),
    stu_age int
    )
    create table stubak
    (
    id nchar(10),
    [name] nchar(10),
    addr nchar(10),
    age int
    )
    ----------------------------------------
    insert into student
    select '0301001', '王立','中国江@',750 union all
    select '0301002', '胡涛','都青塘',675 union all
    select '0301003', '毛,'湖南韶',500 union all
    select '0301004', '李,'上海*',450 
    ------------------------------------
    insert into stubak
    select '0301001', '王立','中国江@',750 union all
    select '0301002', '胡涛','都青塘',675 union all
    select '0301003', '毛','湖南韶',500 union all
    select '0301004', '李天','上海*',450
    ----------------------------------------------------
    create procedure AddAge
    as
    update stubak  set
    stubak.age=stubak.age+B.stu_age
    from student B
    where stubak.id=B.stu_idexec AddAge
    -------------------------------------------------
    0301001    王立         中国江@       1500
    0301002    胡涛         都青塘        1350
    0301003    毛          湖南韶        1000
    0301004    李天         上海*        900 
      

  14.   

    select a.stu_id as id, a.stu_name as name, a.stu_addr as addr, a.stu_age+b.age as age
    from student as a inner join stubak as b on a.stu_id=b.id