Link to home
Start Free TrialLog in
Avatar of qinyan
qinyan

asked on

Use cursor to append row values?

I have a table like this (abridged):
Alert Period                  Department        AlertSetting
Down at least 1 day      DB      E-mail
Down at least 1 day      DB      SMS
Down at least 1 day      IT      E-mail
Down at least 1 day      IT      SMS
Down at least 1 day      QAG      E-mail
Down at least 1 day      QAG      SMS
......

I want to append the values in each row for each comination of Alert Period and Department like this:
Down at least 1 day      DB      E-mail/SMS
Down at least 1 day      IT      E-mail/SMS
Down at least 1 day      QAG      E-mail/SMS
......

I wrote a cursor like this, but the result I got is like:

Down at least 1 day      DB      SMS/SMS
Down at least 1 day      IT      NULL
Down at least 1 day      QAG      NULL

Can someone please tell me where I was wrong in the code?
Thanks a lot!!
------code------
declare @counter int,
        @count int
declare @AlertPeriod nvarchar(150),
        @Department nvarchar(50),
        @AlertSetting nvarchar(100)

create table #Rotate
             (AlertPeriod nvarchar(150),
              Department  nvarchar(50),
              AlertSetting nvarchar(100))

select @counter = 0

declare mycursor cursor
for select distinct [Alert Period], Department
from #ForCursor

open mycursor
fetch next from mycursor
into @AlertPeriod, @Department

while @@fetch_status = 0
begin
     select @count = count(*)
     from #ForCursor
     where [Alert Period] = @AlertPeriod
     and   Department = @Department

while @counter <= @count
begin
     select @AlertSetting = AlertSetting + '/' + AlertSetting
     from #ForCursor
     where [Alert Period] = @AlertPeriod
     and   Department = @Department

     set @counter = @counter + 1
end

insert into #Rotate
values (@AlertPeriod, @Department, @AlertSetting)

set @AlertSetting = null

fetch next from mycursor
into @AlertPeriod, @Department
end

close mycursor
deallocate mycursor
SOLUTION
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dportas
dportas

There are several possible ways to write this query without a cursor. Here's one:

SELECT AlertPeriod, Department,
 SUBSTRING(
  MAX(CASE WHEN AlertSetting = 'E-mail' THEN '/E-mail' ELSE '' END)+
  MAX(CASE WHEN AlertSetting = 'SMS' THEN '/SMS' ELSE '' END)
  ,2,100)
 AS AlertSetting
 FROM YourTable
 GROUP BY AlertPeriod, Department;

Avatar of qinyan

ASKER

Thanks all for your reply!
For BriCrowe: the result I got is like this, so it's repeating it once:
Down at least 1 day      DB      E-mail/SMSE-mail/SMS
Down at least 1 day      IT      E-mail/SMSE-mail/SMS
Down at least 1 day      QAG      E-mail/SMSE-mail/SMS

For vilimed:
I got the following message:
Msg 156, Level 15, State 1, Line 25
Incorrect syntax near the keyword 'then'.
Msg 156, Level 15, State 1, Line 36
Incorrect syntax near the keyword 'then'.
Msg 156, Level 15, State 1, Line 39
Incorrect syntax near the keyword 'then'.
Msg 156, Level 15, State 1, Line 42
Incorrect syntax near the keyword 'else'.
Msg 156, Level 15, State 1, Line 46
Incorrect syntax near the keyword 'else'.
Msg 156, Level 15, State 1, Line 55
Incorrect syntax near the keyword 'then'.
Avatar of qinyan

ASKER

dportas,
Thanks and I like your method which is neat. I use cursor because the number of alert settings are dynamic I think. Right now there are two. What if in the future we have 3, 4 or more? Then I have to manually add that to the case statement. With a cursor, the number or type of alert settings doesn't matter and the appended result automatically changes.
Thanks again!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of qinyan

ASKER

Thanks for all the good suggestions!