Atul Patil

Atul Patil

  • 1k
  • 586
  • 69.4k

Got exception when executing query with CTE cluase

Oct 25 2019 6:08 AM
After executing following query i got exception
"Msg 319, Level 15, State 1, Line 60
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 8180, Level 16, State 1, Procedure sp_describe_parameter_encryption, Line 1 [Batch Start Line 6]
Statement(s) could not be prepared.
An error occurred while executing batch. Error message is: Internal error. Metadata for parameter '@p07290f7aba6643e6897a5d06e06fe5e3' in statement or procedure 'DECLARE @page AS INT = @p07290f7aba6643e6897a5d06e06fe5e3, @size AS INT = @pd9ea99d7d0224318b34dbaa90756f1a9, @search_query AS VARCHAR (50) = @pb5d6ad831f664539a2f4b59bb8cad7d8, @genericSearch AS INT = @pf398ac01c183480eb9d482335baa3eb3, @zone_id AS INT = NULL, @user_id AS INT = @p8504e13806f24366a628538a2f1c8745;"
  1. --GO  
  2. --ALTER PROCEDURE [dbo].[get_zone_locations]  
  3. --@page INT=-1, @size INT=-1, @search_query VARCHAR (50)=NULL, @genericSearch INT=1, @zone_id INT=NULL, @user_id INT=NULL  
  4. --AS  
  5. DECLARE  
  6. @page INT=-1, @size INT=-1, @search_query VARCHAR (50)= '65A', @genericSearch INT=1, @zone_id INT=NULL, @user_id INT=3  
  7. BEGIN  
  8. DECLARE @role_code AS VARCHAR (50);  
  9. DECLARE @venue_id AS INT;  
  10. DECLARE @gender AS VARCHAR (50);  
  11. DECLARE @isLocationAssigned AS INT;  
  12. IF @page = -1  
  13. SET @page = 1;  
  14. IF @size = -1  
  15. SET @size = (SELECT IIF (COUNT(1) > 0, COUNT(1), 1)  
  16. FROM ar_location  
  17. WHERE is_active = 1);  
  18. SELECT @role_code = ur.role_code,  
  19. @gender = u.gender  
  20. FROM users AS u  
  21. INNER JOIN  
  22. user_role AS ur  
  23. ON ur.user_role_id = u.UserRole  
  24. WHERE u.UserId = @user_id  
  25. AND ur.is_active = 1;  
  26. SET @venue_id = (SELECT TOP 1 venue_id  
  27. FROM rel_user_venue  
  28. WHERE user_id = @user_id);  
  29. DECLARE @shift_staff_schedule AS BIT, @location_user_assignment AS BIT, @either_one_or_these AS BIT, @on_duty AS BIT, @location_staff_gender AS BIT;  
  30. SELECT TOP 1 @shift_staff_schedule = uar.shift_staff_schedule,  
  31. @location_user_assignment = uar.location_user_assignment,  
  32. @either_one_or_these = uar.either_one_or_these,  
  33. @on_duty = uar.on_duty,  
  34. @location_staff_gender = uar.location_staff_gender  
  35. FROM user_assignment_rule AS uar  
  36. INNER JOIN  
  37. venue AS v  
  38. ON v.venue_id = uar.venue_id  
  39. WHERE v.venue_id = @venue_id  
  40. AND uar.is_active = 1;  
  41. IF @either_one_or_these = 1  
  42. BEGIN  
  43. SET @shift_staff_schedule = 1;  
  44. SET @location_user_assignment = 1;  
  45. END  
  46. IF @role_code <> 'staff'  
  47. BEGIN  
  48. SET @location_staff_gender = 0;  
  49. END  
  50. SET @isLocationAssigned = 0;  
  51. IF @role_code = 'staff'  
  52. OR @role_code = 'supervisor'  
  53. BEGIN  
  54. SELECT @isLocationAssigned = COUNT(1)  
  55. FROM user_location_assignment  
  56. WHERE user_id = @user_id;  
  57. END;  
  58. WITH restroomList AS  
  59. (  
  60. SELECT DISTINCT CASE WHEN wt.assigned_to > 0 THEN 0 ELSE 1 END AS create_cleaning_alert_btn,  
  61. l.location_id,  
  62. l.location_name,  
  63. ird.infax_restroom_details_id,  
  64. ird.camera_threshold,  
  65. ird.camera_last_reset,  
  66. ird.last_visited,  
  67. ird.camera_last_warn_notify,  
  68. ird.camera_last_max_notify,  
  69. ird.camera_warn_precent,  
  70. ird.current_day_count,  
  71. ird.count_since_reset,  
  72. ird.feedback,  
  73. ird.location_group_name,  
  74. ird.restroom_name,  
  75. ird.restroom_type,  
  76. ird.restroom_description,  
  77. l.longitude,  
  78. l.latitude,  
  79. l.altitude,  
  80. b.building_id,  
  81. b.building_name,  
  82. l.created_by,  
  83. l.created_date,  
  84. l.modified_by,  
  85. l.modified_date,  
  86. v.venue_id,  
  87. v.venue_name,  
  88. z.zone_id,  
  89. z.zone_name,  
  90. l.location_type,  
  91. dbo.fnGetLocationLastCleanedDate(l.location_id) AS last_cleaned_date,  
  92. dbo.fnGetLocationLastCleanedAssigneeName(l.location_id) AS assignee_name,  
  93. --dbo.get_restroom_feedback_by_location(l.location_name) AS feedback,  
  94. blue.created_at AS opened_date,  
  95. CASE WHEN vw.location_id > 0 THEN 1 ELSE 0 END AS is_unassigned,  
  96. --Count(1) OVER () AS total_rows,  
  97. ROW_NUMBER() OVER (PARTITION BY l.location_id ORDER BY l.location_id DESCAS loc_num  
  98. FROM ar_location AS l  
  99. INNER JOIN  
  100. zone AS z  
  101. ON z.zone_id = l.zone_id  
  102. AND z.is_active = 1  
  103. AND l.is_active = 1  
  104. INNER JOIN  
  105. building AS b  
  106. ON b.building_id = z.building_id  
  107. AND b.is_active = 1  
  108. INNER JOIN  
  109. venue AS v  
  110. ON v.venue_id = b.venue_id  
  111. AND v.is_active = 1  
  112. LEFT OUTER JOIN  
  113. staff_schedule AS ss  
  114. ON ss.user_id = @user_id  
  115. AND ss.zone_id = z.zone_id  
  116. AND ss.is_active = 1  
  117. LEFT OUTER JOIN  
  118. user_location_assignment AS ula  
  119. ON ula.user_id = @user_id  
  120. AND ula.is_active = 1  
  121. AND ula.location_id = l.location_id  
  122. LEFT OUTER JOIN  
  123. (SELECT wt.*,  
  124. ROW_NUMBER() OVER (PARTITION BY workorder_id ORDER BY workorder_transaction_id DESCAS NUM  
  125. FROM workorder_transaction AS wt) AS wt  
  126. ON wt.assigned_to = @user_id  
  127. AND NUM = 1  
  128. AND (wt.status_id IN (SELECT status_id  
  129. FROM workorder_status  
  130. WHERE (status = 'Inprogress'  
  131. OR status = 'Assigned'))  
  132. AND @role_code = 'staff')  
  133. LEFT OUTER JOIN  
  134. vwLatestWOTransaction AS blue  
  135. ON blue.location_id = l.location_id  
  136. AND blue.rn = 1  
  137. AND blue.status_id NOT IN (SELECT status_id  
  138. FROM workorder_status  
  139. WHERE (status = 'Close'  
  140. OR status = 'Cancel'))  
  141. LEFT OUTER JOIN  
  142. (SELECT ird.*,  
  143. ROW_NUMBER() OVER (PARTITION BY venue_id, restroom_name ORDER BY infax_restroom_details_id DESCAS r_num  
  144. FROM infax_restroom_details AS ird) AS ird  
  145. ON ird.restroom_name = l.location_name  
  146. AND ird.r_num = 1  
  147. LEFT OUTER JOIN  
  148. vwUnassignedLocations AS vw  
  149. ON vw.location_id = l.location_id  
  150. WHERE (@role_code = 'sysadmin'  
  151. OR (@role_code = 'clientadmin'  
  152. AND v.venue_id = @venue_id))  
  153. OR ((@role_code <> 'sysadmin'  
  154. OR @role_code <> 'clientadmin')  
  155. AND v.venue_id = @venue_id  
  156. AND (@location_staff_gender = 0  
  157. OR (@location_staff_gender = 1  
  158. AND (l.location_type = @gender  
  159. OR l.location_type = 'B')))  
  160. AND (@location_user_assignment = 0  
  161. OR (@location_user_assignment = 1  
  162. AND @either_one_or_these = 0  
  163. AND ula.user_id IS NOT NULL)  
  164. OR (@location_user_assignment = 1  
  165. AND @either_one_or_these = 1  
  166. AND (ula.user_id IS NOT NULL  
  167. OR (@isLocationAssigned = 0  
  168. AND ss.user_id IS NOT NULL))))  
  169. AND (@shift_staff_schedule = 0  
  170. OR ((@shift_staff_schedule = 1  
  171. AND @either_one_or_these = 0  
  172. AND ss.user_id IS NOT NULL)  
  173. OR (@shift_staff_schedule = 1  
  174. AND @either_one_or_these = 1  
  175. AND (ula.user_id IS NOT NULL  
  176. OR (@isLocationAssigned = 0  
  177. AND ss.user_id IS NOT NULL))))))  
  178. --ORDER BY l.location_id DESC  
  179. )  
  180. SELECT *,Count(1) OVER () AS total_rows FROM restroomList  
  181. WHERE loc_num=1 AND ((@genericSearch = 1  
  182. AND ( location_name LIKE '%' + ISNULL(@search_query, location_name) + '%'  
  183. OR venue_name LIKE '%' + ISNULL(@search_query, venue_name) + '%'  
  184. OR building_name LIKE '%' + ISNULL(@search_query, building_name) + '%'  
  185. OR zone_name LIKE '%' + ISNULL(@search_query, zone_name) + '%'))  
  186. OR (@genericSearch <> 1  
  187. AND ( location_name LIKE '%' + ISNULL(@search_query, location_name) + '%'  
  188. OR venue_name LIKE '%' + ISNULL(@search_query, venue_name) + '%'  
  189. OR building_name LIKE '%' + ISNULL(@search_query, building_name) + '%'  
  190. OR zone_name LIKE '%' + ISNULL(@search_query, zone_name) + '%')))  
  191. ORDER BY count_since_reset DESC  
  192. OFFSET ((@page - 1) * @sizeROWS FETCH NEXT @size ROWS ONLY  
  193. END  

Answers (1)