首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

存储过程中动态SQL运行出错,该怎么解决

2012-01-13 
存储过程中动态SQL运行出错.net环境提示信息:“第1行:附近有语法错误。”存储过程为:ALTERPROCEDUREPr_Updat

存储过程中动态SQL运行出错
.net环境提示信息:
“第   1   行:   '= '   附近有语法错误。”
存储过程为:


ALTER     PROCEDURE   Pr_UpdatePoint
(
@ManagerID   int,
@departID   int,
@StartTime   datetime,
@EndTime   datetime,
@NowTime   datetime,
@TableName   varchar(64)
)
AS

declare   @sqlcommand   varchar(2000)
declare   @sqlcommand2   varchar(2000)
declare   @sqlcommand3   varchar(2000)
declare   @sqlcommand4   varchar(2000)
declare   @tempID   int
declare@TotalID   int

select   @sqlcommand= 'select   '+rtrim(@TotalID)+ '=TotalID   from   '+@TableName+ '   where   '+rtrim(@TotalID)+ '   is   null '
exec(@sqlcommand)
if   @TotalID   is   null
begin
    select   @sqlcommand2= 'insert   into[TotalAssess]
(TotalPoint,StartTime,EndTime,NowTime)
SELECT   SUM(RealPoint), ' ' '+convert(varchar,@StartTime,120)+ ' ' ', ' ' '+convert(varchar,@EndTime,120)+ ' ' ', ' ' '+convert(varchar,@NowTime,120)+ ' ' '   FROM   '+@TableName+ '   where   TotalID=null   '
exec(@sqlcommand2)


select   @sqlcommand3= 'UPDATE   Assess   set   TotalID= '+str(@TotalID)+ '   where   TotalID   is   null '
exec(@sqlcommand3)

exec( 'update   TotalAssess   set   ManagerID= '+@ManagerID+ ',departID= '+@departID+ ',checkYN= "未审核 "   where   TotalID= '+@tempID+ ' ')

end
 
不知道错在哪里

[解决办法]
ALTER PROCEDURE Pr_UpdatePoint
(
@ManagerID int,
@departID int,
@StartTime datetime,
@EndTime datetime,
@NowTime datetime,
@TableName varchar(64)
)
AS

declare @sqlcommand varchar(2000)
declare @sqlcommand2 varchar(2000)
declare @sqlcommand3 varchar(2000)
declare @sqlcommand4 varchar(2000)
declare @tempID int
declare@TotalID int

select @sqlcommand= 'select '+rtrim(@TotalID)+ '=TotalID from '+@TableName+ ' where '+rtrim(@TotalID)+ ' is null '
exec(@sqlcommand)
if @TotalID is null
begin
select @sqlcommand2= 'insert into[TotalAssess](TotalPoint,StartTime,EndTime,NowTime)
SELECT SUM(RealPoint), ' ' '+convert(varchar,@StartTime,120)+ ' ' ', ' ' '+convert(varchar,@EndTime,120)+ ' ' ', ' ' '+convert(varchar,@NowTime,120)+ ' ' ' FROM '+@TableName+ ' where TotalID is null '
exec(@sqlcommand2)


select @sqlcommand3= 'UPDATE Assess set TotalID= '+str(@TotalID)+ ' where TotalID is null '
exec(@sqlcommand3)

exec( 'update TotalAssess set ManagerID= '+@ManagerID+ ',departID= '+@departID+ ',checkYN= ' '未审核 ' ' where TotalID= '+@tempID+ ' ')

end

这样看看

热点排行