Solved

using UNION in a while loop

Posted on 2006-07-17
3
410 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
Comment Utility
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
Comment Utility
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
Comment Utility
sorry don't understand ... where / why do you  want to use UNION?
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Move SQL 2005 Express to Server 2012R2 19 68
SQl help with selection 14 43
how to fix this error 14 45
Test a query 23 10
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

763 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

16 Experts available now in Live!

Get 1:1 Help Now