Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

dynamically creating a tablename in tsql Expand / Collapse
Author
Message
Posted Monday, March 2, 2009 2:52 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, May 17, 2010 8:40 AM
Points: 6, Visits: 41
hi, i'm trying to dynamically create a tablename in tsql so that the tablename contains the month and year. I keep getting an error that the stored procedure cannot be executed. can someone please tell me where i'm going wrong?

thanks!


declare @strsqlcreatetable as nvarchar(255)
declare @tablename as nvarchar(255)
declare @strsql as nvarchar(255)

select @tablename = 'lclifecycle' + convert(nvarchar(2), datepart(mm, getdate())) + convert(nvarchar(4), datepart(yyyy, getdate()))
print @tablename

select @strsqlcreatetable = 'create table ' + @tablename + '(emp int)'
print @strsqlcreatetable
exec @strsqlcreatetable
Post #666431
Posted Monday, March 2, 2009 2:57 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, July 18, 2014 2:25 AM
Points: 1,262, Visits: 3,419
declare @strsqlcreatetable as nvarchar(255)
declare @tablename as nvarchar(255)
declare @strsql as nvarchar(255)

select @tablename = 'lclifecycle' + convert(nvarchar(2), datepart(mm, getdate())) + convert(nvarchar(4), datepart(yyyy, getdate()))
print @tablename

select @strsqlcreatetable = 'create table ' + @tablename + '(emp int)'
print @strsqlcreatetable
exec (@strsqlcreatetable) --there is your missing parenthesis!



============================================================
SELECT YOUR PROBLEM FROM SSC.com WHERE PROBLEM DESCRIPTION =
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #666434
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse