SQL Server 的WITH 子句

标签 sql sql-server

使用WITH子句时出现此错误

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 102, Level 15, State 1, Procedure ViewComplaintbyProfile, Line 29
Incorrect syntax near ','.

这是我的程序

ALTER PROCEDURE [dbo].[ViewComplaintbyProfile]

(   @ID int


)

AS
BEGIN
    SET NOCOUNT ON
WITH
one as
     (Select sno = ROW_NUMBER()OVER (order by complaint_id), Complaint_Id, ComplainantName,ComplaintType_id, complaintProfileId,ComplainantProfileId,Description, 
                            Email, Date_Complained, Status, AdminComments, Phone, Evidence,
                           PLevel = CASE PriorityLevel_id WHEN  '1' THEN 'High'
                                     WHEN  '2' THEN 'Medium'
                                     WHEN  '3' THEN 'Low' END ,
                          Complaint_Type = CASE ComplaintType_ID WHEN '1' THEN 'Purchased Contact has incorrect details'
                                      WHEN '2' THEN 'Contacted Profile is already married'
                                      WHEN '3' THEN 'Suspect the Profile has fradudelent contect/credentials'
                                      WHEN '4' THEN 'Suspect the Profile has fake picture'
                                      WHEN '5' THEN 'Profile has obscene or inappropriate content'
                                      WHEN '6' THEN 'Report harassment, offensive remarks, etc., by user'
                                      WHEN '7' THEN 'Miscellaneous issue' END,
         Status1 = CASE Status WHEN 'New' THEN 1
                                       WHEN 'In-Progress' THEN 2
                                       WHEN 'Closed' THEN 3
                                         END

      from Complaints),

      two as
(SELECT sno = ROW_NUMBER()OVER (order by complaint_id), Complaint.complaintProfileId,
      CASE 
           WHEN cast(mmbProfiles.MMB_Id as varchar) IS NOT NULL THEN cast(mmbProfiles.MMB_Id as varchar)
           WHEN cast(UPPMembership.profile_id as varchar) IS NOT NULL THEN 'UPP'
           ELSE 'Not found'
      END as MMBId
  FROM Complaints Complaint
     LEFT JOIN  MMBMembership 
     ON MMBMembership.profile_id = Complaint.complaintProfileId
     left JOIN MMB_BusinessProfiles mmbProfiles
     ON mmbProfiles.MMB_id = MMBMembership.MMB_id
     LEFT JOIN UPPMembership
     ON UPPMembership.profile_id = Complaint.complaintProfileId)

     SELECT one.*,two.MMBId FROM one join two
on one.sno = two.sno
WHERE (ComplaintType_id = @ID)
END

请帮忙

谢谢 太阳

最佳答案

错误消息已经告诉您该怎么做:

.... the previous statement must be terminated with a semicolon.

尝试将 WITH 语句放入其自己的 block 中,方法是在其前面添加分号:

ALTER PROCEDURE [dbo].[ViewComplaintbyProfile]

(   @ID int


)

AS
BEGIN
    SET NOCOUNT ON

; WITH one AS ......
  .........

关于SQL Server 的WITH 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6771410/

相关文章:

android - 在 Sqlite 中使用 AND 的多个 LIKE 语句。

sql - 使用 perl 自动执行 sql 连接

sql - 如何在SQL Server中获取一个月中的天数

sql - SSIS 2008 R2 : ROUND the float column

MySQL触发器错误: 1442

java - HSQLDB 意外标记 :?

sql - Linux SQL 服务器 Windows SSMS 连接

sql-server - 在 SQL Server 中生成直方图

sql - 更改 SQL 表以添加列

c# - 需要 SQL 查询调试