sql server 非自动增长id,怎么返回刚插入的数据的id
我的主键id是用的newid() 所以现在插入一条数据成功后然后返回这条数据的id
可以在sql里面写:
declare @id nvarchar(50)
set @id=(select NEWID())
--insert into table valuse(@id)
print @id
也可以在程序里面定义:
定义一个id,插入成功就把这个id返回调用方法,如果失败就返回空。希望能够帮助到您
我的主键id是用的newid() 所以现在插入一条数据成功后然后返回这条数据的id
select max newid from table
使用OUTPUT 输出 刚插入的ID
CREATE TABLE test(id UNIQUEIDENTIFIER DEFAULT NEWID(),NAME VARCHAR(100))
insert into test (name ) output inserted.id select '1111'
@@identity
使用OUTPUT 输出 刚插入的ID
CREATE TABLE test(id UNIQUEIDENTIFIER DEFAULT NEWID(),NAME VARCHAR(100))
insert into test (name ) output inserted.id select '1111'
select id from table where create_time=currenttime
select @@IDENTITY id --放回字段名称
insert into user_student( ......)
values(...... )
楼上方法也是可行的,不过要保证create_time 必须唯一
最简单方式写个存储过程 执行成功返回ID 插入失败回滚 返回null
declare @max_id as bigint ;
select @max_id=ISNULL(MAX(id), 0) from tablename