【问题标题】:SQL Server WITH clauseSQL Server WITH 子句
【发布时间】:2011-07-21 04:56:40
【问题描述】:

使用 WITH 子句时出现此错误

关键字“with”附近的语法不正确。如果这个语句是 公共表表达式、xmlnamespaces 子句或更改跟踪 上下文子句,前一条语句必须以 分号。
消息 102,级别 15,状态 1,过程 ViewComplaintbyProfile,第 29 行
',' 附近的语法不正确。

这是我的程序

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

请帮忙

谢谢 孙

【问题讨论】:

标签: sql sql-server


【解决方案1】:

错误信息已经告诉你该怎么做:

....前面的语句必须以分号结束。

尝试将WITH 语句添加到自己的块中,方法是在其前面加上分号:

ALTER PROCEDURE [dbo].[ViewComplaintbyProfile]

(   @ID int


)

AS
BEGIN
    SET NOCOUNT ON

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

【讨论】:

  • ...或实际终止前一个语句,如错误消息所述。 :-) 无论如何+1,只是让你很难过。
  • @Aaron Bertrand:嘘!! :-) 吹毛求疵... :-)
  • 吹毛求疵是我的中间名...我试图通过将它包含在我的用户名中使其更加明显,但它超出了分配的长度。 :-)
【解决方案2】:

前进的方法是用分号终止 每个 SQL 语句,例如(剪断 CTE 定义以提高可读性):

ALTER PROCEDURE [dbo].[ViewComplaintbyProfile]
(   @ID INT
)
AS 
BEGIN;   <-- HERE

SET NOCOUNT ON;  -- <-- HERE
WITH one AS
     (...),
     two AS
     (...)
   SELECT one.*, two.MMBId 
     FROM one JOIN two
          ON one.sno = two.sno
    WHERE (ComplaintType_id = @ID);  -- <-- HERE
END;  -- <-- HERE

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 2011-08-18
    • 2021-01-29
    • 2011-02-10
    • 1970-01-01
    相关资源
    最近更新 更多