Solved

sql semi-colon WITH error syntax

Posted on 2014-11-19
4
134 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
ID: 40452351
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 33

Expert Comment

by:ste5an
ID: 40452357
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
ID: 40452358
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 33

Expert Comment

by:ste5an
ID: 40452454
@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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

775 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