dbTalk Databases Forums  

cursor

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss cursor in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
fumi
 
Posts: n/a

Default cursor - 01-12-2004 , 11:42 AM






Please help
I am trying to return the number of records in tables in
our database using the script below. The problem is, I can
not format the EXEC() to return the table names and some
text. such as 'The number of rows in TABLENAME X '=' Y

DECLARE tables_cursor CURSOR
FOR
SELECT tbname FROM maxsyscolumns
OPEN tables_cursor
DECLARE @tablename varchar(30)
FETCH NEXT FROM tables_cursor INTO @tablename
WHILE (@@fetch_status <> -1)
BEGIN
EXEC ('Select count(*) from '+ @tablename)

FETCH NEXT FROM tables_cursor INTO @tablename
END
DEALLOCATE tables_cursor

Reply With Quote
  #2  
Old   
Allan Mitchell
 
Posts: n/a

Default Re: cursor - 01-12-2004 , 12:47 PM






What about

CREATE TABLE CountRecsInTable(TableName varchar(50), CountOfRows int)
GO
EXEC sp_MSforeachtable "INSERT CountRecsInTable SELECT '?',COUNT(*) FROM ?"
GO



--
--

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
www.allisonmitchell.com - Expert SQL Server Consultancy.
www.SQLDTS.com - The site for all your DTS needs.
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org


"fumi" <fumi.omotayo (AT) ecovertfm (DOT) com> wrote

Quote:
Please help
I am trying to return the number of records in tables in
our database using the script below. The problem is, I can
not format the EXEC() to return the table names and some
text. such as 'The number of rows in TABLENAME X '=' Y

DECLARE tables_cursor CURSOR
FOR
SELECT tbname FROM maxsyscolumns
OPEN tables_cursor
DECLARE @tablename varchar(30)
FETCH NEXT FROM tables_cursor INTO @tablename
WHILE (@@fetch_status <> -1)
BEGIN
EXEC ('Select count(*) from '+ @tablename)

FETCH NEXT FROM tables_cursor INTO @tablename
END
DEALLOCATE tables_cursor



Reply With Quote
  #3  
Old   
fumi.omotayo@ecovertfm.com
 
Posts: n/a

Default cursor - 01-13-2004 , 03:12 AM



PLEASE HELP
Can someone please help. I need to modify the EXEC
statement below to display the table name aswell
Quote:
-----Original Message-----
Please help
I am trying to return the number of records in tables in
our database using the script below. The problem is, I
can
not format the EXEC() to return the table names and some
text. such as 'The number of rows in TABLENAME X '=' Y

DECLARE tables_cursor CURSOR
FOR
SELECT tbname FROM maxsyscolumns
OPEN tables_cursor
DECLARE @tablename varchar(30)
FETCH NEXT FROM tables_cursor INTO @tablename
WHILE (@@fetch_status <> -1)
BEGIN
EXEC ('Select count(*) from '+ @tablename)

FETCH NEXT FROM tables_cursor INTO @tablename
END
DEALLOCATE tables_cursor
.


Reply With Quote
  #4  
Old   
Allan Mitchell
 
Posts: n/a

Default Re: cursor - 01-13-2004 , 04:00 AM



Can you not just concatenate the @tablename variable into your exac
statement

something like

DECLARE @tname varchar(128)
set @tname = 'authors'
EXEC('select ''' + @tname + ''', count(*) from ' + @tname)



--

----------------------------

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
www.allisonmitchell.com - Expert SQL Server Consultancy.
www.SQLDTS.com - The site for all your DTS needs.
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org


"fumi.omotayo (AT) ecovertfm (DOT) com" <anonymous (AT) discussions (DOT) microsoft.com> wrote in
message news:538b01c3d9b5$5a5f0330$7d02280a (AT) phx (DOT) gbl...
Quote:
PLEASE HELP
Can someone please help. I need to modify the EXEC
statement below to display the table name aswell
-----Original Message-----
Please help
I am trying to return the number of records in tables in
our database using the script below. The problem is, I
can
not format the EXEC() to return the table names and some
text. such as 'The number of rows in TABLENAME X '=' Y

DECLARE tables_cursor CURSOR
FOR
SELECT tbname FROM maxsyscolumns
OPEN tables_cursor
DECLARE @tablename varchar(30)
FETCH NEXT FROM tables_cursor INTO @tablename
WHILE (@@fetch_status <> -1)
BEGIN
EXEC ('Select count(*) from '+ @tablename)

FETCH NEXT FROM tables_cursor INTO @tablename
END
DEALLOCATE tables_cursor
.




Reply With Quote
  #5  
Old   
fumi
 
Posts: n/a

Default Re: cursor - 01-14-2004 , 06:04 AM



Thanks Allan. Your reply answered my question.
Thanks again
fumi
Quote:
-----Original Message-----
Can you not just concatenate the @tablename variable into
your exac
statement

something like

DECLARE @tname varchar(128)
set @tname = 'authors'
EXEC('select ''' + @tname + ''', count(*) from ' + @tname)



--

----------------------------

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
www.allisonmitchell.com - Expert SQL Server Consultancy.
www.SQLDTS.com - The site for all your DTS needs.
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org


"fumi.omotayo (AT) ecovertfm (DOT) com"
anonymous (AT) discussions (DOT) microsoft.com> wrote in
message news:538b01c3d9b5$5a5f0330$7d02280a (AT) phx (DOT) gbl...
PLEASE HELP
Can someone please help. I need to modify the EXEC
statement below to display the table name aswell
-----Original Message-----
Please help
I am trying to return the number of records in tables
in
our database using the script below. The problem is, I
can
not format the EXEC() to return the table names and
some
text. such as 'The number of rows in TABLENAME X '=' Y

DECLARE tables_cursor CURSOR
FOR
SELECT tbname FROM maxsyscolumns
OPEN tables_cursor
DECLARE @tablename varchar(30)
FETCH NEXT FROM tables_cursor INTO @tablename
WHILE (@@fetch_status <> -1)
BEGIN
EXEC ('Select count(*) from '+ @tablename)

FETCH NEXT FROM tables_cursor INTO @tablename
END
DEALLOCATE tables_cursor
.



.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.