If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Sybase > Sybase concatenation

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-22-11, 07:07
sandeeppvk sandeeppvk is offline
Registered User
 
Join Date: Jun 2011
Posts: 2
Sybase concatenation

Hello Gurus,

I have a requirement like below..

T1 table

ID code
001 1
001 2
002 1
003 4
003 1

Output should be

T2 table

ID code
001 1,2
002 1
003 1,4


I tried with cursors in Sybase... like below...

alter proc sp_test_sample
as
begin
DECLARE @k char(8), @id_var char(8), @cd_var VARCHAR(10)

DECLARE c CURSOR FOR
select id,code from party_sample order by id,code

create table #work
(
id_work char(8), cd_work varchar(200))

select @k ="-1"

OPEN c

FETCH c INTO @id_var, @cd_var

while(@@sqlstatus=0)
begin

if @id_var != @k
insert into #work (id_work, cd_work) values (@id_var,@cd_var)
else
update #work set cd_work = cd_work+','+@cd_var where id_work = @id_var


print 'hello %1! , %2!' , @id_var,@cd_var

select @k = @id_var

FETCH c INTO @id_var, @cd_var


end

CLOSE c

DEALLOCATE c

============


However as sybase is not a row processor like oracle,.. its taking lot of time.. Could you please suggest an alternative solution using temp tables or something similar which will be faster in Sybase...?

I am using Sybase IQ.. List() is not a valid command FYI

Last edited by sandeeppvk; 06-22-11 at 07:11.
Reply With Quote
  #2 (permalink)  
Old 06-23-11, 02:43
rabihm rabihm is offline
Registered User
 
Join Date: Jun 2011
Posts: 1
this should solve your problem...
select ID as "c_id" , (select list(code) from T1 where ID = "c_id") from T1
Reply With Quote
  #3 (permalink)  
Old 06-24-11, 01:11
sandeeppvk sandeeppvk is offline
Registered User
 
Join Date: Jun 2011
Posts: 2
List is not a valid function in Sybase IQ. Any other options which will be faster for concatenating rows into column?
Reply With Quote
  #4 (permalink)  
Old 06-24-11, 12:09
pradyut.dhara pradyut.dhara is offline
Registered User
 
Join Date: May 2011
Posts: 28
Sybase concatenation

Hi,
Please try this option

DECLARE @LastID Varchar(3),
@LastCode VARCHAR(12)

SELECT ID,CODE,CONVERT(VARCHAR(25),'') as RESULT
INTO #T1 FROM T1 order by ID,CODE

UPDATE #T1
SET RESULT = CASE
WHEN ID = @LastID THEN @LastCode+','+CODE
ELSE CODE
END,
@LastID = ID,
@LastCode = CODE

SELECT ID,MAX(RESULT) FROM #T1 GROUP BY ID
GO
Reply With Quote
Reply

Tags
concatenate, sybase

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On