SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


ORDER BY in a cursor


ORDER BY in a cursor

Author
Message
TheRedneckDBA
TheRedneckDBA
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2524 Visits: 2610
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
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39782 Visits: 38563
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



Cool

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
dva2007
dva2007
Right there with Babe
Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)

Group: General Forum Members
Points: 796 Visits: 1112
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
dva2007
dva2007
Right there with Babe
Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)

Group: General Forum Members
Points: 796 Visits: 1112
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
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39782 Visits: 38563
ORDER BY in a cursor works. Copy my code and test it.

Cool

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
dva2007
dva2007
Right there with Babe
Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)

Group: General Forum Members
Points: 796 Visits: 1112
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.
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39782 Visits: 38563
Curious, looking at your code, are you running SQL 2005 or SQL 2000?

Cool

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
dva2007
dva2007
Right there with Babe
Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)

Group: General Forum Members
Points: 796 Visits: 1112
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.
TheRedneckDBA
TheRedneckDBA
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2524 Visits: 2610
Taking out the parentesis arround the select statement fixed it.

The Redneck DBA
phochstr
phochstr
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 29
Thanks Lynn - good solutions can be so simple - and so unintuitive...

P.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search