Solved

SP Error when creating

Posted on 2014-09-08
3
272 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

739 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