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!