Is is possible to get new values for Id (IDENTITY) before inserting data in a table?

你。 提交于 2019-12-19 03:36:46

问题


Is is possible to get new values for Id (IDENTITY) before inserting data in a table ?

Is is possible to write something like that :

INSERT INTO Table1
SELECT *GET_NEW_IDENTITY*, Field1, Field2 FROM Table2

I need the values of Id because I want to insert data in Table1 and, just after, insert data in another table which has a foreign key linked to Table1 (with Id)


回答1:


IDENT_CURRENT. Returns the last identity value generated for a specified table or view. The last identity value generated can be for any session and any scope.

SCOPE_IDENTITY. Returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch.

OUTPUT. Returns information from, or expressions based on, each row affected by an INSERT, UPDATE, DELETE, or MERGE statement. [...] The OUTPUT clause may be useful to retrieve the value of identity or computed columns after an INSERT or UPDATE operation.




回答2:


you can also have the insert statement return the newly inserted value for later use. for example

create table demo( Id int identity primary key, data varchar(10))
go
insert into demo(data) output inserted.Id values('something')



回答3:


No, because it is the act of adding a row which creates the new identity value.

To do what you want,

SELECT newid = @@identity FROM table

just after the INSERT




回答4:


Why would you need to get the identity value before doing the insert? Just do the insert to Table2 returning SCOPE_IDENTITY() and then use the resulting Id value for your insert to Table1.




回答5:


This is just fast demo. You can use new ID for insert for update, insert into another table, query, etc. in another way. Hoping I did not insert errors into script during formatting, editing post

-- run [1] before this script once to have environment

--create temporary table once if not dropped after 
-- really only ID field is needed, the others are for illustration
create table #temp_id (Id int, d1 int, d2 int)

select * from Table2;-- this is read-only, filled once here source  
select * from Table1;--interesting for following runs 

insert into Table1 
  OUTPUT INSERTED.id 
  -- really only ID is needed, the rest is for illustration
    , inserted.d1, inserted.d2 INTO #temp_id   
select field1, field2,  null-- null to be merged later
-- or inserted/updated into another table 
  from Table2;

select * from Table1; 
select * from #temp_id; 


MERGE Table1 AS TARGET 
   USING #temp_id AS SOURCE
      ON (TARGET.id = SOURCE.id) 
   WHEN MATCHED 
 --AND OR  are redundant if Table1.ID is PK    
   THEN 
     UPDATE SET TARGET.IDnew = SOURCE.id;


select * from Table1;


--drop table  #temp_id
--drop table  table1
--drop table  table2

[1]
Reproducing the tables from question and filling with data

create table Table1( Id int identity primary key, d1 int, d2 int, IDnew int)
create table Table2( field1 int, field2 int)
insert into table2 values(111,222)
insert into table2 values(333,444)



回答6:


IDENT_CURRENT('tableName') returns the current value of the identity for the given table. The identity value that will be assigned on Insert will be IDENT_CURRENT('tableName') + IDENT_INCR('tableName').

SELECT IDENT_CURRENT('tableName') + IDENT_INCR('tableName')


来源:https://stackoverflow.com/questions/3923950/is-is-possible-to-get-new-values-for-id-identity-before-inserting-data-in-a-ta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!