SQL 服务器语法更正

SQL Server syntax correction

我正在尝试创建 SQL 服务器存储过程,但出现了一些错误。

create procedure testproc8
as
begin
    declare @x int 
    declare @z int
    declare @y money OUTPUT

    set @y = (select (sum([OrderQty] * [UnitPrice]) / sum([OrderQty])) * @x
              from [dbo].[Transactions]
              where [ProductID] = @z
              group by [ProductID])

    return 
end

错误如下:

Msg 102, Level 15, State 1, Procedure testproc8, Line 6 [Batch Start Line 60]
Incorrect syntax near 'OUTPUT'

Msg 137, Level 15, State 1, Procedure testproc8, Line 7 [Batch Start Line 60]
Must declare the scalar variable "@y"

提前致谢

删除 Output 并仅 select @y 作为您的结果

create proc testproc8
-- Add the parameters for the stored procedure here 
as
begin
declare @x int 
--set @x = certain integer 'if your stored procedure did not have parameters'
declare @z int
--set @z = certain integer 'if your stored procedure did not have parameters'
declare @y money 
set @y = 
(
    select (SUM([OrderQty]*[UnitPrice])/SUM([OrderQty])) * @x
    from [dbo].[Transactions]
    where [ProductID] = @z
    group by [ProductID]
)

select @y
end

试试这个代码:

CREATE PROCEDURE testproc8
@x AS INT,
@z AS INT
AS
BEGIN

DECLARE @y money

set @y = (
    select (SUM([OrderQty]*[UnitPrice])/SUM([OrderQty])) * @x
    from [dbo].[Transactions]
    where [ProductID] = @z
    group by [ProductID]
)

SELECT @y

END