Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Whats wrong with this cursor

Author  Topic 

0341
Starting Member

21 Posts

Posted - 2008-01-22 : 13:40:51
Select won't run but a Print or a DBCC will.

-Phil

USE Northwind
GO

DECLARE @TableName varchar(255)

DECLARE TableCursor CURSOR FOR
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'

OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName

WHILE @@FETCH_STATUS = 0
BEGIN

SELECT TOP 1 * FROM @TableName
FETCH NEXT FROM TableCursor INTO @TableName

END

CLOSE TableCursor
DEALLOCATE TableCursor

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2008-01-22 : 13:48:09
your @Tablename is a varchar not a table. so you should just do

SELECT @TableName

instead of

SELECT TOP 1 * FROM @TableName



Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

0341
Starting Member

21 Posts

Posted - 2008-01-22 : 14:04:34
But I want a row from each table.

-Phil
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2008-01-22 : 17:16:04
You will get it.

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

Koji Matsumura
Posting Yak Master

141 Posts

Posted - 2008-01-22 : 21:52:32
EXEC('SELECT TOP 1 * FROM [' + @TableName + ']')
Go to Top of Page
   

- Advertisement -