Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL 2008 VS 2012 Error: Incorrect syntax near the keyword 'COMPUTE'

My friend sent me the commands that he wrote in server 2008 and they worked with no problems, mine however from a copy and past did not work with 2012. Is there any reason why? Here is the code:

        Use Kudler_Database
        SELECT  AccountNumber, [Description], ShortDescription,Balance
        FROM Chart_of_Accounts 
        ORDER BY left (AccountNumber, 2)
        COMPUTE SUM(Balance) BY left (AccountNumber, 2)
        COMPUTE SUM(Balance); 

Here is the error :

Msg 156, Level 15, State 1, Line 6 Incorrect syntax near the keyword 'COMPUTE'.

like image 893
BigDaddyCardona Avatar asked Sep 19 '12 08:09

BigDaddyCardona


2 Answers

COMPUTE is no longer available in SQL server 2012, thats why you are getting that error. See this page:

  • Discontinued Database Engine Functionality in SQL Server 2012

It said that:

This topic describes the Database Engine features that are no longer available in SQL Server 2012:

*Transact-SQL syntax | COMPUTE / COMPUTE BY *

like image 65
Mahmoud Gamal Avatar answered Nov 04 '22 13:11

Mahmoud Gamal


A kind of hack with RollUp since Compute By is deprecated in SQL Server 2012 - (see "SQL SERVER – Use ROLL UP Clause instead of COMPUTE BY")

DECLARE @t TABLE(AccountNumber VARCHAR(10),[Description] VARCHAR(100),ShortDescription VARCHAR(100),Balance INT)
INSERT INTO @t SELECT '1234567890','Some Description for 1st Account','Short Description for 1st Account',2000 Union All
SELECT '2345678901','Some Description for 2nd Account','Short Description for 2nd Account',3000 Union All
SELECT '1234567890','Some Description for 1st Account','Short Description for 1st Account',4000

SELECT  
    AccountNumber   
    ,Balance
    ,Total = SUM(Balance)
FROM @t 
GROUP BY AccountNumber,Balance
WITH ROLLUP

Result

AccountNumber   Balance  total
1234567890      2000     2000
1234567890      4000     4000
1234567890      NULL     6000
2345678901      3000     3000
2345678901      NULL     3000
NULL            NULL     9000

OR

you can use the below

DECLARE @t TABLE(AccountNumber VARCHAR(10),[Description] VARCHAR(100),ShortDescription VARCHAR(100),Balance INT)
INSERT INTO @t SELECT '1234567890','Some Description for 1st Account','Short Description for 1st Account',2000 Union All
SELECT '2345678901','Some Description for 2nd Account','Short Description for 2nd Account',3000 Union All
SELECT '1234567890','Some Description for 1st Account','Short Description for 1st Account',4000

;With CTE AS
(
SELECT 
    AccountNumber
    ,[Description]
    ,ShortDescription
    ,Balance
    ,SubTotal = SUM(Balance) OVER (PARTITION BY AccountNumber ORDER BY LEFT (AccountNumber, 2))
    ,Rn = ROW_NUMBER() OVER(PARTITION BY AccountNumber ORDER BY LEFT (AccountNumber, 2))
FROM @t)
SELECT 
    AccountNumber
    ,[Description]
    ,ShortDescription
    ,Balance  = CAST(Balance AS VARCHAR(10))
    ,SubTotal = CASE  WHEN Rn != 1 THEN NULL ELSE SubTotal END
FROM CTE
UNION ALL
SELECT
    ' ', ' ',' ' ,'Total Amount' , SUM(Balance) FROM CTE

The output being

AccountNumber   Description                         ShortDescription                  Balance   SubTotal
1234567890      Some Description for 1st Account    Short Description for 1st Account   2000    6000
1234567890      Some Description for 1st Account    Short Description for 1st Account   4000    NULL
2345678901      Some Description for 2nd Account    Short Description for 2nd Account   3000    3000
                                                                                    Total Amount  9000
like image 43
Niladri Biswas Avatar answered Nov 04 '22 13:11

Niladri Biswas