This topic has been archived. It cannot be replied.
-
工作学习 / IT技术讨论 / How to do with a tablename that is a string + a variable? in SQL Server.I need to select some info from a table that tablename contains year and month. eg. there is a table called rpt200503, I tried the way as list below but it does not work. Any one can help?
Declear @yearmonth as varchar(8)
set @yearmonth=cast(year(getdate()) as varchar)+cast(month(getdate()) as varchar)
select * from rpt+@yearmonth
Can you help me? Thanks.
-xxu999(杰姆);
2005-3-26
{366}
(#2203867@0)
-
use dynamic statementDeclear @yearmonth as varchar(8)
declare @strSQL varchar(100)
set @yearmonth=cast(year(getdate()) as varchar)+cast(month(getdate()) as varchar)
select @strSQL='select * from rpt' +@yearmonth
EXEC (@strSQL)
-yangn(Raymond);
2005-3-28
{214}
(#2206312@0)