PostgreSQL Equivalent for @@ROWCOUNT and RAISERROR in SQL Server

In SQL Server, I often use the following pattern:

SELECT *
FROM dbo.cool_dudes cd
WHERE cd.dude_id = 'pgslq_megamind';

IF @@ROWCOUNT = 0
BEGIN
RAISERROR('Query returned no data', 16, 1);
END

This checks if the query returned any rows and raises an error if none are found.

I’m looking for the PostgreSQL equivalent of this logic. Specifically:

  • Is there a built-in variable or function in PostgreSQL that gives the row count of the last executed query?

  • How can I implement the same behavior (throw an error if no rows are returned) in PostgreSQL?

Any suggestions or best practices for handling this scenario in PostgreSQL would be appreciated!

Like essentially all DBMS except SQL Server, Postgres makes a clear distinction between the query language SQL and the procedural language (PL/pgSQL)

The equivalent constructs are only available in PL/pgSQL

This is a German forum, so please ask in German in the future

Dieses Thema wurde automatisch 60 Tage nach der letzten Antwort geschlossen. Es sind keine neuen Antworten mehr erlaubt.