很久没有写文章了,今天做项目时温习了一下SQL。现在有三张表S_action_info、E_grade_info、S_action_grade,其中第三张表为中间表,用于保存前两张表多对多关系。
数据结构图如下:

其它字段可不必考虑,重要的有前两张表的主键,和第三张表中相对应的外键。前两张表中已有部分数据,需要对第三张表进行一些测试数据的插入。于是SQL语句如下:
/* *若建立存储过程 IF EXISTS(SELECT name FROM sysobjects WHERE name='InsertDataIntoS_action_grade' AND TYPE='p') DROP PROCEDURE InsertDataIntoS_action_grade GO CREATE PROCEDURE InsertDataIntoS_action_grade @actionCount int--1表数据记录总数 @gradeCount int--2表数据记录总数 @actionIndex int--1表起始索引 @gradeIndex int--2表起始索引 AS*/ --TRUNCATE TABLE S_action_grade 如有必要,清空已有数据 declare @actionCount int--1表数据记录总数 declare @gradeCount int--2表数据记录总数 declare @actionIndex int--1表起始索引 declare @gradeIndex int--2表起始索引 set @actionCount=(select count(*) from s_action_info) set @gradeCount=(select count(*) from e_grade_info) set @actionIndex=1 while @actionIndex<=@actionCount begin set @gradeIndex=1 while @gradeIndex<=@gradeCount begin insert into s_action_grade(action_id,grade_id,day_process_money,total_process_money,tip_type) values(@actionIndex,@gradeIndex,100000,10000000,'1') set @gradeIndex=@gradeIndex+1 end set @actionIndex=@actionIndex+1 end go |
执行后结果:

Related posts:
以上关联文章由 Yet Another Related Posts Plugin 提供支持。
One Response
onecitychat
04|七|2010 1很喜欢你的博客
Leave a reply