sql语句字符串拼接时技巧运用

declare @strSQL varchar(8000)
declare @week decimal(18,0)
declare @week1 decimal(18,0)

set @week=(select week  from aaaa where date=convert(varchar(10),getdate(),112))
set @week1=@week-1


set @strSQL=''
set @strSQL=@strSQL+'
if object_id(''tempdb..#a'') is not null drop table #a 
create table #a
(
cd int
,student_cd int
,week1 decimal(18,0)
)

insert into #a
select  cd,student_cd,week

from dbo.student'+CONVERT(varchar(50),@week1)+' as a '
--print(@strSQL)
exec(@strSQL)

效果:dbo.student'+CONVERT(varchar(50),@week1)+'

   如:dbo.student2007

 

 

posted @ 2013-02-22 15:39  笑看天下  阅读(364)  评论(0编辑  收藏  举报