5

我的朋友向我发送了他在服务器 2008 中编写的命令,他们可以正常工作,但是我的副本和过去在 2012 中无法正常工作。有什么原因吗?这是代码:

        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); 

这是错误:

消息 156,级别 15,状态 1,第 6 行关键字“计算”附近的语法不正确。

4

3 回答 3

8

COMPUTE在 SQL Server 2012 中不再可用,这就是您收到该错误的原因。请参阅此页面:

它说:

本主题介绍 SQL Server 2012 中不再提供的数据库引擎功能:

*Transact-SQL 语法 | 计算/计算 *

于 2012-09-19T08:59:31.013 回答
3

自 SQL Server 2012 中不推荐使用Compute By 以来,RollUp 的一种 hack - (请参阅“SQL SERVER – 使用 ROLL UP 子句而不是 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

结果

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

或者

你可以使用下面的

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

输出是

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
于 2012-09-21T02:51:53.917 回答
1

您可以使用 GROUPING SETS 创建类似的内容,但它们都包含在一个结果集中,例如:

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

我添加了 GROUPING_ID() ,如果源是原始的,汇总到总行,它可以更容易地计算出来。

我一直想知道你会如何消费这样的东西,因为多个结果集很难处理。你不能将它传递给另一个存储过程,你不能将它直接复制粘贴到 Excel 中(不要乱来),传递给 .net 客户端会很尴尬。你是如何使用之前的代码的?

高温高压

于 2012-09-19T15:22:18.530 回答