日期:2014-05-18 浏览次数:20568 次
go create table TableA (AID varchar(2),strBID varchar(9)) insert into TableA select 'A1',null union all select 'A2',',B1,B3,' union all select 'A3',',B1,B2,' union all select 'A4',',B1,B4,' go create table TableB (BID varchar(2),UpdateTime datetime) insert into TableB select 'B1','20120130' union all select 'B2','20120131' union all select 'B3','20120201' union all select 'B4','20120202' go create function aa(@num int) returns varchar(100) as begin declare @sql varchar(100) set @sql='' --设置当前日期,正常getdate()即可 declare @t datetime set @t='20120203' select @sql=@sql+AID+',' from TableA a left join TableB b on charindex(','+BID+',',strBID)>0 where b.UpdateTime between dateadd(d,-@num,@t) and @t select @sql=left(@sql,len(@sql)-1) return @sql end select dbo.aa(2) /* A2,A4 */