Solved

SQL 2000 WHILE LOOP HELP

Posted on 2008-06-20
1
964 Views
Last Modified: 2012-05-05
Hi,

I need to get the single row for a company. Company has participated in more than one event.

I pass EventCode and Event year to this view1, has following details
CompanyName          EventCd                 iShowYear           Status
ABC                            SAA                       2004                    SOLD
ABC                            SAA                       2006                    SOLD
ABC                            SJJ                         2007                    SOLD
ABC                            SHJ                        2008                    HOLD

Let say i pass EventCd SAA and Event Year 2004, 2005, 2006, 2007, 2008 (list box selection)
Result should be
CompanyName  EventCd    Status2004        Status2005        Status2006        Status2007          Status2008
ABC                    SAA            SOLD                    0                     SOLD                     0                          0


i try to use the following query but during each while loop it selects the whole set i.e 5 times selecting all the records. "Status" +@Year1 also giving me the error.
Could anyone help me?

Thank you.

DECLARE @year1 int, @year2 int, @statusyear char(10)


SET @YEAR1 = YEAR(GETDATE())-4
SET @YEAR2 = YEAR(GETDATE())+3

WHILE (@YEAR1 <= @YEAR2)
Select  vchCompanyName, iOWNERID, vchPhonenumber, vchEmailAddress, chCountryDesc,

max(ISNULL((CASE
         WHEN CHorderlinestatus='C' AND iShowYear=@YEAR1 THEN 'SOLD'
         WHEN CHorderlinestatus='B' AND iShowYear=@YEAR1 THEN 'HOLD'
         END
         ), 0)) AS Status_year_@YEAR1

Group BY vchCompanyName, iOWNERID, vchPhonenumber, vchEmailAddress, chCountryDesc, chShowCd, iShowYear

SET @YEAR1 = @YEAR1 +1
END

0
Comment
Question by:aneethat
[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
1 Comment
 
LVL 17

Accepted Solution

by:
Daniel Reynolds earned 100 total points
ID: 21831730
Do not include the @ sign as part of a column name. It is  a reserved character for variables.
so change AS Status_year_@YEAR1 to AS Status_year_YEAR1

0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

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.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.

688 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