Solved

SQL 2000 WHILE LOOP HELP

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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.
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, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

733 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