February 22, 2010 at 9:16 pm
I create table 1 by 1 as follow,
CREATE TABLE [dbo].[DERInfo_201001](
[trnxid] [int] IDENTITY(1,1) NOT NULL,
[DDate] [datetime] NOT NULL,
[TID] [varchar](20) NOT NULL,
[RID] [varchar](20) NOT NULL,
[TripN] [varchar](10) NOT NULL,
[Busn] [varchar](10) NOT NULL,
[dtGrp] [datetime] NOT NULL,
CONSTRAINT [PK_DERInfo_201001_P01] PRIMARY KEY CLUSTERED
(
[TID] ASC,
[dtGrp] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [DERInfo_201001_P01] UNIQUE NONCLUSTERED
(
[TID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[DERInfo_201002](
[trnxid] [int] IDENTITY(1,1) NOT NULL,
[DDate] [datetime] NOT NULL,
[TID] [varchar](20) NOT NULL,
[RID] [varchar](20) NOT NULL,
[TripN] [varchar](10) NOT NULL,
[Busn] [varchar](10) NOT NULL,
[dtGrp] [datetime] NOT NULL,
CONSTRAINT [PK_DERInfo_201002_P01] PRIMARY KEY CLUSTERED
(
[TID] ASC,
[dtGrp] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [DERInfo_201002_P01] UNIQUE NONCLUSTERED
(
[TID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[DERInfo_201003](
[trnxid] [int] IDENTITY(1,1) NOT NULL,
[DDate] [datetime] NOT NULL,
[TID] [varchar](20) NOT NULL,
[RID] [varchar](20) NOT NULL,
[TripN] [varchar](10) NOT NULL,
[Busn] [varchar](10) NOT NULL,
[dtGrp] [datetime] NOT NULL,
CONSTRAINT [PK_DERInfo_201003_P01] PRIMARY KEY CLUSTERED
(
[TID] ASC,
[dtGrp] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [DERInfo_201003_P01] UNIQUE NONCLUSTERED
(
[TID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
DERInfo_201001 means, DERInfo in Jan 2010.
DERInfo_201002 means, DERInfo in Feb 2010.
DERInfo_201003 means, DERInfo in Mar 2010.
Let's say, i want to create DERInfo table between Jan 2010 till Nov 2010.
I want to using loop, and my SQL program will do it dynamically. How my create table looks like?
February 22, 2010 at 11:41 pm
Is this you are looking for?
declare @tblStr varchar(max)
declare @intMnth int
set @intMnth =1
while @intmnth < 12
begin
set @tblStr = 'CREATE TABLE [dbo].[DERInfo_2010'+cast(@intmnth as varchar(10))+'](
[trnxid] [int] IDENTITY(1,1) NOT NULL,
[DDate] [datetime] NOT NULL,
[TID] [varchar](20) NOT NULL,
[RID] [varchar](20) NOT NULL,
[TripN] [varchar](10) NOT NULL,
[Busn] [varchar](10) NOT NULL,
[dtGrp] [datetime] NOT NULL,
CONSTRAINT [PK_DERInfo_2010'+cast(@intmnth as varchar(10))+'_P'+cast(@intmnth as varchar(10))+'] PRIMARY KEY CLUSTERED
(
[TID] ASC,
[dtGrp] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [DERInfo_2010'+cast(@intmnth as varchar(10))+'_P'+cast(@intmnth as varchar(10))+'] UNIQUE NONCLUSTERED
(
[TID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]'
set @intMnth = @intMnth + 1
exec(@tblStr)
end
February 23, 2010 at 4:00 am
NewBeeSQL (2/22/2010)
Is this you are looking for?
declare @tblStr varchar(max)
declare @intMnth int
set @intMnth =1
while @intmnth < 12
begin
set @tblStr = 'CREATE TABLE [dbo].[DERInfo_2010'+cast(@intmnth as varchar(10))+'](
[trnxid] [int] IDENTITY(1,1) NOT NULL,
[DDate] [datetime] NOT NULL,
[TID] [varchar](20) NOT NULL,
[RID] [varchar](20) NOT NULL,
[TripN] [varchar](10) NOT NULL,
[Busn] [varchar](10) NOT NULL,
[dtGrp] [datetime] NOT NULL,
CONSTRAINT [PK_DERInfo_2010'+cast(@intmnth as varchar(10))+'_P'+cast(@intmnth as varchar(10))+'] PRIMARY KEY CLUSTERED
(
[TID] ASC,
[dtGrp] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [DERInfo_2010'+cast(@intmnth as varchar(10))+'_P'+cast(@intmnth as varchar(10))+'] UNIQUE NONCLUSTERED
(
[TID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]'
set @intMnth = @intMnth + 1
exec(@tblStr)
end
Yes. But i need the table name in format YYYYMM such as Jan 2010 is DERInfo_201001. currently, your code create DERInfo_20101 for Jan 2010
February 23, 2010 at 6:49 am
Hi,
I have made a little changes in the above code
declare @tblStr varchar(max)
declare @intMnth int
declare @strMnth varchar(10)
set @intMnth =1
while @intmnth <= 12
begin
set @strMnth = case when len(cast(@intmnth as varchar(10))) = 1 then '0' + cast(@intmnth as varchar(10)) else cast(@intmnth as varchar(10)) end
set @tblStr = 'CREATE TABLE [dbo].[DERInfo_2010'+@strMnth+'](
[trnxid] [int] IDENTITY(1,1) NOT NULL,
[DDate] [datetime] NOT NULL,
[TID] [varchar](20) NOT NULL,
[RID] [varchar](20) NOT NULL,
[TripN] [varchar](10) NOT NULL,
[Busn] [varchar](10) NOT NULL,
[dtGrp] [datetime] NOT NULL,
CONSTRAINT [PK_DERInfo_2010'+cast(@intmnth as varchar(10))+'_P'+cast(@intmnth as varchar(10))+'] PRIMARY KEY CLUSTERED
(
[TID] ASC,
[dtGrp] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [DERInfo_2010'+cast(@intmnth as varchar(10))+'_P'+cast(@intmnth as varchar(10))+'] UNIQUE NONCLUSTERED
(
[TID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]'
set @intMnth = @intMnth + 1
exec(@tblStr)
end
Hope this solves ur problem
February 23, 2010 at 8:52 am
What is the specific reason for building one table per month instead of adding the related column to one table and (if required) use horizontal partitioning?
How many rows you end up with for one of your monthly tables?
How do you handle quarterly/annual reports? Using UNION ALL, I'm afraid?
I'd question the db design...
February 24, 2010 at 4:55 am
The tables have been segmented because of performance issue. Me going to using Partitioned Views technology.
How about that?
February 24, 2010 at 5:05 am
Im with Lutz on this one.
Not knowing your performance problem , its hard to be specific but a partitioned table would be a better solution.
February 24, 2010 at 6:25 am
I agree with the others... IF you have the Enterprise edition, then partitioned tables would probably be the way to go.
If you don't have the Enterprise edition, then the separate tables you're trying to make and correct construction of the necessary "partitioned views" can be a big help.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 24, 2010 at 3:13 pm
tq to all. all your answer is my inspiration.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply