logo       

Re: Using dynamic SQL to set a variable: msg#00105

Subject: Re: Using dynamic SQL to set a variable
Here is a little stored procedure which I use to count
rows in all tables. Very useful:

create procedure dbo.count_rows

@user_name varChar(255)

as

declare @user_id int,
 @table_name varChar(128)

select @user_id = user_id(@user_name)

declare table_curs insensitive cursor for
select  name
from sysObjects
where type = 'U' and
 uid = @user_id

open table_curs

create table #temp1 (table_name varchar(50), row_count
integer)


fetch next
from table_curs
into @table_name

while @@fetch_status <> -1
 begin

 execute('INSERT INTO #temp1 SELECT ''' + @table_name
+ ''' Table_Name, count(*)
Row_Count from ' + @user_name + '.' + @table_name)

 fetch next
 from table_curs
 into @table_name

 end

close table_curs

deallocate table_curs

select * from #temp1 order by row_count desc

drop table #temp1

GO


                
__________________________________ 
Do you Yahoo!? 
Dress up your holiday email, Hollywood style. Learn more. 
http://celebrity.mail.yahoo.com


------------------------ Yahoo! Groups Sponsor --------------------~--> 
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/dpFolB/TM
--------------------------------------------------------------------~-> 

 


<Prev in Thread] Current Thread [Next in Thread>