Solved

sql semi-colon WITH error syntax

Posted on 2014-11-19
4
143 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
[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
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 34

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 34

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

751 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