Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

using UNION in a while loop

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

829 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