简体   繁体   中英

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'.

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

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 *

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

You can create something similar with GROUPING SETS but it all comes in one resultset, eg something like:

SELECT AcGroup, AccountNumber, [Description], ShortDescription, SUM( Balance ) Balance,  GROUPING_ID(AcGroup, AccountNumber)
FROM ( SELECT LEFT( AccountNumber, 2 ) AcGroup, * FROM Chart_of_Accounts ) x
GROUP BY GROUPING SETS( (AcGroup), ( AccountNumber, [Description], ShortDescription ), () )

SELECT AcGroup, SUM( Balance ) Balance
FROM ( SELECT LEFT( AccountNumber, 2 ) AcGroup, * FROM Chart_of_Accounts ) x
GROUP BY GROUPING SETS( AcGroup, () )

SELECT AcGroup, SUM( Balance ) Balance
FROM ( SELECT LEFT( AccountNumber, 2 ) AcGroup, * FROM Chart_of_Accounts ) x
GROUP BY AcGroup WITH CUBE

I've added GROUPING_ID() which makes it easier to work out if the source is an original, summary to total row.

I always wondered how you would consume something like that as the multiple resultsets make it difficult to handle. You can't pass it to another stored procedure, you can't copy it paste it directly to Excel (without messing around), passing to a .net client would be awkward. How did you consume the previous code?

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM