Monday, May 27, 2013

having problem in creating table from another table

Try




create table dbo.Depot (Depot_id int identity(1,1), Depot_Name varchar(40))
insert into dbo.Depot (Depot_Name) values ('A'),('B'),('C')

go

declare @SQL nvarchar(max), @Columns nvarchar(max)

select @Columns = STUFF((select ', ' + quotename(Depot_name)
from (select distinct depot_name
FROM dbo.Depot) X order by Depot_Name
FOR XML PATH(''), type).value('.', 'nvarchar(max)'), 1,2, '')

set @SQL = N'SELECT * FROM dbo.Depot PIVOT (MAX(Depot_ID)
FOR Depot_Name IN (' + @Columns +
N')) pvt'

execute (@SQL)





For every expert, there is an equal and opposite expert. - Becker's Law





My blog


No comments:

Post a Comment