读书人

存储过程消息 102级别 15状态 1

发布时间: 2012-06-13 12:30:18 作者: rapoo

存储过程消息 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) outputasbegin    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 employeegoif OBJECT_ID('sale','u') is not null drop table salegocreate table employee(    empid int )goinsert into employee select 1gocreate table sale(    salesum int ,    empid int,    saledate datetime    )goinsert into saleselect 1,1,GETDATE()----------------------------------------if OBJECT_ID('GetSaleSumByEmp','p') is not null drop proc GetSaleSumByEmpgo create procedure GetSaleSumByEmp@year int,@month int,@empid nchar(10),@sum numeric(38,2) outputasbegin    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  --这里有问题,不知道楼主想要过滤的条件    endgo---调用存储过程declare @result numeric(38,2)--楼主注意下 使用存储过程使用output时的语法exec GetSaleSumByEmp 2012,6,'n',@result outputselect @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)asbeginselect @sum=sum(sale.salesum)from sale,employeewhere sale.empid=employee.empidand year(saledate)=@yearand month(saledate)=@monthgroup by sale.empid--having saleid=@saleidend 

读书人网 >SQL Server

热点推荐