读书人

执行存储过程老提示必须声明标量变量!

发布时间: 2013-12-20 00:23:10 作者: rapoo

执行存储过程老提示必须声明标量变量!来大森帮我看看
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[TPGL_vote]
(

@camid varchar(20),/*活动概要ID*/

@detailsid varchar(20),/*活动明细ID*/
@uid varchar(20),/*用户ID*/
@cou int output /*0 error 投票结束 1投票成功 2 该用户已经投票该活动 3 投票总数超过可投票数 4*/
)
as
/*定义SQL语句变量*/
Declare @sql nvarchar(4000)

Declare @sumvotes int

Declare @countvotes int

Declare @votes int
Declare @uvotes int


begin
/*事物开始*/


begin
/*查询投票活动状态*/
/*set @sql='select @count=count(*) from emp where id=@id'
exec sp_executesql @sql, N'@count int out,@id varchar(20)', @cou out ,@id
*/
set @sql='select @ret= CAM_STATUS from TPGL_CAMPAIGN where CAM_ID=@camid'


exec sp_executesql @sql, N'@ret int out,@camid varchar(20)', @cou out ,@camid
/*0为结束 1 正在进行*/
if(@cou = 0)
begin
set @cou=0;
return @cou;
end
else
begin
/*判断是否已经投票*/
set @sql='select @count=count(*) from TPGL_CAM_USER_MIDDLE where CAM_ID=@detailsid and UID=@uid'
exec sp_executesql @sql, N'@count int out,@detailsid varchar(20),@uid varchar(20)', @cou out ,@detailsid,@uid
if(@cou =1)
begin
/*该用户已投过票*/
set @cou=2;
return @cou
end
else
begin
/*判断是否超过可投票数 查询票数和*/
set @sql=' select @liz=SUM(CAM_VOTES)from TPGL_CAM_DETAILS where CAM_ID=@camid'
exec sp_executesql @sql, N'@liz int out,@camid varchar(20)', @sumvotes out ,@camid
/*查询可投票数*/


set @sql=' select @lizhi=CAM_COUNT from TPGL_CAMPAIGN where CAM_ID=@camid'
exec sp_executesql @sql, N'@lizhi int out,@camid varchar(20)', @countvotes out ,@camid
print @sumvotes
print @countvotes
if (@sumvotes > @countvotes)
begin


set @cou=3;

return @cou
end
else
begin
/*投票成功*/
/*查询已有票数*/
set @sql=' select @lizhii=CAM_VOTES from TPGL_CAM_DETAILS where DETAILS_ID=@detailsid'
exec sp_executesql @sql, N'@lizhii int out,@detailsid varchar(20)', @votes out ,@detailsid


//应该是这句错了 set @uvotes=@votes+1;


/*对已有票数+1投票成功*/
set @sql=' update TPGL_CAM_DETAILS set CAM_VOTES =@uvotes where DETAILS_ID=@detailsid'

exec sp_executesql @sql
set @sql='insert into TPGL_CAM_USER_MIDDLE(UID,DETAILS_ID,CAM_ID) values('+@uid+','+@detailsid+','+@camid+')'
exec sp_executesql @sql
set @cou=1;

return @cou
end

end
end




end


end

消息 137,级别 15,状态 2,第 1 行
必须声明标量变量 "@uvotes"。




















[解决办法]
@uvotes没申明,declare一下,或者在参数里面传一下。
[解决办法]
听说sp_executesql运行之后变量就无效,不知道是不是这个原因
[解决办法]
把上面的声明变量的地方写在这个上面!

//应该是这句错了 set @uvotes=@votes+1;
[解决办法]
太多了,不想看,如果是楼上说的set @uvotes=@votes+1;这句出错,那么你看看在这句之前你有没有对@votes赋初始值了。没赋值就是错的。

读书人网 >SQL Server

热点推荐