Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS SQL 2005 Convert Vertical Data in Horizontical Row

Posted on 2008-10-23
2
Medium Priority
?
624 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 1000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

824 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