我觉得非常有必要写篇文章来讲一下动态SQL,待补充...
declare @column varchar(10) declare @time varchar(10) declare @time2 varchar(10) declare @sql varchar(4000) set @column=(select 'd'+CONVERT(varchar,DAY(GETDATE()-1))) set @time=(select left(convert(varchar,getdate(),21),7)) set @time2=(select left(convert(varchar,getdate()-1,21),10)) set @sql='update a set a.'+@column+'=b.InWater from WEB_MonthData a , [IPLHSC].[JY].dbo.BAProduceData b where a.Date_Month='''+@time+''' and b.time_stamp='''+@time2+''' and a.ID in(105030) and b.Item=''龙华水司'' update a set a.'+@column+'=b.OutWater from WEB_MonthData a , [IPLHSC].[JY].dbo.BAProduceData b where a.Date_Month='''+@time+''' and b.time_stamp='''+@time2+''' and a.ID in(105027) and b.Item=''龙华水司''' print @sql exec (@sql)