[Обновлено. Оригинальный ответ сдвинут вниз.]
Поскольку у вас есть доступ к листу и информации о предмете, как насчет этого изменения? Я добавил PK во временную таблицу ExSheet / ExSubject.
(Я понимаю, что не отвечаю на ваш вопрос о триггере, но сначала нам нужно понять проблему.)
Код
SET NOCOUNT ON
USE tempdb
GO
IF OBJECT_ID('tempdb..#q1') IS NOT NULL
DROP TABLE #q1
IF OBJECT_ID('tempdb..#questions') IS NOT NULL
DROP TABLE #questions
IF OBJECT_ID('tempdb..#answers') IS NOT NULL
DROP TABLE #answers
CREATE TABLE #q1 (
ExSheet int,
ExSubject varchar(10),
question_desc varchar(70),
ans1 varchar(20),
ans2 varchar(20),
ans3 varchar(20),
ans4 varchar(20),
correct_ans varchar(20),
marks varchar(20),
CONSTRAINT PK_#q1 PRIMARY KEY (ExSheet, ExSubject)
)
CREATE TABLE #questions (
q_id int identity,
que_desc varchar(70),
marks varchar(20)
)
CREATE TABLE #answers (
ans_id int identity,
q_id int,
ans_desc varchar(20),
istrue bit
)
INSERT INTO #q1 VALUES (
1, -- ExSheet
'Subject', -- ExSubject
'Which of the following SQL statements selects the string ''Success''?',
'ans1',
'ans2',
'ans3',
'ans4',
'ans1',
'marks'
)
DECLARE @q_id int
INSERT INTO #questions (
que_desc,
marks
)
SELECT
question_desc,
marks
FROM #q1
WHERE ExSheet = 1
AND ExSubject = 'Subject'
SELECT @q_id = SCOPE_IDENTITY()
-- ans1
INSERT INTO #answers (
q_id,
ans_desc,
istrue
)
SELECT
@q_id,
ans1,
CASE WHEN ans1 = correct_ans THEN 1 ELSE 0 END
FROM #q1
WHERE ExSheet = 1
AND ExSubject = 'Subject'
-- ans2
INSERT INTO #answers (
q_id,
ans_desc,
istrue
)
SELECT
@q_id,
ans2,
CASE WHEN ans2 = correct_ans THEN 1 ELSE 0 END
FROM #q1
WHERE ExSheet = 1
AND ExSubject = 'Subject'
-- ans3
INSERT INTO #answers (
q_id,
ans_desc,
istrue
)
SELECT
@q_id,
ans3,
CASE WHEN ans3 = correct_ans THEN 1 ELSE 0 END
FROM #q1
WHERE ExSheet = 1
AND ExSubject = 'Subject'
-- ans4
INSERT INTO #answers (
q_id,
ans_desc,
istrue
)
SELECT
@q_id,
ans4,
CASE WHEN ans4 = correct_ans THEN 1 ELSE 0 END
FROM #q1
WHERE ExSheet = 1
AND ExSubject = 'Subject'
SELECT * FROM #questions
SELECT * FROM #answers
Результат
q_id que_desc marks
---- ------------------------------------------------------------------- -----
1 Which of the following SQL statements selects the string 'Success'? marks
ans_id q_id ans_desc istrue
------ ---- -------- ------
1 1 ans1 1
2 1 ans2 0
3 1 ans3 0
4 1 ans4 0
Я совершенно уверен, что это не то решение, которое вы ищете, но нам понадобится помощь, чтобы выяснить это.
Пожалуйста, просмотрите этот код и объясните, что нужно изменить, чтобы оно работало на ваше решение.
код
SET NOCOUNT ON
USE tempdb
GO
IF OBJECT_ID('tempdb..#q1') IS NOT NULL
DROP TABLE #q1
IF OBJECT_ID('tempdb..#questions') IS NOT NULL
DROP TABLE #questions
IF OBJECT_ID('tempdb..#answers') IS NOT NULL
DROP TABLE #answers
CREATE TABLE #q1 (
question_desc varchar(20),
ans1 varchar(20),
ans2 varchar(20),
ans3 varchar(20),
ans4 varchar(20),
correct_ans varchar(20),
marks varchar(20)
)
CREATE TABLE #questions (
q_id int identity,
que_desc varchar(20),
marks varchar(20)
)
CREATE TABLE #answers (
ans_id int identity,
q_id int,
ans_desc varchar(20),
istrue bit
)
INSERT INTO #q1 VALUES ('Question 01', 'ans1', 'ans2', 'ans3', 'ans4', 'ans1', 'marks')
DECLARE @q_id int
INSERT INTO #questions (
que_desc,
marks
)
SELECT
question_desc,
marks
FROM #q1
WHERE question_desc = 'Question 01'
SELECT @q_id = SCOPE_IDENTITY()
-- ans1
INSERT INTO #answers (
q_id,
ans_desc,
istrue
)
SELECT
@q_id,
ans1,
CASE WHEN ans1 = correct_ans THEN 1 ELSE 0 END
FROM #q1
WHERE question_desc = 'Question 01'
-- ans2
INSERT INTO #answers (
q_id,
ans_desc,
istrue
)
SELECT
@q_id,
ans2,
CASE WHEN ans2 = correct_ans THEN 1 ELSE 0 END
FROM #q1
WHERE question_desc = 'Question 01'
-- ans3
INSERT INTO #answers (
q_id,
ans_desc,
istrue
)
SELECT
@q_id,
ans3,
CASE WHEN ans3 = correct_ans THEN 1 ELSE 0 END
FROM #q1
WHERE question_desc = 'Question 01'
-- ans4
INSERT INTO #answers (
q_id,
ans_desc,
istrue
)
SELECT
@q_id,
ans4,
CASE WHEN ans4 = correct_ans THEN 1 ELSE 0 END
FROM #q1
WHERE question_desc = 'Question 01'
SELECT * FROM #questions
SELECT * FROM #answers
Результат
q_id que_desc marks
---- ----------- -----
1 Question 01 marks
ans_id q_id ans_desc istrue
------ ---- -------- ------
1 1 ans1 1
2 1 ans2 0
3 1 ans3 0
4 1 ans4 0