Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

using UNION in a while loop

Posted on 2006-07-17
3
Medium Priority
?
440 Views
Last Modified: 2008-02-01
hi all,


how can UNION be used in a loop, while I am iterating through the cursor..?

            declare
                  @sTicketType varchar(50)

            declare groupNames cursor
                  local fast_forward read_only
            for
                  select distinct
                        eValue
                  from ework.eAttribute with (readpast)
                  where eValue is not null or eValue<>''

            open groupNames
            fetch next from groupNames into @stickettype
            while @@fetch_status = 0
                  begin
                        print @sTicketType
                        
                        select
                              m1.sTicketType as [Group Name],
                              (
                                    select
                                          count(m2.rowid)
                                    from ework.MasterReportData m2 with (readpast)
                                          inner join ework.eAlert e2 with (readpast)
                                                On m2.eFolderId = e2.eFolderId
                                    where m2.sStatus not in ('Completed','Withdrawn','Deleted') and e2.eAlertType<>'' and m2.sTicketType=@sTicketType --'Cash App Request'
                              ) as [Ticket Total],
                              m1.sStatus as [Status],
                              count(m1.sStatus) as [Ticket Count],
                              (
                                    select
                                          sum(case
                                                      when (datediff(ss, m3.eCreationTime, GetDate())>86400) then
                                                            1
                                                      else
                                                            0
                                                end)
                                    from ework.MasterReportData m3 with (readpast)
                                          inner join ework.eAlert e3 with (readpast)
                                                on m3.eFolderId = e3.eFolderId
                                    where m3.sStatus not in ('Completed','Withdrawn','Deleted') and e3.eAlertType<>'' and m3.sTicketType=@sTicketType and m3.sStatus=m1.sStatus
                              ) as [Number of Overdue Tickets]
                        from ework.MasterReportData m1 with (readpast)
                              inner join ework.eAlert e1 with (readpast)
                                    On m1.eFolderId = e1.eFolderId
                        where m1.sStatus not in ('Completed','Withdrawn','Deleted') and e1.eAlertType<>'' and m1.sTicketType=@sTicketType
                        group by m1.sTicketType, m1.sStatus
                        having count(m1.sStatus)>0
                        order by m1.sTicketType, m1.sStatus

                        fetch next from groupNames into @sTicketType
                  end

            close groupNames
            deallocate groupNames
0
Comment
Question by:davidlars99
3 Comments
 
LVL 13

Author Comment

by:davidlars99
ID: 17122183
I could use something like this

m3.sTicketType in (@sTicketType)

where @sTicketType would be comma separated values but that's not what I want yet  ;)
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 total points
ID: 17122211
davidlars99,
> I could use something like this

Yes, You need to create a function for this
Read the following article on how to create the function and how to use it ,


http://www.nigelrivett.net/SQLTsql/InCsvStringParameter.html
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 17123037
sorry don't understand ... where / why do you  want to use UNION?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question