0

SQL server(MSSQL) CURSOR dynamic table search :

 DECLARE @tbl    sysname,
            @sql    nvarchar(4000),
            @params nvarchar(4000),
            @count  int

DECLARE tblcur CURSOR STATIC LOCAL FOR
   SELECT * FROM INFORMATION_SCHEMA.TABLES where TABLE_NAME like 'abc%'
OPEN tblcur

WHILE 1 = 1
BEGIN
   FETCH tblcur INTO @tbl
   IF @@fetch_status <> 0
      BREAK

   SELECT @sql =
   N' SELECT @cnt = COUNT(*) FROM dbo.' + quotename(@tbl) '
   EXEC sp_executesql @sql, @cnt = @count OUTPUT

   PRINT @tbl + ': ' + convert(varchar(10), @count) + ' modified rows.'
END

DEALLOCATE tblcur

I want this for PostgreSQL.

what i have done yet :

 DO $$
DECLARE
    i varchar;
    searchsql text;
    temp int;
BEGIN
FOR i IN select table_schema from information_schema.columns where table_schema like 'abc%' 
LOOP
searchsql := 'select count(*) from' || quote_ident(i) ;

    EXECUTE searchsql;
    --IF (temp > 0) THEN
    --  RAISE NOTICE  'Schema % % and count' ,i , temp;
    --END IF;
    RAISE NOTICE  'Schema %' ,i ;
    RAISE NOTICE  'Script %' ,searchsql ;
END LOOP;
END
$$ LANGUAGE plpgsql;

i am new PostgreSQL.

1.How to get count just like sql server (MSSQL)?

2.Is any better way to do this ?

Thanks in Advance .

Jophy job
  • 1,924
  • 2
  • 20
  • 38

0 Answers0