Solved

SQL 2000 WHILE LOOP HELP

Posted on 2008-06-20
1
952 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:
xDJR1875 earned 100 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

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

6 Experts available now in Live!

Get 1:1 Help Now