我正在尝试设置标量值。需要的结果来自SELECT TOP 1 WITH TIES
块。我该怎么做呢?
我试图做的方式是:
USE AdventureWorks2014
GO
CREATE FUNCTION funcGetLastFridayOrders
RETURNS SMALLINT
AS
BEGIN
DECLARE @orders_quantity SMALLINT
SELECT TOP 1 WITH TIES @orders_quantity = COUNT(*)
FROM Purchasing.PurchaseOrderHeader
WHERE DATEPART(DAY,OrderDate) = 5
GROUP BY OrderDate
ORDER BY OrderDate DESC
RETURN @order_quantity
END
错误讯息:
必须声明标量变量“@order_quantity”
而
DECLARE @var INT
SELECT @var = intValue FROM table WHERE id = 1
工作正常。
答案 0 :(得分:0)
这只是回归线上的一个错字。你写了“order_quantity”而不是“orders_quantity”。
这是更正后的陈述:
USE AdventureWorks2014
GO
CREATE FUNCTION funcGetLastFridayOrders
RETURNS SMALLINT
AS
BEGIN
DECLARE @orders_quantity SMALLINT
SELECT TOP 1 WITH TIES @orders_quantity = COUNT(*)
FROM Purchasing.PurchaseOrderHeader
WHERE DATEPART(DAY,OrderDate) = 5
GROUP BY OrderDate
ORDER BY OrderDate DESC
RETURN @orders_quantity
END
答案 1 :(得分:0)
您有语法错误
DECLARE @orders_quantity SMALLINT
但你使用的是
RETURN @order_quantity