刚刚插入数据库的帖子,如何立即得到它的自增ID ACCESS的办法,和SQL2000的办法,都告诉我。多谢了
解决方案 »
- Ispostback的问题
- dataview.RowFilter & dt.select() 的问题,无限送分
- 求高人指点如何快速的掌握EXT
- [100分求解]A page can have only one server-side Form tag. 异常
- |M| 在ascx中读取aspx中的数据用什么方法比较好
- 为什么我的IE看不到任何网页图标了?
- 想问下刚毕业的学生在公司做ASP.NET,公司一般叫你干什么的!!
- 请教关于下载之后中文不能正常显示的问题
- 为什么我这样删除起不到作用啊!
- 急啊,没写过这个,谁能给我一个操作临时记录,而创建xml文件,并对其进行增加,修改,删除的完整的例子?
- 我为什么引用不了System.Windows.Forms???
- DropDowmList取值?
select @@IdentityAccess:
Access中好像没有类似的函数,似乎只能使用 fluxayxxx(阿茂) 的方法
select max(id) from table
http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B815629c# version:
http://support.microsoft.com/kb/816112/EN-US/
就是要找的id
select max(id) from table
如果有许多人同时操作呢??
效率低了? 小弟不明白,初学望指教。
只是在Select时,一定要先锁住数据库,读完后,再解锁
只是在Select时,一定要先锁住数据库,读完后,再解锁----------------------------------------------------------
请问怎么先锁住数据库??
Select Top 1 [ID] From Tables Where SessionID=MySessionID Order By 1 Desc
SELECT @@IDENTITY --返回插入到当前会话中任何作用域内的最后一个 IDENTITY 列值
SELECT IDENT_CURRENT('TbName')--不受作用域和会话的限制,而受限于指定的表。IDENT_CURRENT 返回为任何会话和作用域中的特定表所生成的值。一个作用域就是一个模块——存储过程、触发器、函数或批处理。因此,如果两个语句处于同一个存储过程、函数或批处理中,则它们位于相同的作用域中。
参考的例子如下:USE pubs
DROP TABLE t6
DROP TABLE t7GO
CREATE TABLE t6(id int IDENTITY)
CREATE TABLE t7(id int IDENTITY(100,1))
GO
CREATE TRIGGER t6ins ON t6 FOR INSERT
AS
BEGIN
INSERT t7 DEFAULT VALUES
SELECT @@IDENTITY as [@@IDENTITY]
SELECT SCOPE_IDENTITY() as [SCOPE_IDENTITY]
END
GO
--end of trigger definitionSELECT * FROM t6
--id is empty.SELECT * FROM t7
--id is empty.
--Do the following in Session 1
INSERT t6 DEFAULT VALUES
SELECT @@IDENTITY
/*Returns the value 100, which was inserted by the trigger.*/SELECT SCOPE_IDENTITY()
/* Returns the value 1, which was inserted by the
INSERT stmt 2 statements before this query.*/return
SELECT IDENT_CURRENT('t7')
/* Returns value inserted into t7, i.e. in the trigger.*/SELECT IDENT_CURRENT('t6')
/* Returns value inserted into t6, which was the INSERT statement 4 stmts before this query.*/-- Do the following in Session 2
SELECT @@IDENTITY
/* Returns NULL since there has been no INSERT action
so far in this session.*/SELECT SCOPE_IDENTITY()
/* Returns NULL since there has been no INSERT action
so far in this scope in this session.*/SELECT IDENT_CURRENT('t7')
/* Returns the last value inserted into t7.*/
总结:
对于马上使用的刚才插入的新记录ID用SCOPE_IDENTITY()是最合适的;
对于想要得到一系列的操作中最后得到的那个自增的ID最好用@@IDENTITY;
对于想要得到一个表中的最后一个插入操作所产生的ID的最好用IDENT_CURRENT('TBName')
create proc aaaaa
(
@getid int output
)
as
insert into table () values ()
set @getid=@@identity
用一个表保存id