Solved

using UNION in a while loop

Posted on 2006-07-17
3
430 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
[X]
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
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

636 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