当前位置: 代码迷 >> Sql Server >> 存储过程批量插入!
  详细解决方案

存储过程批量插入!

热度:48   发布时间:2016-04-24 10:19:39.0
存储过程批量插入求助!!!
大神请赐教,如何把tb_student 表中的每条stuId都取出来,与其他数据一起添加到tb_scoreOfClass表中,存储过程如下

   create procedure sp_addTestOfClass
  (@clasId varchar(50),
   @testName varchar(100),
   @content varchar(1000),
   @createTime datetime,
   @startTime datetime,
   @endTime datetime,
   @flag int
   )
as
begin 
    declare @testId int,
            @stuId varchar(50)
insert into  dbo.tb_testOfClass
   ([clasId]
           ,[testName]
           ,[content]
           ,[createTime]
           ,[startTime]
           ,[endTime]
           ,[flag])
values(
 @clasId,
 @testName,
 @content,
 @createTime,
 @startTime,
     @endTime,
     @flag
);
set @testId=SCOPE_IDENTITY();
set @stuId=(select stuId from tb_student where tb_student.clasId=@clasId)
insert into tb_scoreOfClass(
stuId,
testId,
score,
replyContent,
remark,
isSubmmit
)
values(
    @stuId,
    @testId,
    0,
    '',
    '',
    0
    )   
end  

------解决方案--------------------

   CREATE PROCEDURE sp_addTestOfClass
    (
      @clasId VARCHAR(50) ,
      @testName VARCHAR(100) ,
      @content VARCHAR(1000) ,
      @createTime DATETIME ,
      @startTime DATETIME ,
      @endTime DATETIME ,
      @flag INT
    )
   AS
    BEGIN 
        DECLARE @testId INT ,
            @stuId VARCHAR(50)
        INSERT  INTO dbo.tb_testOfClass
                ( [clasId] ,
                  [testName] ,
                  [content] ,
                  [createTime] ,
                  [startTime] ,
                  [endTime] ,
                  [flag]
                )
        VALUES  ( @clasId ,
                  @testName ,
                  @content ,
                  @createTime ,
                  @startTime ,
                  @endTime ,
                  @flag
            );
        SET @testId = SCOPE_IDENTITY();

        INSERT  INTO tb_scoreOfClass
                ( stuId ,
                  testId ,
                  score ,
                  replyContent ,
                  remark ,
                  isSubmmit
  相关解决方案