?
Solved

SQL 2000 WHILE LOOP HELP

Posted on 2008-06-20
1
Medium Priority
?
971 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
1 Comment
 
LVL 17

Accepted Solution

by:
Daniel Reynolds earned 300 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Ready to get certified? Check out some courses that help you prepare for third-party exams.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

862 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