Funzione di recupero di valori di identificazione in SQL SERVER 2000 In SQL Server 2000, la colonna identità è definita da IDENTITY, e quanto segue è correlato all'ottenimento del valore identità dell'ultimo record inserito
Un'illustrazione di esempio della funzione di off
In SQL Server puoi usare SCOPE_IDENTITY(), @@IDENTITY, IDENT_CURRENT() per ottenere il valore dell'ultimo record inserito, e la differenza tra loro è: SCOPE_IDENTITY() restituisce l'ultimo valore IDENTITY inserito all'interno della colonna IDENTITY nello stesso ambito. Un scope è un modulo - stored procedure, trigger, funzioni
Numeri o lotti. Pertanto, se due istruzioni sono nella stessa stored procedure, funzione o batch, sono nello stesso ambito. @@IDENTITY restituisce l'ultimo valore di identificazione generato in tutte le tabelle della sessione corrente IDENT_CURRENT() restituisce l'ultimo valore di identificazione generato per la tabella specificata in qualsiasi sessione e in qualsiasi ambito Ecco un esempio per illustrare le loro differenze
-- a) Codice di esempio -- =========================================== -- Creare un tavolo di test -- =========================================== USE tempdb VAI
CREATE TABLE t1(id int IDENTITY,col int) INSERIRE T1 SELEZIONA 1 UNION ALL SELECT 2 CREATE TABLE T2(id int IDENTITY,col int) VAI
CREA TR_insert_t2 TRIGGER SU T2 PER INSERIMENTO COME INSERIRE T1 SELEZIONA 3 VAI
-- =========================================== -- Testare tre funzioni: 1 -- =========================================== INSERIRE VALORI T2(1) SELECT [SCOPE_IDENTITY()]=SCOPE_IDENTITY(), [@@IDENTITY]=@@IDENTITY, [IDENT_CURRENT() Per t1]=IDENT_CURRENT(N't1'), [IDENT_CURRENT() Per t2]=IDENT_CURRENT(N't2')
/*--Risultato SCOPE_IDENTITY() @@IDENTITY IDENT_CURRENT() Per t1 IDENT_CURRENT() Per t2 ------------------ ------------ -------------------------- ----------------------- 1 3 3 1
(Il numero di righe interessate è 1 riga) --*/ VAI
-- =========================================== -- Testare tre funzioni: 2 -- =========================================== INSERIRE VALORI T1(10) SELECT [SCOPE_IDENTITY()]=SCOPE_IDENTITY(), [@@IDENTITY]=@@IDENTITY, [IDENT_CURRENT() Per t1]=IDENT_CURRENT(N't1'), [IDENT_CURRENT() Per t2]=IDENT_CURRENT(N't2')
/*--Risultato SCOPE_IDENTITY() @@IDENTITY IDENT_CURRENT() Per t1 IDENT_CURRENT() Per t2 ------------------ ------------ -------------------------- ----------------------- 4 4 4 1
(Il numero di righe interessate è 1 riga) --*/ VAI
-- =========================================== -- Testare tre funzioni: 3 -- ** Apri una nuova connessione ed esegui il seguente codice ** -- =========================================== SELECT [SCOPE_IDENTITY()]=SCOPE_IDENTITY(), [@@IDENTITY]=@@IDENTITY, [IDENT_CURRENT() Per t1]=IDENT_CURRENT(N't1'), [IDENT_CURRENT() Per t2]=IDENT_CURRENT(N't2')
/*--Risultato SCOPE_IDENTITY() @@IDENTITY IDENT_CURRENT() Per t1 IDENT_CURRENT() Per t2 ------------------ ------------ -------------------------- ----------------------- NULL NULL 4 & n
--=========================================== -- Elimina l'ambiente di test -- =========================================== DROP TABLE T1,T2
-- b) Descrizione del risultato del codice Come puoi vedere dal codice sopra: IDENT_CURRENT() restituisce sempre l'ultimo valore identificato inserito nella tabella specificata @@IDENTITY restituisce il valore identità della sessione corrente, sia nello stesso ambito che meno, nei test 1 e 2 si può vedere che restituisce il valore identità del record inserito nel trigger, e in
Nel Test 3, NULL viene restituito perché non c'è alcun record di inserimento nella sessione corrente SCOPE_IDENTITY() restituisce il valore di identificazione dello stesso ambito della sessione corrente, quindi nel test 1, 2 restituisce il valore non interessato dal trigger, e nel test 3, perché la sessione corrente non è interpolata
quindi restituisce NULL
|