June 5, 2008 at 8:57 am
Is there a limitation on being able to order the select statement you are using for a cursor?
In the query below, if I comment out the order by clause, it works, but if I leave it there it won't run.
DECLARE @DB varchar(25)
DECLARE DBCursor CURSOR FOR
(
SELECT [Name]
FROM master.dbo.sysdatabases
WHERE
[NAME] LIKE '%PLUS' OR [NAME] LIKE 'BILLING%'
ORDER BY [DBID] DESC
)
OPEN DBCursor
FETCH NEXT FROM DBCursor INTO @DB
WHILE @@fetch_status = 0
BEGIN -- WHILE BEGIN
PRINT @DB
FETCH NEXT FROM DBCursor INTO @DB
END -- WHILE END
CLOSE DBCursor
DEALLOCATE DBCursor
The Redneck DBA
June 5, 2008 at 9:12 am
Try this:
DECLARE @DB varchar(25)
DECLARE DBCursor CURSOR FOR
SELECT [name]
FROM master.sys.databases
WHERE
[name] LIKE '%PLUS' OR [NAME] LIKE 'BILLING%'
ORDER BY [database_id] DESC
OPEN DBCursor
FETCH NEXT FROM DBCursor INTO @DB
WHILE @@fetch_status = 0
BEGIN -- WHILE BEGIN
PRINT @DB
FETCH NEXT FROM DBCursor INTO @DB
END -- WHILE END
CLOSE DBCursor
DEALLOCATE DBCursor
😎
June 5, 2008 at 9:19 am
Hi,
I believe cursor dont like order by. I am not sure. But i tried to run same thing and it didnt work. What you can do is before your cursor start you can enter your required data with order by into some other table. (temp table) and then use select statement from that table.
thanks,
vijay
June 5, 2008 at 9:20 am
Here you go
CREATE PROCEDURE [dbo].[VD_TEMP_0506] AS
DECLARE @DB varchar(25)
create table #t ([name] varchar(50))
insert into #t
SELECT [Name]
FROM master.dbo.sysdatabases
Where [name] like '%NCR%'
order by [Name]
DECLARE DBCursor CURSOR FOR
(
SELECT [Name]
FROM #t
)
OPEN DBCursor
FETCH NEXT FROM DBCursor INTO @DB
WHILE @@fetch_status = 0
BEGIN -- WHILE BEGIN
PRINT @DB
FETCH NEXT FROM DBCursor INTO @DB
END -- WHILE END
CLOSE DBCursor
DEALLOCATE DBCursor
GO
June 5, 2008 at 9:23 am
ORDER BY in a cursor works. Copy my code and test it.
😎
June 5, 2008 at 9:36 am
Hi,
Yes Lynn. you are right. It works. I said i m not sure because i never come up with the situation.
It is just bracket. ()
DECLARE @DB varchar(25)
DECLARE DBCursor CURSOR FOR
SELECT [Name]
FROM master.dbo.sysdatabases
WHERE
[NAME] LIKE '%PLUS' OR [NAME] LIKE 'BILLING%'
ORDER BY [DBID] DESC
OPEN DBCursor
FETCH NEXT FROM DBCursor INTO @DB
WHILE @@fetch_status = 0
BEGIN -- WHILE BEGIN
PRINT @DB
FETCH NEXT FROM DBCursor INTO @DB
END -- WHILE END
CLOSE DBCursor
DEALLOCATE DBCursor
And it will work.
June 5, 2008 at 9:38 am
Curious, looking at your code, are you running SQL 2005 or SQL 2000?
😎
June 5, 2008 at 9:51 am
I have both opened. We have some server on 2000 and some on 2005. so can use 2005 and enterprise manager as well.
When i take jason's code i need to use 2000 and ur code - 2005.
June 5, 2008 at 9:55 am
Taking out the parentesis arround the select statement fixed it.
The Redneck DBA
July 9, 2013 at 12:03 am
Thanks Lynn - good solutions can be so simple - and so unintuitive...
P.
July 9, 2013 at 3:22 am
Please note: 5 year old thread.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 9, 2013 at 6:15 am
Just because it is old doesn't mean some of us won't find it to be useful information.
The Redneck DBA
July 9, 2013 at 6:29 am
Never said it would. Hopefully however it will prevent people wasting time trying to solve a problem that's been solved for 5 years (as I did this morning before I noticed the date)
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 9, 2013 at 10:25 pm
Guys
It saved me lots of time after a wasted afternoon. So, it's an oldie but goldie!
P.
July 10, 2013 at 3:35 am
Viewing 15 posts - 1 through 15 (of 17 total)
You must be logged in to reply to this topic. Login to reply