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


Replacing Cursors and While Loops


Replacing Cursors and While Loops

Author
Message
colin.Leversuch-Roberts
colin.Leversuch-Roberts
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4629 Visits: 715

It's just worth pointing out that by default how a cursor and while loop work are different from a transaction view point. A while will execute as a complete transaction - which might bring about its own problems. ( this assumes no explicit transactions defined )

Just an observation - I like replacing cursors with while loops but you do have to be careful some times.



The GrumpyOldDBA
www.grumpyolddba.co.uk
http://sqlblogcasts.com/blogs/grumpyolddba/
Clinton Herring
Clinton Herring
SSC-Enthusiastic
SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)

Group: General Forum Members
Points: 168 Visits: 12
Good thoughts Mark. Thanks for your input.



Vladimir Sim
Vladimir Sim
SSC-Enthusiastic
SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)

Group: General Forum Members
Points: 134 Visits: 46
Hi Herring,

I read with interest on this topic of the 'replacing cursor and while loop'.

I faced the same problem on having lot of cursor which I replace with loop, but didn't solve my execution time.

Your article really help me to look at different way to replace cursor with select statment. I know how to use
select for updating data using select method from your
article.

But I cannot think of any good way to insert new data
using select statement. Do you have any good way to
insert new data using the select statement which I replace
from cursor.

Please advise. thank you
Clinton Herring
Clinton Herring
SSC-Enthusiastic
SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)

Group: General Forum Members
Points: 168 Visits: 12
You can of course use the Values clause but if you want to call functions to generate some new data then use the select statement...insert into >table< (>columnlist<)
select value1, value2, >function<, >etc. until column list is matched<



Vladimir Sim
Vladimir Sim
SSC-Enthusiastic
SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)

Group: General Forum Members
Points: 134 Visits: 46
Hi Herring,

Below is my example to illustrate my issue.
From your article, I know how to use function to
update using select statement.

But I can't think on how to insert new values
using select statement. Please illustrate with
example to guide me. How to convert my example
into using select statement inserting and updating..
Thank you.

declare cur_insert CURSOR FOR
select ..... FROM
where xxxx

While(xxx)
begin

if(xxx)
Insert into values(xxxx)
else
update

end
Doug Hiwiller
Doug Hiwiller
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 7

Perhaps this has already been asked (or scoffed at for being 'undocumented') but have you considered a function using COALESCE to return your pivoted series?

CREATE TABLE Test (
id INT,
val VARCHAR(10))

INSERT Test (id, val) VALUES(56, 'run')
INSERT Test (id, val) VALUES(56, 'jump')
INSERT Test (id, val) VALUES(56, 'scale')
INSERT Test (id, val) VALUES(57, 'fly')
INSERT Test (id, val) VALUES(57, 'swim')
INSERT Test (id, val) VALUES(57, 'crawl')

GO

CREATE FUNCTION uf_pivot_series (@id INT)
RETURNS VARCHAR(8000)
BEGIN

DECLARE @return VARCHAR(8000)
DECLARE @delimiter CHAR(2)

SET @delimiter = ', '

SELECT @return = COALESCE(@return + @delimiter, '') + val FROM Test
WHERE id = @id


RETURN @return
END

GO

SELECT id,
dbo.uf_pivot_series(id) as val
FROM Test


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