表: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
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
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
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
as
declare @aa int,@bb int
select @aa=stu_age from student
select @aa=age from stubak
update stubak set stu_sum=@aa+@bb
用存储过程如何实现???
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,但是不知道怎么赋值???
结果就不对了
求求大哥们谢谢
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
谢谢
update stubak set age=age+@aa --这里你没有写更新的where条件会把所有的记录行都更新了的
alter proc okok
as
update stubak set age=stubak.age+student.age from stubak,student where stubak.id=student.id
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 请问我写的这个存储过程怎么错了?如何修改??
你那个select给@aa赋值的语句,它的值只是最后一个age的值,前面的值被覆盖了,也就是说,@aa的值并不是数组,只是最后一个age的值,而且你这样写没有必要,一定要这样用变量赋值的话,那大概得用游标了。
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
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
from student as a inner join stubak as b on a.stu_id=b.id