Link to home
Start Free TrialLog in
Avatar of Mehram
MehramFlag for Pakistan

asked on

MS SQL 2005 Convert Vertical Data in Horizontical Row

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.
ASKER CERTIFIED SOLUTION
Avatar of momi_sabag
momi_sabag
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mehram

ASKER

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