declare @id int ,@long int,@i int ,@myid varchar(2000)
declare id_cursor cursor for
SELECT id FROM table1
open id_cursor
fetch next from id_cursor into @id
while @@fetch_status=0
begin
set @long=''
set @i=1
set @myid=''
select @long=long from table1 where id=@id
while @i<@long
begin
set @myid=cast(@myid as varchar(2000))+'0'
set @i=@i+1
end
set @myid=@myid+''+cast(@id as varchar(2000))
update table1 set myid=@myid where id=@id
fetch next from id_cursor into @id
end
close id_cursor
deallocate id_cursor
declare id_cursor cursor for
SELECT id FROM table1
open id_cursor
fetch next from id_cursor into @id
while @@fetch_status=0
begin
set @long=''
set @i=1
set @myid=''
select @long=long from table1 where id=@id
while @i<@long
begin
set @myid=cast(@myid as varchar(2000))+'0'
set @i=@i+1
end
set @myid=@myid+''+cast(@id as varchar(2000))
update table1 set myid=@myid where id=@id
fetch next from id_cursor into @id
end
close id_cursor
deallocate id_cursor
RIGHT(REPLICATE('0',long) + CONVERT(NVARCHAR(10),id),long) AS myid
FROM TABLE