Wednesday, April 30, 2014

Help in Complex query

Can you try the below




DECLARE @MyVariable int;
Select @MyVariable=MAX(code_t1) from #T1
DECLARE @rownum int=0
DECLARE @id int;
Declare @code_desc varchar(10)='0334'

DECLARE upd_cursor CURSOR
FOR SELECT code_t1 FROM #t1

OPEN upd_cursor
FETCH NEXT FROM upd_cursor
INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN
set @rownum=@rownum+1
UPDATE #t1
set code_t1 = (@MyVariable)+@rownum,
code_t1_desc=replace(code_t1_desc,SUBSTRING(code_t1_desc,0,CHARINDEX('_',code_t1_desc)),@code_desc)
where code_t1=@id
FETCH NEXT FROM upd_cursor
INTO @id

END
CLOSE upd_cursor;
DEALLOCATE upd_cursor;

select * from #t1









For the first part answer,Please modify the second part accordingly.


--Prashanth



No comments:

Post a Comment