我需要写这样的存储过程,逻辑简单描述是这样的:
1:从学生表里查询已经参加过考试的学生的id
2:用这些id 再从成绩表,课程表里取得数据填充到另外得表里。
我想要用到cursor,循环等等,但我对oracle 半知半解,请大家帮我写这个SP 的结构?谢谢!

解决方案 »

  1.   

    如果能直接一句sql拉出来这些数据,就不要用游标
      

  2.   

    这个应该是用一个语句就可以了的吧.
    insert into course_score
      (student_id, student_name, course_name, score_value)
      select s.student_id, s.student_name, b.course_name, a.score_value
        from student s, score a, course b
       where a.course_id = b.course_id
         and s.student_id = a.student_id
         and s.status = '已参加'
      

  3.   

    不知道你想插入的表主键是什么。楼上的方法在主键不是序列的时候,是最佳方案
    否则,就只有循环插入了。
    cursor aa is
    select id,..
      from..
    ;for v in aa loopinsert into bb
    values v.id...; commit;
    end loop;
      

  4.   

    请问Robin“主键不是序列”是什么意思?
      

  5.   

    如果你的主键是序列,就要写成insert into table id select seq.nextval,由于不是循环,seq.nextval
    是一个值,就会造成主键冲突
      

  6.   

    insert into course_score 
      (student_id, student_name, course_name, score_value) 
      select s.student_id, s.student_name, b.course_name, a.score_value 
        from student s, score a, course b 
      where a.course_id = b.course_id 
        and s.student_id = a.student_id 
        and s.status = '已参加'谢谢二楼的回答,但是我编译不过啊?在select 的地方报错。
      

  7.   

    编译通过了,但是如果我要插入常量怎么写?比如course_name是“Chinese”?
      

  8.   

    insert into course_score 
      (student_id, student_name, course_name, score_value) 
      select s.student_id, s.student_name,'Chinese' course_name, a.score_value 
        from student s, score a, course b 
      where a.course_id = b.course_id 
        and s.student_id = a.student_id 
        and s.status = '已参加'