Solved

MS SQL 2005 Convert Vertical Data in Horizontical Row

Posted on 2008-10-23
2
618 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
[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
2 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
ID: 22785373
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
ID: 22794003
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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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