Solved

SQL 2000 WHILE LOOP HELP

Posted on 2008-06-20
1
961 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 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

825 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