Guest User

Untitled

a guest
Oct 15th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. declare @DomainCategoryID nvarchar(10)
  2. declare @ProjectType nvarchar(50)
  3. declare @ProjectCuratorID int
  4. declare @Results table (
  5. ID int,
  6. ProjectType nvarchar(50),
  7. ProjectCuratorID int,
  8. Phase nvarchar(50),
  9. Publication nvarchar(50)
  10. )
  11.  
  12. set @DomainCategoryID = N'0154'
  13. set @ProjectType = N'Project'
  14. --set @ProjectCuratorID = 14
  15.  
  16. if @DomainCategoryID is not null
  17. begin
  18. with CatPaths (ID, [Path]) as (
  19. select
  20. c.ID,
  21. convert(nvarchar(1000), N'/ ' + c.ID + N' / ')
  22. from DomainCategory c
  23. left join DomainCategoryParent p on p.CategoryID = c.ID
  24. where p.ParentCategoryID is null
  25. union all
  26. select
  27. c.ID,
  28. convert(nvarchar(1000), n.[Path] + c.ID + N' / ')
  29. from DomainCategory c
  30. inner join DomainCategoryParent p on p.CategoryID = c.ID
  31. inner join CatPaths n on n.ID = p.ParentCategoryID
  32. )
  33.  
  34. insert into @Results (
  35. ID,
  36. ProjectType,
  37. ProjectCuratorID,
  38. Phase,
  39. Publication)
  40. select distinct
  41. p.ID,
  42. p.ProjectType,
  43. p.ProjectCuratorID,
  44. p.Phase,
  45. p.Publication
  46. from CatPaths cp
  47. inner join ProjectDomain pd on pd.DomainCategoryID = cp.ID
  48. inner join ProjectDescription p on p.ID = pd.ProjectID
  49. where cp.[Path] like N'%/ ' + @DomainCategoryID + N' /%'
  50. end
  51.  
  52. else
  53. begin
  54. insert into @Results (
  55. ID,
  56. ProjectType,
  57. ProjectCuratorID,
  58. Phase,
  59. Publication)
  60. select
  61. ID,
  62. ProjectType,
  63. ProjectCuratorID,
  64. Phase,
  65. Publication
  66. from ProjectDescription
  67. end
  68.  
  69.  
  70. if @ProjectType is not null
  71. begin
  72. delete from @Results
  73. where ProjectType <> @ProjectType
  74. end
  75.  
  76. if @ProjectCuratorID is not null
  77. begin
  78. delete from @Results
  79. where ProjectCuratorID <> @ProjectCuratorID
  80. end
  81.  
  82. else
  83. begin
  84. delete from @Results
  85. where Publication = N'Draft'
  86. end;
  87.  
  88. with ProjectWithOneSection as (
  89. select
  90. r.ID,
  91. p.Name,
  92. Preview = (
  93. select top 1 cs.Html
  94. from ProjectDescriptionSection ps
  95. inner join ContentSection cs
  96. on cs.ID = ps.ID
  97. where ps.ProjectID = r.ID
  98. order by ps.[Order] desc
  99. )
  100. from @Results r
  101. inner join ProjectDescription p on p.ID = r.ID
  102. )
  103.  
  104. select * from ProjectWithOneSection
  105. where Preview is not null
Add Comment
Please, Sign In to add comment