declare @temp int insert into table(name) set @temp =@@IDENTITY --=@@IDENTITY 返回最后插入的标识值
IF OBJECT_ID('tempdb..#temp', 'u') IS NOT NULL DROP TABLE #temp GO CREATE TABLE #temp ( id INT, --#1.如果不是自增列 [name] NVARCHAR(10) ) GO --SQL: DECLARE @table TABLE(id int) declare @temp INTinsert into #temp(id, name) OUTPUT inserted.id INTO @table values(2, 'test')--#2.如果是自增列 SET @temp = SCOPE_IDENTITY()SELECT * FROM @table --RESULT: /* id 2 */
CREATE PROC TEST ASdeclare @temp intinsert into table(name) SELECT @temp = @@IDENTITYRETURN @temp
declare @temp int insert into table(name) set @temp =@@IDENTITY
insert into table(name)
set @temp =@@IDENTITY
--=@@IDENTITY 返回最后插入的标识值
DROP TABLE #temp
GO
CREATE TABLE #temp
(
id INT, --#1.如果不是自增列
[name] NVARCHAR(10)
)
GO
--SQL:
DECLARE @table TABLE(id int)
declare @temp INTinsert into #temp(id, name)
OUTPUT inserted.id
INTO @table
values(2, 'test')--#2.如果是自增列
SET @temp = SCOPE_IDENTITY()SELECT * FROM @table
--RESULT:
/*
id
2
*/
ASdeclare @temp intinsert into table(name) SELECT @temp = @@IDENTITYRETURN @temp
declare @temp int
insert into table(name)
set @temp =@@IDENTITY
SCOPE_IDENTITY()去查查这两个的用法