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

存储过程中动态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

这样看看