2010-02-16 14 views

risposta

77

@@ ROWCOUNT restituirà il numero di righe interessate dallo ultimo SQL statement, è meglio catturarlo in una variabile locale seguendo il comando in questione, poiché il suo valore cambierà la prossima volta che lo guardi :

DECLARE @Rows int 
DECLARE @TestTable table (col1 int, col2 int) 
INSERT INTO @TestTable (col1, col2) select 1,2 union select 3,4 
SELECT @[email protected]@ROWCOUNT 
SELECT @Rows AS Rows,@@ROWCOUNT AS [ROWCOUNT] 

OUTPUT:

(2 row(s) affected) 
Rows  ROWCOUNT 
----------- ----------- 
2   1 

(1 row(s) affected) 

si ottiene Rows valore 2, il numero di righe inserite, ma ROWCOUNT è 1 perché il comando SELECT @[email protected]@ROWCOUNT influenzata 1 fila

se si hanno più INSERT o UPDATE, ecc. Nella transazione, è necessario determinare come si desidera "contare" cosa sta succedendo. Potresti avere un totale separato per ogni tabella, un singolo valore totale complessivo o qualcosa di completamente diverso. Avrete bisogno di dichiarare una variabile per ciascun totale che si desidera tenere traccia e aggiungere ad essa dopo ogni operazione che si applica ad esso:

--note there is no error handling here, as this is a simple example 
DECLARE @AppleTotal int 
DECLARE @PeachTotal int 

SELECT @AppleTotal=0,@PeachTotal=0 

BEGIN TRANSACTION 

INSERT INTO Apple (col1, col2) Select col1,col2 from xyz where ... 
SET @[email protected][email protected]@ROWCOUNT 

INSERT INTO Apple (col1, col2) Select col1,col2 from abc where ... 
SET @[email protected][email protected]@ROWCOUNT 

INSERT INTO Peach (col1, col2) Select col1,col2 from xyz where ... 
SET @[email protected][email protected]@ROWCOUNT 

INSERT INTO Peach (col1, col2) Select col1,col2 from abc where ... 
SET @[email protected][email protected]@ROWCOUNT 

COMMIT 

SELECT @AppleTotal AS AppleTotal, @PeachTotal AS PeachTotal 
+1

Invece di utilizzare 'SELECT @Rows = @@ ROWCOUNT', ho provato' SET @Rows = @@ ROWCOUNT' e ho ancora ricevuto gli stessi risultati. Grazie, bel post! – MikeTeeVee

+0

FYI: _Even_ il wrapping del mio INSERT statment in Transaction block statements ha causato il reset di @@ RowCount, quindi ho dovuto impostare la variabile @Rows prima di chiamare Commit! – MikeTeeVee

+1

@MikeTeeVee usando 'SELECT' è possibile creare più assegnazioni in una singola istruzione, in modo da poter aggiungere facilmente' SELECT @Rows = @@ ROWCOUNT, @ ID = SCOPE_IDENTITY(), @Error = @@ ERRORE'. Con 'SET' è possibile impostare solo un valore. 'PROVA CATCH' elimina la necessità di catturare' @@ ERRORE', ma uso ancora 'SELECT'. –

2

Nel caso in cui avete bisogno di ulteriori informazioni per il registro/audit è possibile clausola OUTPUT: In questo modo, non solo manterrai il numero di righe interessate, ma anche i record.

Come esempio della clausola di uscita con inserti: SQL Server list of insert identities

DECLARE @InsertedIDs table(ID int); 

INSERT INTO YourTable 
    OUTPUT INSERTED.ID 
     INTO @InsertedIDs 
    SELECT ... 

HTH

2

ho trovato la risposta a inserire un precedente. Ecco qui.

CREATE TABLE #TempTable (id int) 

INSERT INTO @TestTable (col1, col2) OUTPUT INSERTED.id INTO #TempTable select 1,2 

INSERT INTO @TestTable (col1, col2) OUTPUT INSERTED.id INTO #TempTable select 3,4 

SELECT * FROM #TempTable --this select will chage @@ROWCOUNT value 
Problemi correlati