SQL Server 关键字Select [closed]附近的语法不正确

kr98yfug  于 2022-12-17  发布在  其他
关注(0)|答案(1)|浏览(199)

**已关闭。**此问题为not reproducible or was caused by typos。当前不接受答案。

这个问题是由打字错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
2天前关闭。
Improve this question
创建一个函数,当传入VendorID时,该函数将返回供应商的余额。在查询中使用此函数可返回所有CA供应商及其余额

USE AP

GO

CREATE FUNCTION fnBalanceVendor

(@vendorid INT)

    Returns Table

RETURN (SELECT Vendors.VendorID, (InvoiceTotal - PaymentTotal - CreditTotal) AS Balance

        FROM Vendors Join Invoices ON Vendors.VendorID = Invoices.VendorID

        WHERE Vendors.VendorID = @vendorid)



SELECT *

FROM Vendors INNER JOIN dbo.fnBalanceVendor(8) AS ca ON Vendors.VendorID = ca.VendorID

WHERE VendorState = 'CA'

消息156,级别15,状态1,程序fnBalanceVendor,第9行[批处理开始第2行]
关键字'SELECT'附近的语法不正确。
不知道我在这里做错了什么,任何帮助都将不胜感激,谢谢!

nhaq1z21

nhaq1z211#

我建议在CREATE FUNCTION语句中的RETURN之后添加一个“go”来关闭批处理,因此:

USE AP
GO
CREATE FUNCTION fnBalanceVendor
(@vendorid INT)
    Returns Table
RETURN (SELECT Vendors.VendorID, (InvoiceTotal - PaymentTotal - CreditTotal) AS Balance
        FROM Vendors Join Invoices ON Vendors.VendorID = Invoices.VendorID
        WHERE Vendors.VendorID = @vendorid)

GO

SELECT *
FROM Vendors INNER JOIN dbo.fnBalanceVendor(8) AS ca ON Vendors.VendorID = ca.VendorID
WHERE VendorState = 'CA'

相关问题