很久没有写文章了,今天做项目时温习了一下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:

  1. asp.net中恢复数据库的问题 项目中使用到了恢复数据库的功能。直接使用RESTORE DATABASE table FROM DISK='Path';结果出现了不少问题。 尚未备份数据库的日志尾部 异常详细信息: System.Data.SqlClient.SqlException: 尚未备份数据库 "HopeLoan2009"...

以上关联文章由 Yet Another Related Posts Plugin 提供支持。