[英]Find Last Inserted Record MS SQL SERVER
I applied 12Lac Insert command in Single table , but after some time query terminated , How can I find Last Inserted Record 我在“单个”表中应用了12Lac插入命令,但查询终止了一段时间后,如何找到最后插入的记录
a)Table don't have created Date column a)表未创建日期列
b)Can not apply order by clause because primary key values are manually generated b)由于主键值是手动生成的,因此无法应用order by子句
c)Last() is not buit in fumction in mssql. c)Last()在mssql中不具备功能。
Or any way to find last executed query 或以任何方式查找上次执行的查询
There will be some way but not able to figure out 会有一些方法,但无法弄清楚
Table contain only primary key constrain no other constrain 表仅包含主键约束,没有其他约束
As per comment request here a quick and dirty manual solution, assuming you've got the list of INSERT
statements (or the according data) in the same sequence as the issued INSERT
s. 根据此处的注释请求,假定您已按照与发出的
INSERT
相同的顺序获取了INSERT
语句(或相应数据)列表,这是一种快速而肮脏的手动解决方案。 For this example I assume 1 million records. 对于此示例,我假设有100万条记录。
INSERT ... VALUES (1, ...)
...
INSERT ... VALUES (250000, ...)
...
INSERT ... VALUES (500000, ...)
...
INSERT ... VALUES (750000, ...)
...
INSERT ... VALUES (1000000, ...)
You just have to find the last PK, that has been inserted. 您只需要找到已插入的最后一个PK。 Luckily in this case there is one.
幸运的是,在这种情况下,只有一个。 So you start doing a manual binary search in the table issuing
因此,您开始在发布表的过程中进行手动二进制搜索
SELECT pk FROM myTable WHERE pk = 500000
If you get a row back, you know it got so far. 如果您退回一排,那您就知道了。 Continue checking with
pk = 750000
. 继续检查
pk = 750000
。 Then again, if it is there with pk = 875000
. 如果
pk = 875000
,则再次存在。 If 750000 is not there, then the INSERT
s must have stopped earlier. 如果不存在750000,则
INSERT
必须早些停止。 Then check for pk = 675000
. 然后检查
pk = 675000
。 This process stops in this case after 20 steps. 在这种情况下,此过程将在20个步骤后停止。
It's just plain manual divide and conquer. 这只是简单的手动分而治之。
There is a way. 有一种方法。 Unfortunately you have to do this in advance so it helps you.
不幸的是,您必须提前执行此操作,以帮助您。 So if you have, by any chance the
PRIMARY KEYS
you inserted, still at hand go ahead and delete all rows that have those keys: 因此,如果您有机会插入的
PRIMARY KEYS
仍在手边,请删除所有具有这些键的行:
DELETE FROM tableName WHERE ID IN (id1, id2, ...., idn)
Then you enable Change Data Capture for your database (have the db already selected): 然后,为数据库启用“ 更改数据捕获 ”(已选择数据库):
EXEC sys.sp_cdc_enable_db;
Now you also need to enable Change Data Capture for that table, in an example that I've tried I could just run: 现在,您还需要为该表启用“更改数据捕获”,在我尝试可以运行的示例中:
EXEC sys.sp_cdc_enable_table @source_schema = N'dbo', @source_name = N'tableName', @role_name = null
Now you are almost setup! 现在,您几乎已经准备就绪! You need to look into your system services and verify that
SQL Server Agent
is running for your DBMS, if it does not capturing will not happen. 您需要调查您的系统服务,并验证
SQL Server Agent
是否正在为您的DBMS运行,如果没有捕获则不会发生。
Now when you insert something into your table you can select data changes from a new table called [cdc].[dbo_tableName_CT]
: 现在,当您向表中插入内容时,您可以从名为
[cdc].[dbo_tableName_CT]
的新表中选择数据更改:
SELECT [__$start_lsn]
,[__$end_lsn]
,[__$seqval]
,[__$operation]
,[__$update_mask]
,[ID]
,[Value]
FROM [cdc].[dbo_tableName_CT]
GO
An example output of this looks like this: 一个示例输出如下所示:
you can order by __$seqval
that should give you the order in which the rows were inserted. 您可以
order by __$seqval
,该顺序应该为您提供插入行的顺序。
NOTE: this feature seems not to be present in SQL Server Express
注意:此功能似乎不存在于
SQL Server Express
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.