简体   繁体   English

根据表中最近创建的行查找值(SQL Server)

[英]Find a value based on the most recently created row in a table (SQL Server)

This potentially might be too large of a question for a complete solution, and I've got a bit of a strange set up. 对于一个完整的解决方案来说,这可能是一个太大的问题,而我的设置有些奇怪。 I'm using HP OO to create a text-based RPG just to practice getting used to database design on this platform. 我正在使用HP OO创建基于文本的RPG,只是为了练习适应该平台上的数据库设计。

So it's basically a flow script that runs once. 因此,它基本上是只运行一次的流脚本。 When the script starts, a player (user) is created, and then a character is created. 脚本启动时,将创建一个播放器(用户),然后创建一个角色。 The player inputs a name for its character, and this is stored in the character table. 玩家输入其角色的名称,该名称存储在character表中。 I then call that character name with SELECT name FROM character WHERE character.character_id=x . 然后,我用SELECT name FROM character WHERE character.character_id=x调用该字符名称。 How can I retrieve the name from the correct (most recently created) character. 如何从正确的(最近创建的)字符中检索名称。 The character_id is an auto-incrementing identity column. character_id是一个自动递增的标识列。

There's nothing guaranteeing that the highest value in an identity column is the most recently created record. 无法保证标识列中的最高值是最近创建的记录。 You should add a date_created column to your table and give it a default value of the current date and time ( current_timestamp for a datetime2 field). 您应该在表中添加一个date_created列,并为其提供当前日期和时间的默认值( datetime2字段的current_timestamp )。 That actually does what you want. 那实际上就是您想要的。

OK, your question changed a bit and, Tab's comment here is also correct. 好的,您的问题有所改变,Tab在这里的评论也是正确的。 If you want to insert and get the identity inserted back, you should follow the advice here that he linked. 如果要插入并重新插入身份,则应遵循他链接的建议

However, if you want to be able to determine the order of creation -- which is what you originally asked -- then you should use a date_created field. 但是,如果您想确定创建顺序(这就是您最初要求的顺序),则应使用date_created字段。 It's possible to get around IDENTITY and insert any value you want, and things like UPDATEs and DELETEs can change things as well. 可以避开IDENTITY并插入所需的任何值,并且UPDATE和DELETE之类的东西也可以更改。 Essentially, it's a bad idea to assign meaning to a record's value of an IDENTITY column relative to other records in the table (ie, this was created before or after these other records) because you can actually get around that. 从本质上讲,相对于表中的其他记录(即,在其他记录之前或之后创建),将含义分配给IDENTITY列的记录值是个不好的主意,因为您实际上可以解决这个问题。

Personally, I would either use the OUTPUT clause to have my INSERTs send the ID back: 就个人而言,我可以使用OUTPUT子句让我的INSERT发送回ID:

INSERT INTO Character (...)
OUTPUT INSERTED.Id
VALUES (....);

Or I'd reuse the same connection and return the SCOPE_IDENTITY() . 或者,我将重用相同的连接并返回SCOPE_IDENTITY()

INSERT INTO Character (...)
VALUES (....);

SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY];
SELECT row FROM table WHERE id=(
    SELECT max(id) FROM table
    )
this should work

Make sure the id is unique (auto increments? great!) 确保ID是唯一的(自动递增?太好了!)

When you insert into the character table, you should rely on Scope_Identity() or @@Identity (depending on how your database is setup. Read more about it here ) to return the unique ID (the auto incremented ID) from the table, and use that moving forward. 当您插入字符表时,应该依靠Scope_Identity()@@Identity (取决于数据库的设置方式。 在此处了解更多信息)从表中返回唯一ID(自动递增ID),并且利用前进的脚步。

Doing it any other way opens you up to data integrity issues. 以任何其他方式进行操作都会使您面临数据完整性问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM