java - SQL Server 2014 错误:必须声明标量变量?在视图中调用函数
问题描述
我在尝试创建的视图中调用函数时遇到问题。出现的错误是“必须声明标量变量“@ProductID”。“(Products.Price + GST)AS TotalPrice”中的 GST 也以红色下划线作为问题。我错过了什么?
这是我的功能:
CREATE FUNCTION fn_ReturnTaxedPrice
(
@ProductID int,
@GST float
)
RETURNS float
AS
BEGIN
DECLARE @Price float
DECLARE @GSTPrice float
SET @Price = (SELECT Price FROM Products WHERE ProductID = @ProductID)
SET @GSTPrice = @Price * (1 - @GST)
RETURN (@GSTPrice)
END
这是我的看法:
CREATE VIEW vw_SalesDetails
AS
SELECT Sales.SaleNo,
Sales.SaleDate,
Customer.FirstName,
Customer.LastName,
Category.Category,
Products.ProductDescription,
Type.Type,
Products.Year,
Products.Price,
dbo.fn_ReturnTaxedPrice(@ProductID, 0.1) AS GST,
(Products.Price + GST) AS TotalPrice
FROM Category
JOIN Customer ON Category.CategoryID = Customer.CategoryID
JOIN Sales ON Customer.CustomerID = Sales.CustomerID
JOIN SalesProducts ON Sales.SaleNo = SalesProducts.SaleNo
JOIN Products ON Products.ProductID = SalesProducts.ProductID
JOIN ProductType ON Products.ProductID = ProductType.ProductID
JOIN Type ON Type.TypeID = ProductType.TypeID
解决方案
我希望这有帮助。
CREATE FUNCTION fn_ReturnTaxedPrice
(
@ProductID int,
@GST float
)
RETURNS money --this is the correct data type for money
AS
BEGIN
DECLARE @Price money
SET @Price = (SELECT Price FROM Products WHERE ProductID = @ProductID)
RETURN @Price * @GST
END
现在应该可以了。
CREATE VIEW vw_SalesDetails
AS
SELECT Sales.SaleNo,
Sales.SaleDate,
Customer.FirstName,
Customer.LastName,
Category.Category,
Products.ProductDescription,
Type.Type,
Products.Year,
Products.Price,
dbo.fn_ReturnTaxedPrice(Products.ProductID, 0.1) AS GST,
dbo.fn_ReturnTaxedPrice(Products.ProductID, 0.1) + Products.Price AS TotalPrice
FROM Category
JOIN Customer ON Category.CategoryID = Customer.CategoryID
JOIN Sales ON Customer.CustomerID = Sales.CustomerID
JOIN SalesProducts ON Sales.SaleNo = SalesProducts.SaleNo
JOIN Products ON Products.ProductID = SalesProducts.ProductID
JOIN ProductType ON Products.ProductID = ProductType.ProductID
JOIN Type ON Type.TypeID = ProductType.TypeID
推荐阅读
- node.js - 在 Heroku (react app) 上部署 Docker 镜像的问题
- java - 在 HttpsURLConnection 中传递 pdf 文件
- ios - NavigationBar 中的自定义 TitleView
- reactjs - 页面加载时,调度未在 useEffect 内触发
- angular - Angular 10:CommonJs 和 AMD 依赖可能导致优化救助,hotkeys.js 依赖于“mousetrap”
- javascript - 在生成的 GUID 值上链接 json 对象
- python - numpy 3d数组中的for循环
- android - 更新 android studio & gradle 插件后未配置 Kotlin
- java - 如何检查是否正在调用特定类或类的方法?
- ios - sceneDidEnterBackground 上的 URL 请求(iOS13)