Solved

MS SQL 2005 Convert Vertical Data in Horizontical Row

Posted on 2008-10-23
2
614 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
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

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…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

805 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