0

我有这样的 Exam_Result 表和 Subject_Info 表....

1] Exam_Result table....

Resultid         numeric(18, 0)   Unchecked
Enroll_Number    varchar(50)      Checked
Student_Name     varchar(100)     Checked
Course_Id        varchar(50)      Checked
Semester         varchar(50)      Checked
Subject_Id       varchar(50)      Checked
MarksObtained    numeric(18, 0)   Checked
Exam_Type        varchar(50)      Checked

2] Subject_Info table....

Subject_Id       varchar(20)    Unchecked
Subject_Name     varchar(50)    Checked
Semester         varchar(10)    Checked
Course_Id        varchar(20)    Checked
Course_Type      varchar(20)    Checked

我想一次插入每个子标记....例如,想要输入具有 11111、Course_Id = CIV 和 Sem = 1 的唯一学生 Enroll_No 的每个子标记....

Enroll_Number 11111
Course_Id     CIV
Semester      1

这是主题...我想输入这样的标记...

Subject_Id   MarksObtained
EM           23
SM           25
CL           30

并且这个结果必须存储到 Exam_Result 表中......

注意:每个学期的科目都不是固定的,它可能会根据学校而改变......所以我希望它有动态的解决方案......

你能帮我解决这个问题吗..??? 或任何其他解决方案给我.... thnks提前

4

2 回答 2

0

我认为你可以用循环来做到这一点,这是一个例子

将@ID 声明为 VARCHAR(25)

SET @ID = '1,2,3,4,'

选择 @ID 作为 TOTAL_ID

声明@DELIMITER CHAR(1)
设置@DELIMITER = ','

将 @CURRENTID 声明为 INT

WHILE(CHARINDEX(@DELIMITER,@ID,0) <> 0)

开始选择@CURRENTID
= RTRIM(LTRIM(SUBSTRING(@ID,1,CHARINDEX(@DELIMITER,@ID,0 )-1 ))),

     @ID = RTRIM(LTRIM(SUBSTRING(@ID,CHARINDEX(@DELIMITER,@ID,0)+1,LEN(@ID))))

选择 @CURRENTID 作为 CURRENTID

选择 @ID 作为 REMAINING_ID

结尾

根据您的要求,我已经为您完成了循环

将@IDSCORE 声明为 VARCHAR(MAX)

SET @IDSCORE = 'EM,23,;SM,25,;CL,30,;'

声明 @MAINDELIMITER CHAR(1)

设置@MAINDELIMITER = ';'

声明@DELIMITER CHAR(1)

SET @DELIMITER = ','

声明 @CURRENTIDSCORE VARCHAR(MAX)

将@SUBJECTID 声明为 VARCHAR(10)

将@SCORE 声明为 VARCHAR(10)

如果(@IDSCORE 不为空)

开始

WHILE(CHARINDEX(@MAINDELIMITER,@IDSCORE,0) <> 0)

开始

  PRINT @IDSCORE

  SELECT @CURRENTIDSCORE = RTRIM(LTRIM(SUBSTRING(@IDSCORE,1,CHARINDEX(@MAINDELIMITER,@IDSCORE,0 )-1 ))),
      @IDSCORE = RTRIM(LTRIM(SUBSTRING(@IDSCORE,CHARINDEX(@MAINDELIMITER,@IDSCORE,0)+1,LEN(@IDSCORE))))   
  SELECT @SUBJECTID = RTRIM(LTRIM(SUBSTRING(@CURRENTIDSCORE,1,CHARINDEX(@DELIMITER,@CURRENTIDSCORE,0 )-1 ))),
      @CURRENTIDSCORE = RTRIM(LTRIM(SUBSTRING(@CURRENTIDSCORE,CHARINDEX(@DELIMITER,@CURRENTIDSCORE,0)+1,LEN(@CURRENTIDSCORE))))
  SELECT @SCORE = RTRIM(LTRIM(SUBSTRING(@CURRENTIDSCORE,1,CHARINDEX(@DELIMITER,@CURRENTIDSCORE,0 )-1 ))),
      @CURRENTIDSCORE = RTRIM(LTRIM(SUBSTRING(@CURRENTIDSCORE,CHARINDEX(@DELIMITER,@CURRENTIDSCORE,0)+1,LEN(@CURRENTIDSCORE))))

  PRINT @CURRENTIDSCORE   

  PRINT @SUBJECTID

  PRINT @SCORE

结尾

结尾

于 2012-04-27T05:44:43.563 回答
0

使用 SQL Server 2005,您可以使用 INSERT... UNION ALL hack 在单个语句中插入多行:

INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5

如此处更详细地描述:

http://blog.sqlauthority.com/2007/06/08/sql-server-insert-multiple-records-using-one-insert-statement-use-of-union-all/

顺便说一句,在 SQL Server 2008 中,TSQL 原生支持插入多行:

INSERT INTO Customers (Name, City, Phone)
VALUES
    ('Customer #1', 'Jerusalem', '2343245'),
    ('Customer #2', 'Tel Aviv', '0987345'),
    ('Customer #3', 'Haifa', '275466')

http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/12/05/sql-server-2008-t-sql-insert-multiple-rows.aspx

编辑:

根据要求,要在 C# 中构建这样的字符串,您可以执行以下操作:

// create some dummy data
var data = new List<Tuple<string, int>>
{
    new Tuple<string, int>("First", 1),
    new Tuple<string, int>("Second", 2),
    new Tuple<string, int>("Third", 3)
};

// create the [UNION ALL]...SELECT clauses
var selectClauses = String.Join(
    " UNION ALL ", 
    data.Select(t => String.Format("SELECT '{0}', {1}", t.Item1, t.Item2)));

// build our entire SQL string
// this results in the string:
// "INSERT INTO MyTable (FirstCol, SecondCol) SELECT 'First', 1 UNION ALL SELECT 'Second', 2 UNION ALL SELECT 'Third', 3"
var sql = "INSERT INTO MyTable (FirstCol, SecondCol) " + selectClauses;

// now execute this string against your database

如果使用这种方法,您应该注意转义引号、SQL 注入等。这只是为了让你开始。

于 2012-04-27T04:48:01.257 回答