Updated sql script

13 08 2007

It feels wrong when i need two nestled cursors in one sql script . so here is a update

 

create table #t
(
fksid int,
parentid varchar(4000) default ”,
parentname varchar(4000) default ”
)
set nocount on
insert into #t (fksid) select distinct fksid from tblsidgroup

declare @fksid int
declare @pname varchar(4000) — this is a limit
declare @pid varchar(4000) — this is a limit

DECLARE t_cursor CURSOR FOR
SELECT fksid FROM #t
OPEN t_cursor
FETCH NEXT FROM t_cursor into @fksid
WHILE @@FETCH_STATUS = 0
BEGIN
    select @pname=”
    select @pid = ”
    select @pid = @pid + cast(fksidparent as varchar(5)) + ‘,’ ,@pname=@pname + name + ‘,’ from tblsidgroup inner join tblsid on ( tblsid.pkid=tblsidgroup.fksidparent) where tblsidgroup.fksid=@fksid order by fksidparent

    update #t set parentname=@pname,parentid=@pid where fksid=@fksid
    FETCH NEXT FROM t_cursor into @fksid
END
CLOSE t_cursor
DEALLOCATE t_cursor

set nocount off
select * from #t
drop table #t

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: