SUPERCAT.DEV

Benvenut* sul mio blog

TSQL

ID inserimenti

25-10-2023

Registrare valori multipli degli id degli INSERT

DECLARE @IDs TABLE(ID INT);

IF EXISTS(SELECT [name] FROM tempdb.sys.tables WHERE [name] like '#NOMI%')
BEGIN
	DROP TABLE #NOMI;
END

CREATE TABLE #NOMI (
	Id INT NOT NULL IDENTITY,
	Nome VARCHAR(20)
)

INSERT INTO #NOMI VALUES ('PIPPO'), ('PLUTO')

INSERT #NOMI(Nome) OUTPUT inserted.ID INTO @IDs(ID)
SELECT N'MINNI'
UNION ALL
SELECT N'CLARABELLA';

DECLARE @ULTIMO INT
INSERT INTO #NOMI(Nome)
SELECT N'ZIO PAPERONE'

SELECT @ULTIMO = IDENT_CURRENT('#NOMI')  
SELECT @ULTIMO

SELECT ID FROM @IDs;