日期:2014-05-18  浏览次数:20598 次

存储过程消息 102,级别 15,状态 1,过程 GetSaleSumByEmp,第 2 行
create procedure GetSaleSumByEmp
@sum(salesum) output,
@year int,
@month int,
@empid nchar(10)
as
begin
select @sum=sum(sale.salesum)
from sale,employee
where sale.empid=employee.empid
and year(saledate)=@year
and month(saledate)=@month
group by sale.empid
having saleid=@saleid
end

消息 102,级别 15,状态 1,过程 GetSaleSumByEmp,第 2 行
'(' 附近有语法错误。
消息 137,级别 15,状态 1,过程 GetSaleSumByEmp,第 8 行
必须声明标量变量 "@sum"。


怎么改啊?帮帮忙吧

------解决方案--------------------
SQL code

create procedure GetSaleSumByEmp
@year int,
@month int,
@empid nchar(10),
@sum numeric(38,2) output
as
begin
    select @sum=sum(sale.salesum)
    from sale,employee
    where sale.empid=employee.empid
    and year(saledate)=@year    --建议使用[表名.saledate]
    and month(saledate)=@month--建议使用[表名.saledate]
    group by sale.empid
    --having saleid=@saleid  --这里有问题,不知道楼主想要过滤的条件    
end

------解决方案--------------------
SQL code

if OBJECT_ID('employee','u') is not null drop table employee
go
if OBJECT_ID('sale','u') is not null drop table sale
go
create table employee
(
    empid int 
)
go
insert into employee select 1
go
create table sale
(
    salesum int ,
    empid int,
    saledate datetime
    
)
go
insert into sale
select 1,1,GETDATE()
----------------------------------------
if OBJECT_ID('GetSaleSumByEmp','p') is not null drop proc GetSaleSumByEmp
go 
create procedure GetSaleSumByEmp
@year int,
@month int,
@empid nchar(10),
@sum numeric(38,2) output
as
begin
    select @sum=sum(sale.salesum)
    from sale,employee
    where sale.empid=employee.empid
    and year(saledate)=@year    --建议使用[表名.saledate]
    and month(saledate)=@month--建议使用[表名.saledate]
    group by sale.empid
    --having saleid=@saleid  --这里有问题,不知道楼主想要过滤的条件    
end
go
---调用存储过程
declare @result numeric(38,2)
--楼主注意下 使用存储过程使用output时的语法
exec GetSaleSumByEmp 2012,6,'n',@result output
select @result
/*

---------------------------------------
1.00

(1 row(s) affected)
*/

------解决方案--------------------
SQL code

create procedure GetSaleSumByEmp
--@sum(salesum) output, --输出变量 需要有类型 不能有括号在这
@sum float output,
@year int,
@month int,
@empid nchar(10)
as
begin
select @sum=sum(sale.salesum)
from sale,employee
where sale.empid=employee.empid
and year(saledate)=@year
and month(saledate)=@month
group by sale.empid
--having saleid=@saleid
end