Solved

sql semi-colon WITH error syntax

Posted on 2014-11-19
4
127 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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calculate values in an array 18 35
SQL Server Deadlocks 12 50
Counting connections to SQL Server through C# 3 29
T-SQL:  I Want "Summary"--Not "Detail" 6 22
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

920 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

13 Experts available now in Live!

Get 1:1 Help Now