March 2, 2009 at 2:52 am
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
March 2, 2009 at 2:57 am
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!
😎 :hehe:
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply