[Updated. Original answer pushed down.]
Since you have access to the sheet and subject information, what about this change? I added a PK in the temp table of ExSheet/ExSubject.
(I understand I'm not answering your trigger question, but we need to understand the problem first.)
Code
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
Result
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
I'm quite sure this is not the solution you're looking for, but we'll need help getting this figured out.
Please look over this code and explain what needs to change to work for your solution.
Code
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
Result
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