Solved

SP Error when creating

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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 ?
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

820 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