生成50万条记录的大数据表的TSQL语句。
http://www.cnblogs.com/jiny-z/archive/2006/10/26/540801.html
经常做数据库访问性能测试时,需要用到数据量很大的表,自己动手写一段TSQL语句即可。
TSQL_生成表结构:
经常做数据库访问性能测试时,需要用到数据量很大的表,自己动手写一段TSQL语句即可。
TSQL_生成表结构:
1 /****** 对象: 表 [dbo].[LargeTable] 脚本日期: 2006-10-26 15:40:27 ******/
2 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[LargeTable]')
and OBJECTPROPERTY(id, N'IsUserTable') = 1)
3 drop table [dbo].[LargeTable]
4 GO
5
6 /****** 对象: 表 [dbo].[LargeTable] 脚本日期: 2006-10-26 15:40:27 ******/
7 CREATE TABLE [dbo].[LargeTable] (
8 [ID] [int] IDENTITY (1, 1) NOT NULL ,
9 [Title] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
10 [Content] [ntext] COLLATE Chinese_PRC_CI_AS NULL ,
11 [PublicTime] [datetime] NULL ,
12 [Author] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
13 [IsTop] [tinyint] NULL
14 ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
15 GO
16
17 ALTER TABLE [dbo].[LargeTable] WITH NOCHECK ADD
18 CONSTRAINT [PK_LargeTable] PRIMARY KEY CLUSTERED
19 (
20 [ID]
21 ) ON [PRIMARY]
22 GO
23
24 ALTER TABLE [dbo].[LargeTable] ADD
25 CONSTRAINT [DF_LargeTable_IsTop] DEFAULT (0) FOR [IsTop]
26 GO
27
28
29 exec sp_addextendedproperty N'MS_Description', N'作者', N'user', N'dbo', N'table', N'LargeTable', N'column', N'Author'
30 GO
31 exec sp_addextendedproperty N'MS_Description', N'内容', N'user', N'dbo', N'table', N'LargeTable', N'column', N'Content'
32 GO
33 exec sp_addextendedproperty N'MS_Description', N'文章表,包含100万条记录', N'user', N'dbo', N'table', N'LargeTable', N'column', N'ID'
34 GO
35 exec sp_addextendedproperty N'MS_Description', N'是否置顶 0.不置顶 1.置顶', N'user', N'dbo', N'table', N'LargeTable', N'column', N'IsTop'
36 GO
37 exec sp_addextendedproperty N'MS_Description', N'发布时间', N'user', N'dbo', N'table', N'LargeTable', N'column', N'PublicTime'
38 GO
39 exec sp_addextendedproperty N'MS_Description', N'文章标题', N'user', N'dbo', N'table', N'LargeTable', N'column', N'Title'
40
41
42 GO
43
44
45 TSQL_生成表数据:
46
47 /*truncate table largetable*/
48
49 declare @title nvarchar(100)
50 declare @content nvarchar(100)
51 declare @publictime datetime
52 declare @author nvarchar(10)
53 declare @istop tinyint
54
55 declare @randtime_month tinyint
56 declare @randtime_day tinyint
57 declare @randtime_hour tinyint
58 declare @randtime_minute tinyint
59 declare @randtime_second tinyint
60
61 declare @str varchar(30)
62
63 print '开始执行时间:' + cast(getdate() as varchar)
64 declare @i int,@count int
65 set @i=1
66 set @count=500000
67 while @i<=@count
68 begin
69 set @randtime_month=rand(@i)*12
70 set @randtime_day=rand(@i)*28
71 set @randtime_hour=rand(@i)*24
72 set @randtime_minute=rand(@i)*60
73 set @randtime_second=rand(@i)*60
74 set @str='2006-'+cast(@randtime_month as varchar)+'-'
+cast(@randtime_day as varchar)
+' '+cast(@randtime_hour as varchar)+':'
+cast(@randtime_minute as varchar)+':'
+cast(@randtime_second as varchar)
75
76 set @title='文章标题'+cast(@i as varchar)
77 set @content='文章内容'+cast(@i as varchar)
78 set @publictime=convert(datetime,@str,120)
79 set @author='作者'+cast(@i as varchar)
80 if @i%10000=0
81 set @istop=1
82 else
83 set @istop=0
84
85 insert into largetable values(@title,@content,@publictime,@author,@istop)
86
87 set @i=@i+1
88 end
89 print '执行完毕时间:' + cast(getdate() as varchar)
2 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[LargeTable]')
and OBJECTPROPERTY(id, N'IsUserTable') = 1)
3 drop table [dbo].[LargeTable]
4 GO
5
6 /****** 对象: 表 [dbo].[LargeTable] 脚本日期: 2006-10-26 15:40:27 ******/
7 CREATE TABLE [dbo].[LargeTable] (
8 [ID] [int] IDENTITY (1, 1) NOT NULL ,
9 [Title] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
10 [Content] [ntext] COLLATE Chinese_PRC_CI_AS NULL ,
11 [PublicTime] [datetime] NULL ,
12 [Author] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
13 [IsTop] [tinyint] NULL
14 ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
15 GO
16
17 ALTER TABLE [dbo].[LargeTable] WITH NOCHECK ADD
18 CONSTRAINT [PK_LargeTable] PRIMARY KEY CLUSTERED
19 (
20 [ID]
21 ) ON [PRIMARY]
22 GO
23
24 ALTER TABLE [dbo].[LargeTable] ADD
25 CONSTRAINT [DF_LargeTable_IsTop] DEFAULT (0) FOR [IsTop]
26 GO
27
28
29 exec sp_addextendedproperty N'MS_Description', N'作者', N'user', N'dbo', N'table', N'LargeTable', N'column', N'Author'
30 GO
31 exec sp_addextendedproperty N'MS_Description', N'内容', N'user', N'dbo', N'table', N'LargeTable', N'column', N'Content'
32 GO
33 exec sp_addextendedproperty N'MS_Description', N'文章表,包含100万条记录', N'user', N'dbo', N'table', N'LargeTable', N'column', N'ID'
34 GO
35 exec sp_addextendedproperty N'MS_Description', N'是否置顶 0.不置顶 1.置顶', N'user', N'dbo', N'table', N'LargeTable', N'column', N'IsTop'
36 GO
37 exec sp_addextendedproperty N'MS_Description', N'发布时间', N'user', N'dbo', N'table', N'LargeTable', N'column', N'PublicTime'
38 GO
39 exec sp_addextendedproperty N'MS_Description', N'文章标题', N'user', N'dbo', N'table', N'LargeTable', N'column', N'Title'
40
41
42 GO
43
44
45 TSQL_生成表数据:
46
47 /*truncate table largetable*/
48
49 declare @title nvarchar(100)
50 declare @content nvarchar(100)
51 declare @publictime datetime
52 declare @author nvarchar(10)
53 declare @istop tinyint
54
55 declare @randtime_month tinyint
56 declare @randtime_day tinyint
57 declare @randtime_hour tinyint
58 declare @randtime_minute tinyint
59 declare @randtime_second tinyint
60
61 declare @str varchar(30)
62
63 print '开始执行时间:' + cast(getdate() as varchar)
64 declare @i int,@count int
65 set @i=1
66 set @count=500000
67 while @i<=@count
68 begin
69 set @randtime_month=rand(@i)*12
70 set @randtime_day=rand(@i)*28
71 set @randtime_hour=rand(@i)*24
72 set @randtime_minute=rand(@i)*60
73 set @randtime_second=rand(@i)*60
74 set @str='2006-'+cast(@randtime_month as varchar)+'-'
+cast(@randtime_day as varchar)
+' '+cast(@randtime_hour as varchar)+':'
+cast(@randtime_minute as varchar)+':'
+cast(@randtime_second as varchar)
75
76 set @title='文章标题'+cast(@i as varchar)
77 set @content='文章内容'+cast(@i as varchar)
78 set @publictime=convert(datetime,@str,120)
79 set @author='作者'+cast(@i as varchar)
80 if @i%10000=0
81 set @istop=1
82 else
83 set @istop=0
84
85 insert into largetable values(@title,@content,@publictime,@author,@istop)
86
87 set @i=@i+1
88 end
89 print '执行完毕时间:' + cast(getdate() as varchar)