[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

FARRUKH

3/22/2007 8:19:00 PM

wht are the advantages of using cursor and disadvantages? is there any
alternatives

thanks
6 Answers

David Portas

3/22/2007 8:39:00 PM

0

On 22 Mar, 20:19, FARRUKH <farrscorpi...@hotmail.com> wrote:
> wht are the advantages of using cursor and disadvantages? is there any
> alternatives
>
> thanks

Cursors allow you to perform operations on a set of data one row at a
time. The alternative is to use set-based operations like INSERT,
UPDATE, DELETE and SELECT that operate on whole sets of rows at a
time.

SQL Server and the SQL language are designed and optimized for set-
based solutions rather than row-by-row processing so cursors are often
slow, complex, resource-hungry and hard to debug compared to the set-
based equivalents. Most of the time (I'd say at least 99.9% of the
time for data manipulation operations) set-based solutions offer a
more effective solution than cursors.

On the other hand a legitimate place to use a cursor is for something
inherently procedural (typically admin and control tasks such as
managing backups, working with metadata or invoking other external
processes) but in general you should assume the solution to any data-
manipulation problem will be set-based unless expert analysis proves
otherwise.

That's why I would class cursors as a feature for advanced users only.
If you find yourself writing cursors regularly then it's time to re-
think what you are doing or maybe get some training or a second
opinion. Too many cursors are written by inexperienced developers who
don't know better techniques.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/librar...(en-US,SQL.90).aspx
--

sloan

3/22/2007 8:39:00 PM

0


Avoid cursors ..........

There is 99.9% a way to do whatever you need without a cursor.

I've written 1 cursor in 7 years.

alternatives:



derivedTables
UserDefinedFunction

are alternatives.

Here is a derivedTable example:

Use Northwind

GO


-- select * from customers where Country='USA' order by City
set nocount on


--AllCount Number of Customers Per Country
--LondonOnlyCount -- Number of Customers In London specifically. ( which
means, only UK will have a value )
--StartsWithPContactNamesCount -- Number of Customers whose contact name
starts with P... Per Country



declare @returnValues table ( entryid int IDENTITY (1,1) , Country
varchar(24) , AllCount int , LondonOnlyCount int ,
StartsWithPContactNamesCount int )

--Give me something to work with, which is distinct county names
insert into @returnValues ( Country , AllCount , LondonOnlyCount ,
StartsWithPContactNamesCount )
Select distinct Country , 0 , 0 , 0 from dbo.customers


Update @returnValues

Set
AllCount = derivedTable1.PerCountryAllCount
From
@returnValues vart ,
( Select Country , count(*) as PerCountryAllCount from dbo.customers
innerC group by Country )
as derivedTable1
Where
vart.Country = derivedTable1.Country




Update @returnValues

Set
StartsWithPContactNamesCount = derivedTable2.PerCountryCount
From
@returnValues vart ,
( Select Country , count(*) as PerCountryCount from dbo.customers innerC
where innerC.ContactName like 'P%' group by Country )
as derivedTable2
Where
vart.Country = derivedTable2.Country





Update @returnValues

Set
LondonOnlyCount = derivedTable3.PerCountryLondonOnlyCount
From
@returnValues vart ,
( Select Country , count(*) as PerCountryLondonOnlyCount from
dbo.customers innerC where innerC.City like 'London' group by Country )
as derivedTable3
Where
vart.Country = derivedTable3.Country











-- OR all together
Update @returnValues

Set
StartsWithPContactNamesCount = derivedTable2.PerCountryCount
From
@returnValues vart
,
( Select Country , count(*) as PerCountryAllCount from dbo.customers
innerC group by Country )
as derivedTable1
,
( Select Country , count(*) as PerCountryCount from dbo.customers innerC
where innerC.ContactName like 'P%' group by Country )
as derivedTable2
,
( Select Country , count(*) as PerCountryLondonOnlyCount from
dbo.customers innerC where innerC.City like 'London' group by Country )
as derivedTable3
Where
vart.Country = derivedTable1.Country
and
vart.Country = derivedTable2.Country
and
vart.Country = derivedTable3.Country








select Country , AllCount , LondonOnlyCount ,
StartsWithPContactNamesCount from @returnValues



"FARRUKH" <farrscorpio77@hotmail.com> wrote in message
news:D8EEB0E4-DBCA-4E62-9D0D-44927DAE0434@microsoft.com...
> wht are the advantages of using cursor and disadvantages? is there any
> alternatives
>
> thanks


sloan

3/22/2007 8:50:00 PM

0


David's explanation is much better than mine.

My hope (via the example script) was to show some options you may not know
exist.



"sloan" <sloan@ipass.net> wrote in message
news:epXMgIMbHHA.208@TK2MSFTNGP05.phx.gbl...
>
> Avoid cursors ..........
>
> There is 99.9% a way to do whatever you need without a cursor.
>
> I've written 1 cursor in 7 years.
>
> alternatives:
>
>
>
> derivedTables
> UserDefinedFunction
>
> are alternatives.
>
> Here is a derivedTable example:
>
> Use Northwind
>
> GO
>
>
> -- select * from customers where Country='USA' order by City
> set nocount on
>
>
> --AllCount Number of Customers Per Country
> --LondonOnlyCount -- Number of Customers In London specifically. ( which
> means, only UK will have a value )
> --StartsWithPContactNamesCount -- Number of Customers whose contact name
> starts with P... Per Country
>
>
>
> declare @returnValues table ( entryid int IDENTITY (1,1) , Country
> varchar(24) , AllCount int , LondonOnlyCount int ,
> StartsWithPContactNamesCount int )
>
> --Give me something to work with, which is distinct county names
> insert into @returnValues ( Country , AllCount , LondonOnlyCount ,
> StartsWithPContactNamesCount )
> Select distinct Country , 0 , 0 , 0 from dbo.customers
>
>
> Update @returnValues
>
> Set
> AllCount = derivedTable1.PerCountryAllCount
> From
> @returnValues vart ,
> ( Select Country , count(*) as PerCountryAllCount from dbo.customers
> innerC group by Country )
> as derivedTable1
> Where
> vart.Country = derivedTable1.Country
>
>
>
>
> Update @returnValues
>
> Set
> StartsWithPContactNamesCount = derivedTable2.PerCountryCount
> From
> @returnValues vart ,
> ( Select Country , count(*) as PerCountryCount from dbo.customers innerC
> where innerC.ContactName like 'P%' group by Country )
> as derivedTable2
> Where
> vart.Country = derivedTable2.Country
>
>
>
>
>
> Update @returnValues
>
> Set
> LondonOnlyCount = derivedTable3.PerCountryLondonOnlyCount
> From
> @returnValues vart ,
> ( Select Country , count(*) as PerCountryLondonOnlyCount from
> dbo.customers innerC where innerC.City like 'London' group by Country )
> as derivedTable3
> Where
> vart.Country = derivedTable3.Country
>
>
>
>
>
>
>
>
>
>
>
> -- OR all together
> Update @returnValues
>
> Set
> StartsWithPContactNamesCount = derivedTable2.PerCountryCount
> From
> @returnValues vart
> ,
> ( Select Country , count(*) as PerCountryAllCount from dbo.customers
> innerC group by Country )
> as derivedTable1
> ,
> ( Select Country , count(*) as PerCountryCount from dbo.customers innerC
> where innerC.ContactName like 'P%' group by Country )
> as derivedTable2
> ,
> ( Select Country , count(*) as PerCountryLondonOnlyCount from
> dbo.customers innerC where innerC.City like 'London' group by Country )
> as derivedTable3
> Where
> vart.Country = derivedTable1.Country
> and
> vart.Country = derivedTable2.Country
> and
> vart.Country = derivedTable3.Country
>
>
>
>
>
>
>
>
> select Country , AllCount , LondonOnlyCount ,
> StartsWithPContactNamesCount from @returnValues
>
>
>
> "FARRUKH" <farrscorpio77@hotmail.com> wrote in message
> news:D8EEB0E4-DBCA-4E62-9D0D-44927DAE0434@microsoft.com...
> > wht are the advantages of using cursor and disadvantages? is there any
> > alternatives
> >
> > thanks
>
>


Steve Dassin

3/24/2007 2:35:00 AM

0

"sloan" <sloan@ipass.net> wrote in message
news:epXMgIMbHHA.208@TK2MSFTNGP05.phx.gbl...
>
> Avoid cursors ..........
> There is 99.9% a way to do whatever you need without a cursor.
> I've written 1 cursor in 7 years.

So your saying you found a crutch in sql kludges for all but one. And you
want to be rewarded for that? Seems you should be rewarded for the 1 you
didn't need to lean on something.


sloan

3/26/2007 6:59:00 PM

0

Huh?

The reason I had to use a cursor was because the original developers
violated 1NF in their db design.

I don't understand how you equate "I didn't use a cursor" with "sql kludge".

By using UDF's or derivedTables, you can eliminate most of "loop over the
data" and "figure something out".

The exception (as has been noted) is when you're performing db maintenance,
usually by a dba. Because the dba is usually using MS built in methods to
"Get a List of Server" , "Get a List of Databases", etc , etc.


Cursors are performance killers for the most part. And thus the advice to
the user that he should avoid them.




"Steve Dassin" <rac4sqlnospam@net> wrote in message
news:OBds4zcbHHA.4632@TK2MSFTNGP03.phx.gbl...
> "sloan" <sloan@ipass.net> wrote in message
> news:epXMgIMbHHA.208@TK2MSFTNGP05.phx.gbl...
> >
> > Avoid cursors ..........
> > There is 99.9% a way to do whatever you need without a cursor.
> > I've written 1 cursor in 7 years.
>
> So your saying you found a crutch in sql kludges for all but one. And you
> want to be rewarded for that? Seems you should be rewarded for the 1 you
> didn't need to lean on something.
>
>


Steve Dassin

3/26/2007 8:53:00 PM

0

"sloan" <sloan@ipass.net> wrote in message
news:OGiFNj9bHHA.4140@TK2MSFTNGP06.phx.gbl...
> Huh?

Precisely the reaction that most application developers have when
you tell them 'Avoid cursors ..........'

Hopefully progress thru point, counter-point.

best,
steve