Showing posts with label scripts. Show all posts
Showing posts with label scripts. Show all posts
novice techie

PostgreSQL ROW_COUNT equivalent to MS SQL @@ROWCOUNT

In MSSQL @@ROWCOUNT is used to get the number of records affected by the last executed sql query statement, like this in PostgreSQL ROW_COUNT is used to get the last executed sql query statements affected row count. This usually used in the UPDATE, INSERT,  DELETE statements whether it is affected at least one row.

How to access the number of rows affected in PostgreSQL using ROW_COUNT? Get count of records affected by INSERT or UPDATE in PostgreSQL

GET DIAGNOSTICS sql query statement  is used for ROW_COUNT of last executed sql query.
GET DIAGNOSTICS v_RowCountInt = ROW_COUNT

How to use PostgreSQL rowcount function for row count of last executed sql query?

DO
$$
DECLARE v_RowCountInt  Int;

BEGIN
 UPDATE usermst
 SET emalid = 'abc@gmail.com';
 
 GET DIAGNOSTICS v_RowCountInt = ROW_COUNT;
  
 RAISE NOTICE 'Returned % rows', v_RowCountInt;

END;
$$

How to use the @@ROWCOUNT in MSSQL?

@@ROWCOUNT is used in MS SQL to get the last executed queries row count.  In this update sql query statement @@rowcount is used to detect if any rows were changed.

How to use PostgreSQL rowcount for row count of last executed sql query?

UPDATE usermst  
SET emailid = 'abc@gmail.com' 
IF @@ROWCOUNT=0 
PRINT 'No data updated.'                                           

PostgreSQL rowcount | Postgres row count | Postgres count rows |

How to get row count in PostgreSQL?

The basic SQL standard query to get count of rows in a table is

SELECT COUNT(*) FROM tablename;

PostgreSQL COUNT function is an aggregate function. This COUNT function allows you to get the number of records in a table that matches the specific condition of SQL query. In this tutorial you will learn how to use the COUNT aggregate function to get the row count in PostgreSQL.

Example for PostgreSQL COUNT(*)

SELECT COUNT(*) FROM usermst;

Example for Postgre SQL COUNT(DISTINCT)

SELECT COUNT(DISTINCT username) FROM usermst;

Example for Post gre SQL COUNT() with GROUP BY clause

SELECT username, COUNT(username) FROM usermst GROUP BY username;

Example PostgreSQL COUNT() with HAVING clause

SELECT username, COUNT(username) FROM usermst GROUP BY username HAVING COUNT(username) > 1;


TAGS
POSTGRESQL ROW COUNT | SQL ROWCOUNT | POSTGRES ROW COUNT | POSTGRESQL UPDATE RETURNING COUNT

Please visit other related articles

PostgreSQL: Add macros in PgAdminIII
PostgreSQL: Export PostgreSQL data in to excel file
PostgreSQL: How to restrict the user access to the databases in Postgresql based on the permission
PostgreSQL: List table details using Alt+F1 in PostgreSQL
PostgreSQL: How to get ROW_COUNT of last executed query in PostgreSQL like MS SQL @@ROWCOUNT ?
novicetechie.com

Log Shipping - How you can convert the secondary database into primary
This topic explains the log shipping if the primary database is suspected or corrupted and how you can convert the secondary database into primary database.
To recover a database without restoring (to be done in the master data base)
Execute the RESTORE DATABASE statement, specifying:
  • ·   The name of the database to be recovered
  • ·   The RECOVERY clause


  /* To recover a database without restoring (to be done in the master database)  
   Execute the RESTORE DATABASE statement, specifying:
   The name of the database to be recovered
   The RECOVERY clause

Example
For example, you can recover the database, AdventureWorks2008R2, as in a restore operation without restoring data
*/

-- Restore database using WITH RECOVERY.
RESTORE DATABASE AdventureWorks2008R2
   WITH RECOVERY