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

快下班了问一个存储过程
ALTER PROCEDURE dbo.fileInfo_storedProcess
@CMD char(10),
@loginname nvarchar(50)='',
@articleno nvarchar(50)='',
@pagecount int=0,
@figcount int=0,
@tablecount int=0,
@equcount int=0,
@note ntext='',
@newfile bit='0',
@onlineearly bit='0',
@revise bit='0',
@vouchar bit='0',
@datetime datetime='1990-01-01',
@sourcepath nvarchar(50)='',
@purposepath nvarchar(50)=''

AS

 if(@CMD='SELECT')
 
  BEGIN
DECLARE @sqlwhere varchar(4000)
if(@loginname<>'')

set @sqlwhere= @sqlwhere + "where LoginName=@loginname"



if(@articleno<>'')

set @sqlwhere= @sqlwhere+" ArticleNo=@articleno"





END
else if(@CMD='INSERT')
BEGIN
INSERT INTO FileInfor(LoginName,ArticleNo,PageCount,TableCount,EquCount,Note,Newfile,OnLineearly,Revise,Vouchar,DateTime,SourePath,PurposePath) values(@loginname,@articleno,@pagecount,@tablecount,@equcount,@note,@newfile,@onlineearly,@revise,@vouchar,@datetime,@sourcepath,@purposepath)
 
END



RETURN 
报下面的错误
invalid column name "where LoginName=@loginname"
invalid column name "ArticleNo=@articleno"
高手们看看阿。


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

DECLARE @sqlwhere varchar(4000) 
if(@loginname < > ' ') 

set @sqlwhere= @sqlwhere + "where LoginName=@loginname" 



if(@articleno < > ' ') 

set @sqlwhere= @sqlwhere+" ArticleNo=@articleno" 

------------------------
将这两处修改为:
DECLARE @sqlwhere varchar(4000) 
if(@loginname < > ' ') 
set @sqlwhere= @sqlwhere + 'where LoginName=' + @loginname 

if(@articleno < > ' ') 

set @sqlwhere= @sqlwhere+' ArticleNo=' + @articleno
这样试试