为什么这个')'导致错误?

时间:2016-10-19 23:28:10

标签: sql-server-2012

我想创建一个存储过程spSumInvoices,它将接收感兴趣的供应商ID并打印出供应商的名称和所有发票的总和。

这是我正在使用的代码,不确定它是否正确我是存储过程的新手:

IF OBJECT_ID ('spSumInvoices') IS NOT NULL
    DROP PROC spSumInvoices;
GO

CREATE PROC spSumInvoices
    @FindVen int,
    @InvoiceTotal Money,
    @VenName VARCHAR
AS
BEGIN  
    SELECT 
        @FindVen, SUM(InvoiceTotal) AS 'Total', @VenName
    FROM 
        Vendors,Invoices
    WHERE 
        Invoices.VendorID = @FindVen
    GROUP BY 
        @InvoiceTotal
END

PRINT 'Vendor' + @VenName
PRINT 'Has an invoice total of $' + CONVERT(VARCHAR,@InvoiceTotal);

我收到此错误:

  

Msg 164,Level 15,State 1,Procedure spSumInvoices,Line 15
  每个GROUP BY表达式必须至少包含一个不是外部引用的列。

1 个答案:

答案 0 :(得分:0)

这个问题的答案是:    {USE AP;     如果OBJECT_ID(' spSumInvoices')不是NULL     DROP PROC spSumInvoices;     GO

CREATE PROC spSumInvoices
@FindVen int



AS
BEGIN
DECLARE @VenName Varchar(50);
SET @VenName =(SELECT VendorName FROM Vendors WHERE VendorID = @FindVen);
DECLARE @InvoiceTotal money;
SET @InvoiceTotal = (SELECT SUM(InvoiceTotal) FROM Invoices WHERE
VendorID = @FindVen);


SELECT @VenName,@InvoiceTotal
FROM Vendors,Invoices
WHERE Invoices.VendorID = @FindVen

END

PRINT 'Vendor ' + @VenName
PRINT 'Has an invoice total of $' + CONVERT(VARCHAR,@InvoiceTotal);
enter code here}