1
-- ======================================================2
3
--列出SQL SERVER 所有表,字段名,主键,类型,长度,小数位数等信息4
5
--在查询分析器里运行即可,可以生成一个表,导出到EXCEL中6
7
-- ======================================================8
9
Select10
11
(Case When A.Colorder=1 Then D.Name Else '' End)表名,12
13
A.Colorder 字段序号,14
15
A.Name 字段名,16
17
(Case When Columnproperty( A.Id,A.Name,'Isidentity')=1 Then '√'Else '' End) 标识,18
19
(Case When (Select Count(*)20
21
From Sysobjects22
23
Where (Name In24
25
(Select Name26
27
From Sysindexes28
29
Where (Id = A.Id) And (Indid In30
31
(Select Indid32
33
From Sysindexkeys34
35
Where (Id = A.Id) And (Colid In36
37
(Select Colid38
39
From Syscolumns40
41
Where (Id = A.Id) And (Name = A.Name))))))) And42
43
(Xtype = 'Pk'))>0 Then '√' Else '' End) 主键,44
45
B.Name 类型,46
47
A.Length 占用字节数,48
49
Columnproperty(A.Id,A.Name,'Precision') As 长度,50
51
Isnull(Columnproperty(A.Id,A.Name,'Scale'),0) As 小数位数,52
53
(Case When A.Isnullable=1 Then '√'Else '' End) 允许空,54
55
Isnull(E.Text,'') 默认值,56
57
Isnull(G.[Value],'') As 字段说明58
59
From Syscolumns A Left Join Systypes B60
61
On A.Xtype=B.Xusertype62
63
Inner Join Sysobjects D64
65
On A.Id=D.Id And D.Xtype='U' And D.Name<>'Dtproperties'66
67
Left Join Syscomments E68
69
On A.Cdefault=E.Id70
71
Left Join Sysproperties G72
73
On A.Id=G.Id And A.Colid = G.Smallid74
75
Order By A.Id,A.Colorder76
77
列出SQL SERVER 所有表、字段定义,类型,长度,一个值等信息78
79
并导出到Excel 中80
81
-- ======================================================82
83
-- Export all user tables definition and one sample value84
85
-- jan-13-2003,Dr.Zhang86
87
-- ======================================================88
89
在查询分析器里运行:90
91
SET ANSI_NULLS OFF92
93
GO94
95
SET NOCOUNT ON96
97
GO98
99
100
101
SET LANGUAGE 'Simplified Chinese'102
103
go104
105
DECLARE @tbl nvarchar(200),@fld nvarchar(200),@sql nvarchar(4000),@maxlen int,@sample nvarchar(40)106
107
108
109
SELECT d.name TableName,a.name FieldName,b.name TypeName,a.length Length,a.isnullable IS_NULL INTO #t110
111
FROM syscolumns a, systypes b,sysobjects d112
113
WHERE a.xtype=b.xusertype and a.id=d.id and d.xtype='U'114
115
116
117
DECLARE read_cursor CURSOR118
119
FOR SELECT TableName,FieldName FROM #t120
121
122
123
SELECT TOP 1 '_TableName ' TableName,124
125
'FieldName ' FieldName,'TypeName ' TypeName,126
127
'Length' Length,'IS_NULL' IS_NULL,128
129
'MaxLenUsed' AS MaxLenUsed,'Sample Value ' Sample,130
131
'Comment ' Comment INTO #tc FROM #t132
133
134
135
OPEN read_cursor136
137
138
139
FETCH NEXT FROM read_cursor INTO @tbl,@fld140
141
WHILE (@@fetch_status <> -1) --- failes142
143
BEGIN144
145
IF (@@fetch_status <> -2) -- Missing146
147
BEGIN148
149
SET @sql=N'SET @maxlen=(SELECT max(len(cast('+@fld+' as nvarchar))) FROM '+@tbl+')'150
151
--PRINT @sql152
153
EXEC SP_EXECUTESQL @sql,N'@maxlen int OUTPUT',@maxlen OUTPUT154
155
--print @maxlen156
157
SET @sql=N'SET @sample=(SELECT TOP 1 cast('+@fld+' as nvarchar) FROM '+@tbl+' WHERE len(cast('+@fld+' as nvarchar))='+convert(nvarchar(5),@maxlen)+')'158
159
EXEC SP_EXECUTESQL @sql,N'@sample varchar(30) OUTPUT',@sample OUTPUT160
161
--for quickly162
163
--SET @sql=N'SET @sample=convert(varchar(20),(SELECT TOP 1 '+@fld+' FROM '+164
165
--@tbl+' order by 1 desc ))'166
167
PRINT @sql168
169
print @sample170
171
print @tbl172
173
EXEC SP_EXECUTESQL @sql,N'@sample nvarchar(30) OUTPUT',@sample OUTPUT174
175
INSERT INTO #tc SELECT *,ltrim(ISNULL(@maxlen,0)) as MaxLenUsed,176
177
convert(nchar(20),ltrim(ISNULL(@sample,' '))) as Sample,' ' Comment FROM #t where TableName=@tbl and FieldName=@fld178
179
END180
181
FETCH NEXT FROM read_cursor INTO @tbl,@fld182
183
END184
185
186
187
CLOSE read_cursor188
189
DEALLOCATE read_cursor190
191
GO192
193
194
195
SET ANSI_NULLS ON196
197
GO198
199
SET NOCOUNT OFF200
201
GO202
203
select count(*) from #t204
205
DROP TABLE #t206
207
GO208
209
210
211
select count(*)-1 from #tc212
213
214
215
select * into ##tx from #tc order by tablename216
217
DROP TABLE #tc218
219
220
221
--select * from ##tx222
223
224
225
declare @db nvarchar(60),@sql nvarchar(3000)226
227
set @db=db_name()228
229
--请修改用户名和口令 导出到Excel 中230
231
set @sql='exec master.dbo.xp_cmdshell ''bcp ..dbo.##tx out c:\'+@db+'_exp.xls -w -C936 -Usa -Psa '''232
233
print @sql234
235
exec(@sql)236
237
GO238
239
DROP TABLE ##tx240
241
GO242
243
244
245
246
247
248
249
-- ======================================================250
251
--根据表中数据生成insert语句的存储过程252
253
--建立存储过程,执行 spGenInsertSQL 表名254
255
--感谢playyuer256
257
-- ======================================================258
259
CREATE proc spGenInsertSQL (@tablename varchar(256))260
261
262
263
as264
265
begin266
267
declare @sql varchar(8000)268
269
declare @sqlValues varchar(8000)270
271
set @sql =' ('272
273
set @sqlValues = 'values (''+'274
275
select @sqlValues = @sqlValues + cols + ' + '','' + ' ,@sql = @sql + '[' + name + '],'276
277
from278
279
(select case280
281
when xtype in (48,52,56,59,60,62,104,106,108,122,127)282
283
then 'case when '+ name +' is null then ''NULL'' else ' + 'cast('+ name + ' as varchar)'+' end'284
285
when xtype in (58,61)286
287
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast('+ name +' as varchar)'+ '+'''''''''+' end'288
289
when xtype in (167)290
291
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'292
293
when xtype in (231)294
295
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'296
297
when xtype in (175)298
299
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'300
301
when xtype in (239)302
303
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'304
305
else '''NULL'''306
307
end as Cols,name308
309
from syscolumns310
311
where id = object_id(@tablename)312
313
) T314
315
set @sql ='select ''INSERT INTO ['+ @tablename + ']' + left(@sql,len(@sql)-1)+') ' + left(@sqlValues,len(@sqlValues)-4) + ')'' from '+@tablename316
317
--print @sql318
319
exec (@sql)320
321
end322
323
324
325
GO326
327
328
329
330
331
332
333
-- ======================================================334
335
--根据表中数据生成insert语句的存储过程336
337
--建立存储过程,执行 proc_insert 表名338
339
--感谢Sky_blue340
341
-- ======================================================342
343
344
345
CREATE proc proc_insert (@tablename varchar(256))346
347
as348
349
begin350
351
set nocount on352
353
declare @sqlstr varchar(4000)354
355
declare @sqlstr1 varchar(4000)356
357
declare @sqlstr2 varchar(4000)358
359
select @sqlstr='select ''insert '+@tablename360
361
select @sqlstr1=''362
363
select @sqlstr2=' ('364
365
select @sqlstr1= ' values ( ''+'366
367
select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case368
369
-- when a.xtype =173 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'370
371
when a.xtype =104 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end'372
373
when a.xtype =175 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'374
375
when a.xtype =61 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'376
377
when a.xtype =106 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'378
379
when a.xtype =62 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'380
381
when a.xtype =56 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end'382
383
when a.xtype =60 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'384
385
when a.xtype =239 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'386
387
when a.xtype =108 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'388
389
when a.xtype =231 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'390
391
when a.xtype =59 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'392
393
when a.xtype =58 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'394
395
when a.xtype =52 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end'396
397
when a.xtype =122 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'398
399
when a.xtype =48 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end'400
401
-- when a.xtype =165 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'402
403
when a.xtype =167 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'404
405
else '''NULL'''406
407
end as col,a.colid,a.name408
409
from syscolumns a where a.id = object_id(@tablename) and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and a.xtype <>36410
411
)t order by colid412
413
414
415
select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename416
417
-- print @sqlstr418
419
exec( @sqlstr)420
421
set nocount off422
423
end424
425
GO