含有参数的sql拼接语法,实例:
set @sql1='select top 1 BG_LCDVal from ' +@table1+ ' where BE_Equid=''2019072115100246229574887d4000XX'' and BV_VarName=''ABC'' and BG_DateTime >= '''+ @ddate1+ ''' order by BG_DateTime'
转换时间(增加天数)
declare @ddate1 varchar(100) ='2020-08-01' declare @mondate varchar(10)=convert(varchar(30),dateadd(day,1,cast(@ddate2 as datetime)),23)
定义表名:
declare @table1 varchar(100)='[MyDB'+SUBSTRING(@ddate1,6,2)+'].dbo.[B_GatherData'+@temp1+']'