Solved

MS SQL 2005 Convert Vertical Data in Horizontical Row

Posted on 2008-10-23
2
609 Views
Last Modified: 2012-05-05
Sir, I am using following statement to convert Vertical data in Horizontical Rows , but unable to understand error, please help

--Create Data Temp Table
Drop Table #UpdateReceiving

Create Table #UpdateReceiving (JobNo Varchar(25), VslNVoy Varchar(25), DiversionStation Varchar(25), Tonnage Numeric(18,3))
Insert Into #UpdateReceiving(JobNo, VslNVoy, DiversionStation, Tonnage)

Select      b.jobno,b.vslnvoy,a.DiversionStation,
            Tonnag=sum(pnweight)/1000
from IMPCDTCP a Join IMPCD b on a.jobno=b.jobno
Where b.AgentCode='041' and a.del='0' and a.DiversionStation<>''
group by b.jobno,b.vslnvoy,a.sectorOrder, a.Sector, a.DiversionStation
Order by b.jobno,b.vslnvoy,a.sectorOrder, a.Sector, a.DiversionStation

Create Unique Index XI_UpdateReceiving On #UpdateReceiving (jobno,vslnvoy,DiversionStation,Tonnage)

--Select * From #UpdateReceiving

---Create Distinct Head Table
Drop Table #Column
Create Table #Column (Ident Int Identity(0,1), DiversionStation Varchar(100))
Insert Into #Column(DiversionStation)
SELECT DISTINCT DiversionStation FROM #UpdateReceiving
ORDER BY DiversionStation
--Select * From #Column

Create Unique Index XI_Column on #Column (Ident, DiversionStation)

--Create Distinct Row Detail

Drop Table #Row
Create Table #Row (JobNo varchar(20))
Insert Into #Row(JobNo)
SELECT DISTINCT JobNo FROM #UpdateReceiving
order by jobno
--Select * From #Row

Create Unique Index XI_Row on #Row(JobNo)

Declare @ColumnIndex Int
Declare @MaxColumn Int
Declare @ColumnText Varchar(100)
Declare @Sql varchar(8000)


Select       @ColumnIndex=0,
      @MaxColumn=Max(Ident)
from #Column

--print @MaxColumn

While @ColumnIndex <= @MaxColumn
Begin
      Select @ColumnText=DiversionStation
      From #Column
      where @ColumnIndex = Ident


Select @SQL= 'ALTER TABLE #Row ADD ' + QUOTENAME(@ColumnText) + ' Numeric(18,3) Default 0'
Exec   (@SQL)

      SELECT @SQL =       'UPDATE #Row SET ' + QUOTENAME(@ColumnText) + ' = #UpdateReceiving.Tonnage
                     FROM #UpdateReceiving (INDEX(XI_UpdateReceiving), NOLOCK), #Column
                               WHERE #Row.JobNo = #UpdateReceiving.JobNo
                               AND #Column.DiversionStation = #UpdateReceiving.DiversionStation
                               AND #Column.Ident = ' + CAST(@ColumnIndex AS VARCHAR(12))
        EXEC  (@SQL)
--Print @SQL

--Select * from #Row

Select @ColumnIndex = @ColumnIndex + 1

End


Error:

Msg 1018, Level 15, State 1, Line 2
Incorrect syntax near 'INDEX'. If this is intended as a part of a table hint, A WITH keyword and parenthesis are now required. See SQL Server Books Online for proper syntax.
0
Comment
Question by:Mehram
2 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
Comment Utility
it seems that it is ok syntax wise
i would just try to add print statements in order to find the exact statement that causes the error
0
 

Author Comment

by:Mehram
Comment Utility
The above code is creating column correctly. However it is not inserting values into the columns, scren short attached. please help where i am doing mistake.
Image.bmp
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

9 Experts available now in Live!

Get 1:1 Help Now