Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««123

Replacing Cursors and While Loops Expand / Collapse
Author
Message
Posted Monday, April 10, 2006 6:43 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 1:08 AM
Points: 2,674, Visits: 695

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/
Post #272180
Posted Tuesday, April 11, 2006 6:58 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, March 09, 2009 1:35 PM
Points: 60, Visits: 12
Good thoughts Mark. Thanks for your input.


Post #272433
Posted Sunday, May 07, 2006 12:08 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, July 12, 2009 7:27 PM
Points: 88, 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



Post #278181
Posted Sunday, May 07, 2006 12:44 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, March 09, 2009 1:35 PM
Points: 60, 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<



Post #278182
Posted Sunday, May 07, 2006 10:48 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, July 12, 2009 7:27 PM
Points: 88, 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






Post #278193
Posted Monday, April 09, 2007 2:00 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, April 24, 2009 8:57 AM
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

Post #356970
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse