Solved

sql semi-colon WITH error syntax

Posted on 2014-11-19
4
121 Views
Last Modified: 2014-12-02
Hi, I have a problem with the following code, giving the error below.  If I add a semi-colon before 'WITH' as it says to do when I've looked up the problem, it then returns that as an error so am stuck!  Any ideas of the correct syntax?

IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL
    DROP TABLE #TempTable

CREATE TABLE #TempTable(
OrderBy int,
TotalUsers int,
FinancialYear int,
CombYear varchar(20),
Measure varchar(150))



INSERT INTO #TempTable (OrderBy, TotalUsers, FinancialYear, CombYear, Measure)

;With myTable as 
	(SELECT '1' AS OrderBy, count( D1.UserID) AS TotalUsers, FinYear, CombYr, 'Number of Active Accounts Created in Period' AS Name
FROM ext_User AS D1
INNER JOIN Dates
ON CONVERT(VARCHAR, UserCreated, 110) = CONVERT(VARCHAR, Date, 110)
where UserCreated < GetDate() and UserStatusCode = 'A'
GROUP BY FinYear, CombYr)

Select OrderBy, TotalUsers, FinYear, Sum(TotalUsers) OVER(ORDER BY FinYear) AS RollingTotal, CombYr, Name
from myTable

SELECT * FROM #TempTable

Open in new window



Error is:
Msg 156, Level 15, State 1, Line 15
Incorrect syntax near the keyword 'With'.
Msg 319, Level 15, State 1, Line 15
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.



With the semi-colon code is:

IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL
    DROP TABLE #TempTable

CREATE TABLE #TempTable(
OrderBy int,
TotalUsers int,
FinancialYear int,
CombYear varchar(20),
Measure varchar(150))



INSERT INTO #TempTable (OrderBy, TotalUsers, FinancialYear, CombYear, Measure)

;With myTable as 
	(SELECT '1' AS OrderBy, count( D1.UserID) AS TotalUsers, FinYear, CombYr, 'Number of Active Accounts Created in Period' AS Name
FROM ext_User AS D1
INNER JOIN Dates
ON CONVERT(VARCHAR, UserCreated, 110) = CONVERT(VARCHAR, Date, 110)
where UserCreated < GetDate() and UserStatusCode = 'A'
GROUP BY FinYear, CombYr)

Select OrderBy, TotalUsers, FinYear, Sum(TotalUsers) OVER(ORDER BY FinYear) AS RollingTotal, CombYr, Name
from myTable


SELECT * FROM #TempTable

Open in new window


Error message is:
Msg 102, Level 15, State 1, Line 15
Incorrect syntax near ';'.

Thanks in advance
0
Comment
Question by:deborahhowson00
  • 2
4 Comments
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
The semi-colon is correct in that it has to be before any WITH construct.  I think Microsoft does that to make exam questions extra hard.

Without knowing anything about this code, try this...
;
With myTable as (
   SELECT '1' AS OrderBy, count( D1.UserID) AS TotalUsers, FinYear, CombYr, 'Number of Active Accounts Created in Period' AS Name
   FROM ext_User AS D1
      JOIN Dates ON CONVERT(VARCHAR, UserCreated, 110) = CONVERT(VARCHAR, Date, 110)
   WHERE UserCreated < GetDate() and UserStatusCode = 'A'
   GROUP BY FinYear, CombYr)
INSERT INTO #TempTable (OrderBy, TotalUsers, FinancialYear, CombYear, Measure)
SELECT OrderBy, TotalUsers, FinYear, Sum(TotalUsers) OVER(ORDER BY FinYear) AS RollingTotal, CombYr, Name
FROM myTable

Open in new window

0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
The problem is that each SQL statement should/could be terminated with a semi-colon. Due to the usage of the word WITH in other T-SQL statements the parser requires that the last statement before a CTE must be terminated with a colon.
Also your usage of the CTE is incorrect:

IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL
    DROP TABLE #TempTable;

CREATE TABLE #TempTable
    (
      OrderBy INT ,
      TotalUsers INT ,
      FinancialYear INT ,
      CombYear VARCHAR(20) ,
      Measure VARCHAR(150)
    );

WITH    myTable
          AS ( SELECT   '1' AS OrderBy ,
                        COUNT(D1.UserID) AS TotalUsers ,
                        FinYear ,
                        CombYr ,
                        'Number of Active Accounts Created in Period' AS Name
               FROM     ext_User AS D1
                        INNER JOIN Dates ON CONVERT(VARCHAR, UserCreated, 110) = CONVERT(VARCHAR, Date, 110)
               WHERE    UserCreated < GETDATE()
                        AND UserStatusCode = 'A'
               GROUP BY FinYear ,
                        CombYr
             )
    INSERT  INTO #TempTable
            ( OrderBy ,
              TotalUsers ,
              FinancialYear ,
              CombYear ,
              Measure
            )
            SELECT  OrderBy ,
                    TotalUsers ,
                    FinYear ,
                    SUM(TotalUsers) OVER ( ORDER BY FinYear ) AS RollingTotal ,
                    CombYr ,
                    Name
            FROM    myTable;

SELECT  *
FROM    #TempTable;

Open in new window

0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
Comment Utility
Without having the exact tables, it's hard to answer your question.

However, I can confirm that the following does work:

IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL
    DROP TABLE #TempTable

CREATE TABLE #TempTable(
OrderBy int,
TotalUsers int,
FinancialYear int,
GrandTotal int,
CombYear varchar(20),
Measure varchar(150))
go


With myTable as 
	(SELECT '1' AS OrderBy, count( D1.Users) AS TotalUsers, FinYear, 'CombYr' as CombYr, 'Number of Active Accounts Created in Period' AS Name
FROM ext_UserAudit AS D1
--INNER JOIN Dates
--ON CONVERT(VARCHAR, UserCreated, 110) = CONVERT(VARCHAR, Date, 110)
--where UserCreated < GetDate() and UserStatusCode = 'A'
GROUP BY FinYear)
INSERT INTO #TempTable (OrderBy, TotalUsers, FinancialYear, GrandTotal, CombYear, Measure)
Select OrderBy, TotalUsers, FinYear, Sum(TotalUsers) OVER(ORDER BY FinYear) AS RollingTotal, CombYr, Name
from myTable

SELECT * FROM #TempTable

Open in new window


Therefore, no semicolon, and the INSERT INTO goes after the WITH.
0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
@Philip, there is no statement before your CTE. Before the CTE is a GO. This is the SSME batch terminator. Thus no semi-colon is needed. This is a (:totally:) different case.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now