sim.plified.com

Chris Pollock

Chris Pollock - web developer (PHP/mySQL & ASP.NET)
undivided… my thoughts on world, family, church, business, technology and Jesus Christ (all in all)

T-SQL Loop through SELECT Statement

I recently changed the a data structure in calendar program and needed to move some data around.  Normally to do this I would write an external app to query the database and rewrite the data.  This time I decided to try using T-SQL.  It worked great.  Below is my code:

DECLARE @Event_ID int

DECLARE my_cursor CURSOR FOR
SELECT Event_ID FROM ITS_CALENDAR_EVENTS_LIST
WHERE EVENT_SA_Flag = 1

OPEN my_cursor

FETCH NEXT FROM my_cursor
INTO @Event_ID

WHILE @@FETCH_STATUS = 0
BEGIN
    INSERT INTO ITS_CALENDAR_EVENTS_TO_FLAGS (Event_ID, Event_Flag_ID)
    VALUES (@Event_ID, 1)

    FETCH NEXT FROM my_cursor
    INTO @Event_ID
END

CLOSE my_cursor
DEALLOCATE my_cursor
Technorati Tags: ,,

No Comments, Comment or Ping

Reply to “T-SQL Loop through SELECT Statement”

My Pictures

CIMG6647CIMG6646CIMG6643CIMG6641CIMG6640CIMG6638CIMG6637CIMG6635CIMG6632CIMG6629

Chris Pollock

Web Developer - proficient in both PHP and ASP.NET.
Rochester, New York

View my web developement site.

View Chris Pollock's LinkedIn profile

My Pictures

CIMG6647CIMG6646CIMG6643CIMG6641CIMG6640CIMG6638CIMG6637CIMG6635CIMG6632CIMG6629