日期:2014-05-17  浏览次数:20489 次

Must declare the variable '@SerID'
如题,SQL 2000,在调用如下过程时提示"Must declare the variable '@SerID'"
declare @ret int
exec Proc_Insert_TestXmlInfo '11','22','33','44',@ret

存储过程:


--测试XML备份
if object_id('Proc_Insert_TestXmlInfo','P') is not null
drop procedure Proc_Insert_TestXmlInfo;
go

create proc Proc_Insert_TestXmlInfo
@ModelName nvarchar(20),
@BarcodeNo nvarchar(20),
@Station nvarchar(5),
@XmlInfo varchar(4096),
@Ret int output
as
set nocount on;
begin
declare @tb_name nvarchar(20),@SerID int

set @Ret=-1
-- 设置数据表名
if(Month(getdate()) > 6)
begin
set @tb_name = 'Y' + Convert(nvarchar,Year(getdate())) + '_2'
end
else
begin
set @tb_name = 'Y' + Convert(nvarchar,Year(getdate())) + '_1'
end

-- 判断数据表是否存在
if object_id(@tb_name,'U') is null
begin
exec ('CREATE TABLE [dbo].['+@tb_name+'] (
[SerialNo] [numeric](18, 0) IDENTITY (1, 1) not NULL ,
[ModelName] [varchar] (30) COLLATE Chinese_PRC_CI_AS NULL ,
[BarcodeNo] [varchar] (30) COLLATE Chinese_PRC_CI_AS not NULL ,
[Station] [varchar] (5) COLLATE Chinese_PRC_CI_AS not NULL ,
[Information] [varchar] (4096) COLLATE Chinese_PRC_CI_AS not NULL ,
[Upload_Date] [datetime]  default getdate() not NULL 
) ON [PRIMARY]')
end


--查询数据是否存在,如果存在就更新插入日期,否则插入数据
exec('select @SerID=SerialNo from '+@tb_name+'where Information='+@XmlInfo)
if (@@rowcount > 0)
begin
exec ('update from '+@tb_name+'set Upload_Date=getdate() where SerialNo=@SerID')
set @Ret=1
end
else
begin
--插入数据
exec ('insert into '+@tb_name+' (ModelName,BarcodeNo,Station,Information) 
values('''+@ModelName+''','''+@BarcodeNo+''','''+@Station+''','''+@XmlInfo+''') ')
set @Ret=1
end
end
go

------解决方案--------------------
SerialNo=@SerID 这个地方应该是 serialno ='+@serID+''' 这样,lz还需要转换一下@serid为字符型
拼接字符串注意参数的引用。
------解决方案--------------------
整段修改

  --查询数据是否存在,如果存在就更新插入日期,否则插入数据 

DECLARE @SQL NVARCHAR(MAX)
SET @SQL = N'
  DECLARE @SerID INT
  select @SerID=SerialNo from '+@tb_name+'where Information='+@XmlInfo + '     
  if (@@rowcount > 0)         
  begin            
update from '+@tb_name+' set Upload_Date=getdate() where SerialNo=@SerID
set @Ret=1         
   end        
   else        
   begin        &nb