关于使用 SQL Server (ASP.NET MVC) 插入/更新行的问题

发布于 2024-09-05 16:10:04 字数 378 浏览 10 评论 0 原文

我有一个非常大的表,有很多行,每一行都有每个用户在特定日期的统计信息。显然我没有任何未来的统计数据。因此,要更新我使用的统计信息

UPDATE Stats SET Visits=@val WHERE ... a lot of conditions ... AND Date=@Today

,但是如果该行不存在怎么办?我必须使用

INSERT INTO Stats (...) VALUES (Visits=@val, ..., Date=@Today)

如何检查该行是否存在?与执行 COUNT(*) 有什么不同吗?

如果我用空单元格填充表格,则需要数十万行,占用兆字节并且不存储任何数据。

I have a very big table with a lot of rows, every row has stats for every user for certain days. And obviously I don't have any stats for future. So to update the stats I use

UPDATE Stats SET Visits=@val WHERE ... a lot of conditions ... AND Date=@Today

But what if the row doesn't exist? I'd have to use

INSERT INTO Stats (...) VALUES (Visits=@val, ..., Date=@Today)

How can I check if the row exists or not? Is there any way different from doing the COUNT(*)?

If I fill the table with empty cells, it'd take hundreds of thousands of rows taking megabytes and storing no data.

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

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

发布评论

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

评论(4

久随 2024-09-12 16:10:05

我会尝试更新,然后如果 @@ROWCOUNT = 0 尝试插入

UPDATE Stats SET Visits=@val WHERE ... a lot of conditions ... AND Date=@Today
IF @@ROWCOUNT = 0
   INSERT INTO Stats (...) VALUES (Visits=@val, ..., Date=@Today)

或尝试在 TRY/CATCH 中插入,然后如果失败则更新。

我不会先测试

I would try an UPDATE, then if @@ROWCOUNT = 0 try an INSERT

UPDATE Stats SET Visits=@val WHERE ... a lot of conditions ... AND Date=@Today
IF @@ROWCOUNT = 0
   INSERT INTO Stats (...) VALUES (Visits=@val, ..., Date=@Today)

Or try an INSERT inside a TRY/CATCH and then UPDATE if it fails.

I wouldn't test first

带刺的爱情 2024-09-12 16:10:05

在这些情况下,我倾向于执行以下操作(伪代码,因为我自己就是 DB2 人员):

try:
    INSERT
catch already-exists:
    try:
        UPDATE
    catch anything:
        generate error

如果您更有可能拥有行,则可以按相反的顺序执行此操作

try:
    UPDATE
catch not-there:
    try:
        INSERT
    catch anything:
        generate error

: >永远不要首先在 DBMS 中进行测试,因为数据库可能会在测试和执行之间发生变化(“请求宽恕比寻求许可更好”原则)。

What I tend to do in those cases is just perform the following (pseudo-code since I'm a DB2 man myself):

try:
    INSERT
catch already-exists:
    try:
        UPDATE
    catch anything:
        generate error

You can do it in the opposite order if you're more likely to have rows than not:

try:
    UPDATE
catch not-there:
    try:
        INSERT
    catch anything:
        generate error

You never test first in a DBMS since the database can change underneath you between the test and the execution (the "better to ask forgiveness than seek permission" principle).

薄荷→糖丶微凉 2024-09-12 16:10:05

不幸的是,SQL Server 没有任何类型的“INSERT OR UPDATE”命令(据我所知),就像某些数据库引擎一样。这意味着您必须首先检查该行或按照其他答案的建议捕获错误。希望您的行有一个人工主键,因此您可以首先使用所有 WHERE 条件查询该键,然后,如果返回一行,则执行 UPDATE ... WHERE key = ...,否则执行 INSERT。这样您就不会运行整个查询两次。

Unfortunately SQL Server doesn't have any kind of "INSERT OR UPDATE" command (that I know of), like some DB engines. That means that you do have to check for the row first or catch the error as other answers recommend. Hopefully your row has an artificial primary key, so what you could is first query for that key using all your WHERE conditions and then, if a row is returned do an UPDATE ... WHERE key = ..., otherwise do an INSERT. That way you're not running the entire query twice.

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