Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. IF EXISTS (SELECT id From tempdb.dbo.sysobjects o Where o.xtype in ('U') And o.id = object_id(N'tempdb..#Curr_Dates'))
  2. DROP TABLE [dbo].[#Curr_Dates]
  3. CREATE TABLE #Curr_Dates (ColumnId varchar(50))
  4. DECLARE @TableUnitAllocation SYSNAME
  5.  
  6. SELECT @TableUnitAllocation = 'dbo.CuMe_Curr_Unit_Allocation'
  7. DECLARE @SQLUnitAllocation NVARCHAR(MAX)
  8. SELECT @SQLUnitAllocation ='
  9. SELECT Distinct 2 Id, '''' Office_Allocation_Year, Unit_Allocation_Id Booking_Link_Alloc_Id, ''Total Saleable'' Booking_Link_Program_Id,
  10. ''Total Saleable'' Booking_Link_Program_Id, ''''
  11. Booking_Link_OverBook_Id, '''' Booking_Link_Default, '''' Office_Allocation_Batch_Id, '''' Office_Allocation_Upd_Date,
  12. value,
  13. Null OtherValue,
  14. Convert(Varchar(10), Cast(Substring(Replace(Code, ''Unit_Allocation_'', Unit_Allocation_Year), 1, 4) + ''-'' + SUBSTRING(REPLACE(Code, ''Unit_Allocation_'', Unit_Allocation_Year), 5, 2) + ''-'' + SUBSTRING(REPLACE(Code, ''Unit_Allocation_'', Unit_Allocation_Year), 7, 2) As Datetime), 6) Code,
  15. 3 TypeId
  16. FROM dbo.CuMe_Curr_Unit_Allocation
  17. UNPIVOT (
  18. Value FOR Code IN (' + STUFF((
  19. SELECT ', [' + c.name + ']'
  20. FROM sys.columns c WITH(NOLOCK)
  21. LEFT JOIN (
  22. SELECT i.[object_id], i.column_id
  23. FROM sys.index_columns i WITH(NOLOCK)
  24. WHERE i.index_id = 1
  25. ) i ON c.[object_id] = i.[object_id] AND c.column_id = i.column_id
  26. WHERE c.[object_id] = OBJECT_ID(@TableUnitAllocation)
  27. AND i.[object_id] IS NULL
  28. And c.Name In (Select 'Unit_Allocation_' + ColumnId From dbo.[#Curr_Dates])
  29. --Ignore these Columns
  30. And c.Name Not In ('Unit_Allocation_Id', 'Unit_Allocation_Office_Id', 'Unit_Allocation_Club', 'Unit_Allocation_Resort', 'Unit_Allocation_Unit_Type', 'Unit_Allocation_Date', 'Unit_Allocation_Year', 'Unit_Allocation_Batch_Id', 'Unit_Allocation_Upd_Date')
  31. ORDER BY c.Name
  32. FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '') + ''
  33. print @SQLUnitAllocation
  34. EXEC(@SQLUnitAllocation)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement