问题描述
createprocYiReport(@yearint,@monthint)asdeclare@selyearint--年declare@selMonthint--月declare@varUnitvarchar(50)declare@varGroupsvarchar(20)declare@RCountintdeclare@vardayvarchar(50)declare@typeidbitdeclare@varGood_idvarchar(20)declare@Nstrnvarchar(4000)declareRRcursorforselectvarUnit,varGroups,varGood_id,iReceivedCountasRCount,datepart(dd,dcreate_date)asvarday,0astypeidfromtbl_Info_CuttingunionallselectvarUnit,varGroups,varGood_id,iReturnCountasRCount,datepart(dd,dcreate_date)asvarday,1astypeidfromtbl_Info_ReceiveGoodsopenRRfetchnextfromRRinto@varUnit,@varGroups,@varGood_id,@RCount,@varday,@typeidwhile@@fetch_status=0beginset@Nstr=N'insertintoTBL_YieldReport(company,varGroups,varGoodID,varbool,s@varday)values(@varUnit,@varGroups,@varGood_id,@typeid,@RCount)';EXECUTEsp_executesql@NstrendfetchnextfromRRinto@varUnit,@varGroups,@varGood_id,@RCount,@varday,@typeidCLOSERRDEALLOCATERRexecYiReport2008,5报下面的错误:服务器:消息137,级别15,状态2,行1必须声明变量'@varUnit'。
解决方案
解决方案二:
set@Nstr=N'insertintoTBL_YieldReport(company,varGroups,varGoodID,varbool,s@varday)values(@varUnit,@varGroups,@varGood_id,@typeid,@RCount)';修改为set@Nstr=N'insertintoTBL_YieldReport(company,varGroups,varGoodID,varbool,s'+@varday+')values('+@varUnit+','+@varGroups+','+@varGood_id+','+cast(@typeidasnvarchar(1))+','+cast(@RCountasnvarchar(10))+')';