Solved

SP Error when creating

Posted on 2014-09-08
3
282 Views
Last Modified: 2014-09-08
I am trying to create a new SP that currently have in one database but when I do I get an error. I used the MS SQL option to Script the SP on a new window. This is the code:

----

/****** Object:  StoredProcedure [dbo].[ADMIN_DEL_CONTACT_DUPS]    Script Date: 09/08/2014 16:15:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[ADMIN_DEL_CONTACT_DUPS]
AS
BEGIN

SELECT
   DISTINCT AttyId,userid
FROM
   dbo.Attscont
GROUP BY
   AttyId,userid
HAVING
   COUNT(1) > 1

WITH TMP_DUPS AS(
   SELECT AttyId, userid, rownum = ROW_NUMBER()OVER(PARTITION BY AttyId, userid ORDER BY AttyId, userid)
   FROM dbo.Attscont
)
DELETE FROM TMP_DUPS WHERE rownum > 1

END


GO

----

This is the error I get

Msg 319, Level 15, State 1, Procedure ADMIN_DEL_CONTACT_DUPS, Line 14
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.

-----

I get similar error for this procedure:

---
/****** Object:  StoredProcedure [dbo].[ADMIN_DEL_CASE_DUPS]    Script Date: 09/08/2014 16:15:32 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[ADMIN_DEL_CASE_DUPS]
AS
BEGIN

SELECT
   DISTINCT caseid,userid
FROM
   Atts
GROUP BY
   caseid,userid
HAVING
   COUNT(1) > 1

WITH TMP_DUPS AS(
   SELECT caseid, userid, rownum = ROW_NUMBER()OVER(PARTITION BY caseid, userid ORDER BY caseid, userid)
   FROM Atts
)
DELETE FROM TMP_DUPS WHERE rownum > 1

END

GO
0
Comment
Question by:amucinobluedot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40310788
You need a semi-colon before WITH if another statement precedes WITH:

;WITH TMP_DUPS AS(

Well, technically the ; is after the previous statement, but you can just code it before the WITH :-).
0
 

Author Comment

by:amucinobluedot
ID: 40310806
Let me try ...
0
 

Author Closing Comment

by:amucinobluedot
ID: 40310811
Worked .. thanks !
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question