Solved

using UNION in a while loop

Posted on 2006-07-17
3
415 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 500 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now