如何识别插入语句的最后一行

发布于 2024-08-19 05:47:16 字数 277 浏览 7 评论 0原文

我想知道哪条记录是按以下顺序最后插入到详细表 B 上的...例如,如果我有:

1 row Header Table A
--------------------
1 row Detail Table B
2 row Detail Table B
3 row Detail Table B
4 row Detail Table B (last)

如果插入了 4 行,我想做一些 T-SQL 或运行一个过程...谢谢进步!

使用 SQL Server 2000

I want to know which record is the last inserted on Detail Table B in the following order... for example If I have :

1 row Header Table A
--------------------
1 row Detail Table B
2 row Detail Table B
3 row Detail Table B
4 row Detail Table B (last)

I want to do some T-SQL or run a procedure if the 4 row is inserted... Thanks in advance!

Using SQL-Server 2000

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

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

发布评论

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

评论(5

¢好甜 2024-08-26 05:47:16

根据定义,SQL 表没有隐含的顺序。因此,您无法根据详细信息表中的行顺序可靠地确定这一点。您将需要添加某种数字列,其中包含插入行的顺序。

SQL tables, by definition, have no implied ordering. Therefore you cannot reliably determine this based on the ordering of rows in your Detail table. You will need to add some sort of numeric column that contains the order in which you insert your rows.

筱果果 2024-08-26 05:47:16

按照上面的建议查看有关触发器的一些帮助,然后也许查看触发器创建和使用的特殊插入和删除表。

触发器语句中使用了两个特殊的表:deleted表和inserted表。 Microsoft® SQL Server 自动创建和管理这些表。您可以使用这些临时的、驻留在内存中的表来测试某些数据修改的效果并设置触发操作的条件;但是,您不能直接更改表中的数据。

我相信通过触发器插入或删除的每一行都是通过这些表完成的。

当您进行插入时,您可能会查询插入表以找到插入其中的最后一行。

Have a look at some help on triggers as advised above and then maybe look at the special Inserted and Deleted tables created and used by triggers.

Two special tables are used in trigger statements: the deleted table and the inserted table. Microsoft® SQL Server automatically creates and manages these tables. You can use these temporary, memory-resident tables to test the effects of certain data modifications and to set conditions for trigger actions; however, you cannot alter the data in the tables directly.

Every row that is inserted or deleted via a trigger is done via these tables i believe.

As you do your insert you could possibly query the insert table to find the last row that was pushed through it.

倚栏听风 2024-08-26 05:47:16

该表是否有标识列(例如“TableBId”)?这是兰迪·明德在他的回答中谈到的数字列。如果确实如此,那么只需使用 ORDER BY 函数就非常简单:

SELECT TOP 1 * FROM TableB ORDER BY Id DESC

如果您没有标识列,那么您可以通过创建一个新列 (Id)、将其设置为主键并将其标识规范设置为打开来添加一个标识列。

如果您想在将新项目插入表后运行一些 sql,则可以使用插入触发器。

可以阅读此处的触发器或查看下面的 astander 链接:)

Does that table have an identity column (E.g., "TableBId")? This is the numeric column Randy Minder talks about in his answer. If it does then it is pretty easy just use the ORDER BY function:

SELECT TOP 1 * FROM TableB ORDER BY Id DESC

If you do not have an identity column then you can add one by creating a new column (Id), setting it as the primary key and setting it's Identity specification on.

If you want to run some sql after a new item is inserted into a table you can use an insert trigger.

Can read about triggers here or look at astander's links below :)

小鸟爱天空丶 2024-08-26 05:47:16

使用 IDENTITY 来确定最后插入的记录可能会产生很大的误导。如果某些记录已被删除,并且您已重置 IDENTITY 起始值,该怎么办?

例如,您可能拥有 1 - 10000 条记录,但删除了 1000-9000 条记录,并且您决定重置您的身份以从 1000 重新开始?

另一方面,触发因素可能会起作用,但你需要仔细权衡后果。这可能会给您的系统增加大量负载。

你如何插入记录?通过应用程序?

我建议考虑添加一个时间戳列(或者甚至只是一个具有默认值 GETDATE() 的常规 DATETIME 列)。这可以帮助确定哪条记录最后插入到您的事务中。例如:

-- begin your transaction 

-- insert statements here 

SELECT id
FROM TableB
WHERE timestampcol = (SELECT MAX(timestampcol) FROM TableB)

-- run your sproc based on the last record inserted

-- commit or rollback your transaction

Using the IDENTITY to determine which record was inserted last could be very misleading. What if some records have been deleted, and you've reset your IDENTITY start value?

For example, you could potentially have records 1 - 10000, but delete from 1000-9000, and you decide to reset your identity to start again at 1000?

Triggers, on the other hand, could work but you need to weigh the consequences carefully. This could add a lot of load to your system.

How are you inserting the records? Through an application?

I will suggest looking at adding a timestamp column (or even just a regular DATETIME column with default value GETDATE()). This can aid in determining which record was inserted last in your transaction. For example:

-- begin your transaction 

-- insert statements here 

SELECT id
FROM TableB
WHERE timestampcol = (SELECT MAX(timestampcol) FROM TableB)

-- run your sproc based on the last record inserted

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