sql server 连接表值一次上传保存四次

发布于 2024-11-05 18:37:46 字数 1048 浏览 0 评论 0原文

我将 csv 文件上传到 sql server 2008 中的虚拟表(csv_upload)。我必须使用触发器将数据分发到下面的表;

Questions          Answers       Test              QAT
---------         ---------     ------            -----
Questid           Ansid          Testid           Questid
Question          Answers        Testname         Ansid
                                                  Testid
                                                  id
                                                  StdUsername

触发代码;

 INSERT INTO tbl_answers ( Answer)
select  DISTINCT Answer  
from tbl_csv_upload


INSERT INTO tbl_questions ( Question )
select DISTINCT Question 
from tbl_csv_upload 

INSERT INTO tbl_taqa (StdUsername,questid, ansid , testid ) 
 SELECT  StdUsername ,q.quest_id,a.ans_id,t.test_id
  FROM csv_upload c, questions q, answers a, test t
WHERE c.Question = q.Question
AND c.Answer = a.Answer 
AND t.test_id = IDENT_CURRENT('test')

该触发器对于第一次从 asp.net 应用程序上传效果很好,但在第二次上传时,它会将数据保存在 QAT 表中 4 次,而不是其他表中。 请我紧急帮助。

I uploaded a csv file to a dummy table(csv_upload) in sql server 2008. I have to distribute the data to the tables below using a trigger;

Questions          Answers       Test              QAT
---------         ---------     ------            -----
Questid           Ansid          Testid           Questid
Question          Answers        Testname         Ansid
                                                  Testid
                                                  id
                                                  StdUsername

The trigger code;

 INSERT INTO tbl_answers ( Answer)
select  DISTINCT Answer  
from tbl_csv_upload


INSERT INTO tbl_questions ( Question )
select DISTINCT Question 
from tbl_csv_upload 

INSERT INTO tbl_taqa (StdUsername,questid, ansid , testid ) 
 SELECT  StdUsername ,q.quest_id,a.ans_id,t.test_id
  FROM csv_upload c, questions q, answers a, test t
WHERE c.Question = q.Question
AND c.Answer = a.Answer 
AND t.test_id = IDENT_CURRENT('test')

This trigger worked well for the first upload from an asp.net application but on second upload it saves the data 4 time in the QAT table instead of ones but other tables are ok.
Pls i need urgent help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2024-11-12 18:37:46

您的数据中可能存在重复的问题和答案。 DISTINCT 会删除上传表中当前存在的重复项,但下次调用过程时,它们会再次插入,因为它没有考虑表中已存在的值。

使用它来插入答案:

INSERT
INTO    answers (answer)
SELECT  answer
FROM    tbl_csv_upload
EXCEPT
SELECT  answer
FROM    answers

以及类似的问题查询。

Probably, you have duplicates of questions and answers in your data. DISTINCT gets rid of duplicates currently present in the upload table, but the next time your are calling your proc they get inserted again since it does not take into account values already present in the table.

Use this to insert the answers:

INSERT
INTO    answers (answer)
SELECT  answer
FROM    tbl_csv_upload
EXCEPT
SELECT  answer
FROM    answers

and a similar query for questions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文