SQL @@ROWCOUNT and ROW_COUNT()
Every engine keeps a per-session count of the rows the previous statement changed. The name differs, and so does which statements reset it.
- Categoría
- System & session
- Returns
integer- In this category
- 2 of 13
Sintaxis
@@ROWCOUNT
ROW_COUNT()
changes()
GET DIAGNOSTICS n = ROW_COUNT
SQL%ROWCOUNT
Each block is a separate form. The support table and the examples show which engine takes which.
Soporte del motor
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Sí | @@ROWCOUNT; ROWCOUNT_BIG() past 2 billion rows |
| PostgreSQL | Different name or syntax | GET DIAGNOSTICS n = ROW_COUNT inside PL/pgSQL; clients read it from the command tag |
| MySQL | Sí | ROW_COUNT(); -1 after a SELECT |
| SQLite | Different name or syntax | changes(); total_changes() for the whole connection |
| Oracle | Different name or syntax | SQL%ROWCOUNT inside PL/SQL |
Ejemplos
SQL Server
CREATE TABLE stock (sku INT, qty INT); INSERT INTO stock VALUES (1, 5), (2, 0), (3, 0); UPDATE stock SET qty = 10 WHERE qty = 0; SELECT @@ROWCOUNT AS updated; -- 2
MySQL
CREATE TABLE stock (sku INT, qty INT); INSERT INTO stock VALUES (1, 5), (2, 0), (3, 0); UPDATE stock SET qty = 10 WHERE qty = 0; SELECT ROW_COUNT() AS updated; -- 2
SQLite
CREATE TABLE stock (sku INT, qty INT); INSERT INTO stock VALUES (1, 5), (2, 0), (3, 0); UPDATE stock SET qty = 10 WHERE qty = 0; SELECT changes() AS updated; -- 2
PostgreSQL
CREATE TABLE stock (sku INT, qty INT); INSERT INTO stock VALUES (1, 5), (2, 0), (3, 0); DO $$ DECLARE n bigint; BEGIN UPDATE stock SET qty = 10 WHERE qty = 0; GET DIAGNOSTICS n = ROW_COUNT; RAISE NOTICE 'updated %', n; -- updated 2 END $$;
Conviene saberlo
- SQL Server resets @@ROWCOUNT after statements such as PRINT, SET options, BEGIN TRANSACTION and COMMIT. Copy it into a variable on the line right after the DML: SELECT @n = @@ROWCOUNT.
- MySQL counts rows actually changed: an UPDATE that writes the value a column already has reports 0. Clients that connect with the CLIENT_FOUND_ROWS flag get rows matched instead.
- SQLite's changes() counts only rows the statement changed directly. Rows changed by triggers, foreign key actions or REPLACE conflict handling are left out.
Questions about @@ROWCOUNT / ROW_COUNT
Why is @@ROWCOUNT 0 when my UPDATE changed rows?
Another statement ran in between. PRINT, SET options and transaction statements reset it to 0. Capture it immediately after the INSERT, UPDATE or DELETE.
Does MySQL's ROW_COUNT() count rows matched or rows changed?
Rows changed, unless the client connected with CLIENT_FOUND_ROWS. An UPDATE that writes the same value counts 0.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.