--execute add_data '面向对象系统分析与设计实验信计201','面向对象系统分析与设计实验信计201','101001' create proc add_data @course_id varchar(200),@table_name varchar(200),@teacher_id varchar(200) as if exists(select * from syscursors where cursor_name='prj_Cursor') deallocate prj_Cursor declare @str_sql varchar(1024); declare @num int; SELECT @num=count(NAME)-3 FROM SYSCOLUMNS WHERE ID=OBJECT_ID(@table_name); set @str_sql=CONCAT('insert into tb_Course(course_id,course_name,num_of_project,table_name,teacher_id)values(', '''', @course_id, ''',', '''', left(@course_id,len(@course_id)-3), ''',', cast(@num as varchar), ',''', @table_name, ''',''', @teacher_id, ''')'); print @str_sql EXEC(@str_sql)--课程信息插入到tb_Course DECLARE prj_Cursor SCROLL CURSOR for SELECT NAME FROM sys.columns WHERE [object_id]=OBJECT_ID(@table_name) order by column_id; OPEN prj_Cursor declare @prj varchar(128) FETCH ABSOLUTE 4 FROM prj_Cursor into @prj while (@@fetch_status=0) begin--把实验项目加入到tb_Project表 print(@prj) set @str_sql='insert into tb_Project(project_id,project_name,course_id,project_open) values('''+ right(@course_id,3)+@prj+''','+ ''''+@prj+''','''+@course_id+''','+'0'+')' print @str_sql EXEC(@str_sql)--项目信息插入到tb_Project FETCH next FROM prj_Cursor into @prj end set @str_sql=CONCAT('insert into tb_User(user_name,user_password,full_name) select ', 'SNO', ',substring(sys.fn_sqlvarbasetostr(HASHBYTES(''MD5'',','SNO)),3,32),Sname', ' from ', @table_name, ' where SNO not in(select user_name from tb_User)' ); --如果字段的类型是nvarchar的,md5加密前需要强制转化一下类型CAST([SNO] as varchar(50)) print @str_sql EXEC(@str_sql)--学生账号插入到tb_User set @str_sql='insert into tb_SC(SNO,course_id) select SNO,''' +@course_id+ ''' from '+@table_name print @str_sql EXEC(@str_sql)--选课信息插入到tb_SC go
时间: 2023-08-05 13:09:41 浏览: 125
CREATE_DB_AND_TABLE_add_edit_del-pw.rar_CREATE_DB_AND_TABLE_Tabl
这段代码是用来将面向对象系统分析与设计实验信计201这门课程的信息插入到数据库中,并且创建相应的实验项目和学生账号。其中,先向tb_Course表中插入课程信息,然后通过游标遍历实验项目的字段,将每个实验项目插入到tb_Project表中。接着,将学生账号信息插入到tb_User表中,并将学生选课信息插入到tb_SC表中。
阅读全文