------------------------------------------------------------------------------------------------------------
exec prc_newtb '2011','1', '1-1','1-4'
@year @moth @startdate @ enddate
注意:前面两参数 是年份 和 月份,不可以为空
后面是时间段,值可以为空。现在要创建这样动态的表。如果表已存在则删除重新创建。
表名 见下面实例,来实现
如
exec prc_newtb '2011','1', '1-1','1-4'
得到的表应该为
表名:tb_201101_part1-4 (“201101”是指2011年1月 ,“1-4”这里面的是指 1号到4号的)
列名:ID (自增),RowID(唯一随机数值且为主键,字母和数字都可,只要保证不重复), 2011-1-1 ,2011-1-2,2011-1-3,2011-1-4 (后面都为 字符串类型varchar(50))
------------------------------------------------------------------------------------------------------------
exec prc_newtb '2011','1', '',''
得到的表应该为
表名:tb_201101_all
列名:ID (自增),RowID(唯一随机数值且为主键,字母和数字都可,只要保证不重复), 2011-1-1 ,2011-1-2,2011-1-3,2011-1-4......2011-31 (后面都为 字符串类型varchar(50))
7 个解决方案
#1
'1-4'
建议改为:'1_4'
建议改为:'1_4'
#2
好的.谢谢宝贵的建议
#3
后面是时间段,值可以为空
这个为空,表名是什么规则
这个为空,表名是什么规则
#4
什么意思?
#5
Create proc [dbo].[prc_newtb]
(
@year nvarchar(4),
@moth nvarchar(2),
@startdate nvarchar(10),
@enddate nvarchar(10)
)
AS
declare @newtb nvarchar(max)
declare @tablename nvarchar(50)
declare @begindate1 datetime
declare @enddate1 datetime
declare @date datetime
begin
if(len(@year)<=0 or len(@moth)<=0)
begin
print '年份和月不能为为空!'
end
else
begin
select @tablename='tb_'+LEFT(CONVERT(varchar(100), CAST(@year+'-'+@moth+'-01' as datetime), 112),6)+'_'
if(LEN(@startdate)<=0 or LEN(@enddate)<=0)
begin
set @tablename+='all'
set @begindate1=CAST(@year+'-'+@moth+'-01' as datetime)
set @enddate1=dateadd(day,-1,(DATEADD(MONTH,1,@begindate1)))
end
else
begin
set @begindate1=CAST(@year+'-'+@startdate as datetime)
set @enddate1=CAST(@year+'-'+@enddate as datetime)
set @tablename+='part'+(select substring(@startdate,len(@startdate)-charindex('-',reverse(@startdate))+2,8000))
set @tablename+='_'+(select substring(@enddate,len(@enddate)-charindex('-',reverse(@enddate))+2,8000))
end
set @newtb='IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''[dbo].['+@tablename+']'') AND type in (N''U''))'
set @newtb+=' DROP TABLE [dbo].['+@tablename+']'
set @newtb+=' create table '+@tablename+' ( ID int identity(1,1),RowID nvarchar(50) primary key,'
set @date=@begindate1
while(@date<=@enddate1)
begin
set @newtb+='['+ cast(YEAR(@date) as nvarchar(4))+'-'+cast(MONTH(@date) as nvarchar(2))+'-'+CAST(DAY(@date) as nvarchar(2))+'] nvarchar(50)'
if(@date<>@enddate1)
begin
set @newtb+=','
end
set @date=DATEADD(DAY,1,@date)
end
set @newtb+=' )'
end
exec (@newtb)
end
#6
表明是按建议的格式新建的 如tb_201101_part1_5
#7
兄弟辛苦了,测试无问题,感激感激
#1
'1-4'
建议改为:'1_4'
建议改为:'1_4'
#2
好的.谢谢宝贵的建议
#3
后面是时间段,值可以为空
这个为空,表名是什么规则
这个为空,表名是什么规则
#4
什么意思?
#5
Create proc [dbo].[prc_newtb]
(
@year nvarchar(4),
@moth nvarchar(2),
@startdate nvarchar(10),
@enddate nvarchar(10)
)
AS
declare @newtb nvarchar(max)
declare @tablename nvarchar(50)
declare @begindate1 datetime
declare @enddate1 datetime
declare @date datetime
begin
if(len(@year)<=0 or len(@moth)<=0)
begin
print '年份和月不能为为空!'
end
else
begin
select @tablename='tb_'+LEFT(CONVERT(varchar(100), CAST(@year+'-'+@moth+'-01' as datetime), 112),6)+'_'
if(LEN(@startdate)<=0 or LEN(@enddate)<=0)
begin
set @tablename+='all'
set @begindate1=CAST(@year+'-'+@moth+'-01' as datetime)
set @enddate1=dateadd(day,-1,(DATEADD(MONTH,1,@begindate1)))
end
else
begin
set @begindate1=CAST(@year+'-'+@startdate as datetime)
set @enddate1=CAST(@year+'-'+@enddate as datetime)
set @tablename+='part'+(select substring(@startdate,len(@startdate)-charindex('-',reverse(@startdate))+2,8000))
set @tablename+='_'+(select substring(@enddate,len(@enddate)-charindex('-',reverse(@enddate))+2,8000))
end
set @newtb='IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''[dbo].['+@tablename+']'') AND type in (N''U''))'
set @newtb+=' DROP TABLE [dbo].['+@tablename+']'
set @newtb+=' create table '+@tablename+' ( ID int identity(1,1),RowID nvarchar(50) primary key,'
set @date=@begindate1
while(@date<=@enddate1)
begin
set @newtb+='['+ cast(YEAR(@date) as nvarchar(4))+'-'+cast(MONTH(@date) as nvarchar(2))+'-'+CAST(DAY(@date) as nvarchar(2))+'] nvarchar(50)'
if(@date<>@enddate1)
begin
set @newtb+=','
end
set @date=DATEADD(DAY,1,@date)
end
set @newtb+=' )'
end
exec (@newtb)
end
#6
表明是按建议的格式新建的 如tb_201101_part1_5
#7
兄弟辛苦了,测试无问题,感激感激