Find number of rows in table using cursor in SQL

Regarding MSSQL,

Following code uses sys.objects table to retrieve ‘table names’, and running cursor on it to loop through each tables, then used system procedure ‘sp_executesql‘ to run a simple query to find number of records in it.

declare @tblname as nvarchar(200)
declare curForCount cursor for
select name from sys.objects where type=’u’ and name ‘sysdiagrams’

OPEN curForCount
fetch next from curForCount
into @tblname
WHILE @@FETCH_STATUS=0
BEGIN

declare @query nvarchar(1000);
set @query=’select ”’ + @tblname + ‘ — ” + cast(count(*) as nvarchar(20)) from ‘ + @tblname

exec sp_executesql @query

fetch next from curForCount
into @tblname
END
CLOSE curForCount
DEALLOCATE curForCount

 

Other Code stuff from Siddharth Borania:

LINQ queries with join in c#

https://siddharthboraniait.wordpress.com/2012/09/06/linq-query-in-csharp-asp-net-join

Advertisements