September 2, 2010 at 12:31 pm
I'm trying to write a stored proc and when it runs, it will use today's date as part of the name of a table it will create when it run each time. I can't seem to figure it out.
I want the table name to be "Name_S_YYYYMMDD" and this is what I have so far:
Create table 'Name_S_'+RIGHT('0000'+ convert(varchar(4),YEAR(getdate())),4)
+RIGHT('00' + convert(varchar(2),MONTH(getdate())),2)
+RIGHT('00' + convert(varchar(2),DAY(getdate())),2)
([AccessKey] [uniqueidentifier] NOT NULL ,
[Grantee] [uniqueidentifier] NOT NULL ,
[Permission] [int] NOT NULL ,
[GroupKey] [uniqueidentifier] NULL ,
[RoleKey] [uniqueidentifier] NULL ,
[UserKey] [uniqueidentifier] NULL
) ON [PRIMARY]
GO
Any help would be appreciated.
Thank you.
Rog
September 2, 2010 at 12:57 pm
Will this help
Declare @date Char(10)
set @date = convert(char(10),'2010-09-02 14:56:09.840',101)
PRINT @date
Using GETDATE()
Declare @date Char(10)
set @date = convert(char(10),GETDATE(),101)
SELECT @date
Result will be 09/02/2010 in either case, or can convert to VARCHAR and it works
Declare @date VARCHAR(10)
set @date = REPLACE(convert(VARCHAR(10),GETDATE(),101),'/',''
Above reults: 09022010
September 2, 2010 at 1:08 pm
I tried using a variable to create the table but that didn't seem to work:
create table @variable (fields...)
September 2, 2010 at 1:13 pm
The table name can't be a variable in the CREATE TABLE statement, so you'll have to use dynamic sql:
DECLARE @sql varchar(max);
SET @sql = 'CREATE TABLE dbo.' + QuoteName(convert(char(10),'2010-09-02 14:56:09.840',101)) + ' (ColA int....)';
EXEC (@sql);
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
September 2, 2010 at 1:23 pm
Also, the format that you're looking for in the CONVERT is 112, which gives you YYYYMMDD without the need to use REPLACE to get rid of extraneous characters.
SELECT Convert(varchar(8), Getdate(), 112)
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
September 2, 2010 at 1:37 pm
I tested this and it did work (SQL 2005)
DECLARE @Table NVARCHAR(1000)
SET @Table =
'CREATE TABLE [dbo].[filestats](
[statskey] [int] IDENTITY(1,1) NOT NULL,
[dbname] [varchar](128) NULL,
[fName] [varchar](2048) NULL,
[timeStart] [datetime] NULL,
[readsNum1] [bigint] NULL,
[readsBytes1] [bigint] NULL,
[readsIoStall1] [bigint] NULL,
[writesNum1] [bigint] NULL,
[writesBytes1] [bigint] NULL,
[ioStall1] [bigint] NULL
)'
EXECUTE sp_executesql @Table
Specifically what error message(s) are you getting?
September 2, 2010 at 1:53 pm
Thanks everybody so far... here is what I have that is generating a syntax error.
What I am trying to accomplish... dynamically create the table name with today's date... so today's table would be Name_S_20100902... and then create the fields as shown below. I get an Incorrect syntax near 'AccessKey'. error message so it has something to do with trying to dynamically create the table name and then the fields.
DECLARE @create nvarchar(1000)
SET @create = 'CREATE TABLE dbo.Name_S_'+Convert(varchar(8), Getdate(), 112)
(
[AccessKey] [uniqueidentifier] NOT NULL ,
[Grantee] [uniqueidentifier] NOT NULL ,
[Permission] [int] NOT NULL ,
[GroupKey] [uniqueidentifier] NULL ,
[RoleKey] [uniqueidentifier] NULL ,
[UserKey] [uniqueidentifier] NULL
)
EXECUTE sp_executesql @create
September 2, 2010 at 1:59 pm
Roger Abram (9/2/2010)
Thanks everybody so far... here is what I have that is generating a syntax error.What I am trying to accomplish... dynamically create the table name with today's date... so today's table would be Name_S_20100902... and then create the fields as shown below. I get an Incorrect syntax near 'AccessKey'. error message so it has something to do with trying to dynamically create the table name and then the fields.
DECLARE @create nvarchar(1000)
SET @create = 'CREATE TABLE dbo.Name_S_'+Convert(varchar(8), Getdate(), 112)
(
[AccessKey] [uniqueidentifier] NOT NULL ,
[Grantee] [uniqueidentifier] NOT NULL ,
[Permission] [int] NOT NULL ,
[GroupKey] [uniqueidentifier] NULL ,
[RoleKey] [uniqueidentifier] NULL ,
[UserKey] [uniqueidentifier] NULL
)
EXECUTE sp_executesql @create
You're missing some quotes. Remember, you're building a string and then executing it.
DECLARE @create nvarchar(1000)
SET @create = 'CREATE TABLE dbo.Name_S_'+Convert(varchar(8), Getdate(), 112)+'
(
[AccessKey] [uniqueidentifier] NOT NULL ,
[Grantee] [uniqueidentifier] NOT NULL ,
[Permission] [int] NOT NULL ,
[GroupKey] [uniqueidentifier] NULL ,
[RoleKey] [uniqueidentifier] NULL ,
[UserKey] [uniqueidentifier] NULL
)'
EXECUTE sp_executesql @create
September 2, 2010 at 2:24 pm
Thanks! I kept playing around with where to place them and just didn't get the right combination.
Thank you and thank everyone who jumped in.
Roger
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply