sql server 连接表值一次上传保存四次
我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的数据中可能存在重复的问题和答案。
DISTINCT
会删除上传表中当前存在的重复项,但下次调用过程时,它们会再次插入,因为它没有考虑表中已存在的值。使用它来插入答案:
以及类似的
问题
查询。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:
and a similar query for
questions
.