大侠们帮忙。谢谢了。得到满意答复,分立即送出
表exam 主键eid 其他字段max,cid
表course 主键cid
表course_tk这个表主要做一一映射的作用,我外边的程序要传一个平台的courseID然后通过这个表映射成另一个平台的courseID然后根据另一个平台的courseID处理业务。
表stu_exam 主键uid 其他字段max,cid
存储过程,要求接2个参数一个uid一个cid_tk
然后根据cid_tk在course_tk表中检索出本平台要用的cid然后根据cid检索exam表【说面:一个课程对应多次考试】然后把符合条件的数据检索出来,依次追加到stu_exam表,
不知道我说清楚没有,求大家帮个忙,路过的帮我顶一下,谢谢了 ToT
还有,就是存储过程中的事务处理怎么写?

解决方案 »

  1.   

    简单的说:create procedure sp_test(@uid int,@cid_tk int)
    as
    begin
        insert into stu_exam(...)
        select
            ...
        from
            course_tk ct,
            exam      ex
        where
            ct.关联字段=ex.关联字段
            and
            ex.uid=@uid
            and
            ct.cid_tk=@cid_tk
    end
    go需要根据表结构来完善这个SP
      

  2.   

    表结构:
    exam:
    eid cid name description maxtimes
    6 0005 第1次试题 3
    7 0005 第二次试题 期中考试 2
    8 0005 55 55 55
    9 0005 第3次模拟题 100
    10 0005 4 4 4
    11 0005 5 5555 5
    12 0005 第5次模拟题 模拟题模拟题模拟题 12
    stu_exam:
    uid eid maxtimes usedtimes createtime
    wz 6 7                7              2007-1-11
    wz 7 100             7                     2007-1-20 13:36:00
    wz 9 100             1                    2007-1-30 10:17:00
    wz 12 12              1                   2007-3-2 11:34:00
    course_tk:[ccode向但与我上面说的cid_tk]
    cid cname ccode createuser createdate orders
    0001 大学英语 A00011 zhangxia 2006-7-25 16:23:00 0
    0002 工程力学与建筑结构 B0501A zhangxia 2006-8-24 13:23:27 0
    0003 基础会计 B0101A zhangxia 2006-8-24 13:25:19 0楼上的一会儿我给分,
      

  3.   

    还有解释我需要一个flg,表示以下这个存储过程成没成功,怎么表示,同时我还需要一个输出变量,这个变量检索出结果的eid【说明,一个课程对应多个考试】
      

  4.   

    create procedure sp_test(@uid varchar(20),@ccode varchar(20))
    as
    begin
        insert into stu_exam(uid,eid,maxtimes,usedtimes,createtime)
        select
            @uid,
            ex.eid,
            ex.maxtimes,
            count(ex.eid),
            ct.createtime
        from
            course_tk ct
        left join
            exam      ex
        on
            ct.cid=ex.cid
        where
            ct.ccode=@ccode
        group by
            ex.eid,ex.maxtimes,ct.createtime
    end
    go
      

  5.   

    create procedure sp_test(@uid varchar(20),@ccode varchar(20))
    as
    begin
        --向stu_exam表insert数据
        insert into stu_exam(uid,eid,maxtimes,usedtimes,createtime)
        select
            @uid,
            ex.eid,
            ex.maxtimes,
            count(ex.eid),
            ct.createtime
        from
            course_tk ct
        left join
            exam      ex
        on
            ct.cid=ex.cid
        where
            ct.ccode=@ccode
        group by
            ex.eid,ex.maxtimes,ct.createtime
        
        --以结果集方式返回符合条件的eid
        select eid from course_tk where ccode=@ccode
    end
    go
      

  6.   

    create procedure sp_test(@uid varchar(20),@ccode varchar(20))
    as
    begin
        --向stu_exam表insert数据
        insert into stu_exam(uid,eid,maxtimes,usedtimes,createtime)
        select
            @uid,
            ex.eid,
            ex.maxtimes,
            isnull(count(ex.eid),0),  --如果没用使用过,则赋值为0
            ct.createtime
        from
            course_tk ct
        left join
            exam      ex
        on
            ct.cid=ex.cid
        where
            ct.ccode=@ccode
        group by
            ex.eid,ex.maxtimes,ct.createtime
        
        --以结果集方式返回符合条件的eid
        select eid from course_tk where ccode=@ccode
    end
    go
      

  7.   

    忘记说了一个,我还要判断但前要插入的数据存不存在,如果存在我要对stu_exam表的这条记录进行更新,str_exam表的主键是uid和eid其中uid不用关了,是程序传过来的。
      

  8.   

    create procedure sp_test(@uid varchar(20),@ccode varchar(20))
    as
    begin
        --删除已经存在的数据
        delete c 
        from
            course_tk a,exam b,stu_exam c
        where 
            a.cid=b.cid
            and
            b.eid=c.eid
            and
            a.ccode=@ccode
            and
            c.uid=@uid
            
        --向stu_exam表insert数据
        insert into stu_exam(uid,eid,maxtimes,usedtimes,createtime)
        select
            @uid,
            ex.eid,
            ex.maxtimes,
            isnull(count(ex.eid),0),  --如果没用使用过,则赋值为0
            ct.createtime
        from
            course_tk ct
        left join
            exam      ex
        on
            ct.cid=ex.cid
        where
            ct.ccode=@ccode
        group by
            ex.eid,ex.maxtimes,ct.createtime
        
        --以结果集方式返回符合条件的eid
        select eid from course_tk where ccode=@ccode
    end
    go
      

  9.   

    如果存在我要对stu_exam表的这条记录进行更新
    ---------------------------------------------------------------------------------
    可以不更新而改用直接删除,然后重新insert的方式.