插入触发器(SQL 2005)

时间:2009-11-12 17:16:55

标签: sql-server insert triggers

我有一个临时表(question_desc, ans1, ans2, ans3, ans4, correct_ans, marks),有10个条目。

从这个表中我必须在另外两个表中插入值:

questions (q_id(auto-generated), que_desc, marks)

answers (ans_id(auto_generated), q_id(FK), ans_desc, istrue)

对于questions表中的每个插入,answers表中应该有四个插入,istrue位将设置为1以获得正确答案。

corr_ans 1的问题1类似,有四个条目

(1,1,djhjfj,1),
(2,1,hdjfsh,0),
(3,1,hsssh,0),
(4,1,jfsh,0)

questions表中插入一行后,我需要获取q_id并在插入4个答案记录时使用它。

我尝试使用插入触发器,但它不起作用,因为它是语句级别触发器,而不是行级别。我正在使用SQL 2005.请帮助。

4 个答案:

答案 0 :(得分:0)

[更新。原来的答案被推倒了。]

由于您可以访问工作表和主题信息,这个更改怎么办?我在ExSheet / ExSubject的临时表中添加了一个PK。

(我知道我没有回答您的触发问题,但我们需要先了解问题。)

代码

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

答案 1 :(得分:0)

这是基本想法,尝试将此功能合并到您应用的其余部分。

CREATE PROCEDURE dbo.funnyInsert(
@question_desc varchar(100)
,@ans1 varchar(100)
,@ans2 varchar(100)
,@ans3 varchar(100)
,@ans4 varchar(100)
,@corect_ans varchar(100)
,@mark numeric(4,1)
)
AS
BEGIN
    DECLARE @lastID int

    INSERT INTO dbo.questions(que_desc, mark)
        VALUES(@question_desc, @mark);

    SET @lastID = IDENT_CURRENT('dbo.questions')

    INSERT INTO dbo.answers(q_id,ans_desc, isTrue)
        VALUES(@lastID, @ans1,
        CASE WHEN @ans1 = @corect_ans THEN 1 ELSE 0 END);

    INSERT INTO dbo.answers(q_id,ans_desc, isTrue)
        VALUES(@lastID, @ans2,
        CASE WHEN @ans2 = @corect_ans THEN 1 ELSE 0 END);

    INSERT INTO dbo.answers(q_id,ans_desc, isTrue)
        VALUES(@lastID, @ans3,
        CASE WHEN @ans3 = @corect_ans THEN 1 ELSE 0 END);

    INSERT INTO dbo.answers(q_id,ans_desc, isTrue)
        VALUES(@lastID, @ans4,
        CASE WHEN @ans4 = @corect_ans THEN 1 ELSE 0 END);

END

答案 2 :(得分:0)

我不确定在这种情况下我会使用触发器,因为似乎没有任何条件确定是否将记录添加到问题和答案表中。

在将一条记录添加到临时表中的代码处,我会调用一个存储过程,将7个字段作为参数传递。

存储过程会在问题和答案表中进行原始插入(如果在其他地方需要,也会更新临时表)。

如果问题是跟踪问题中最后插入的记录的ID以便在答案中使用,请查看本文关于确定自动生成的ID的范围:http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/

编辑:如果您决定使用触发器,则可以让触发器从触发器内部调用上述存储过程。

希望这有用。

答案 3 :(得分:0)

您只是创建相对空的答案记录,以便以后可以返回并编辑它们吗?如果是这样,我只会修改你的插入sp来做这样的事情

declare @questions table (qid int identity(1,1), question nvarchar(max))
declare @answers table (aid int identity(1,1), qid int, answer nvarchar(max))


insert into @questions values ('Who?')
insert into @questions values ('What')
insert into @questions values ('When?')
insert into @questions values ('Where?')


insert into @answers (qid)
select qid
from @questions
union all
select qid
from @questions
union all
select qid
from @questions
union all
select qid
from @questions

select *
from @answers

你也可以修改内容,这样你只会插入一些没有任何答案的问题。