sql: sq_helptext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
--查看表生成脚本 sql server     --- '\r'是回车,'\n'是换行  /t相当于键盘的Tab键    --- 操作系统的不同,换行符操也不同:/r Mac /n Unix/Linux /r/n Windows
USE   [master]   --设定为系统用的,就是在各数据库中都可以调用。如果写在当前数据库中,只能用于当前数据库
  GO
  SET   ANSI_NULLS   ON  
  GO  
  SET   QUOTED_IDENTIFIER   ON  
  GO  
  /* 
  ****************************************************************************  
    功能描述:   获取指定表的创建脚本,包括表和字段的属性、外键(注释掉的)  
  ----------------------------------------------------------------------------  
    参数列表:    
                  1:   @TableName   需要创建脚本的表的名称  
  ****************************************************************************  
 */ 
  IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_HelpTable]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_HelpTable]
GO
  CREATE   PROCEDURE   [dbo].[sp_HelpTable](@TableName   sysname)  
  AS  
      SET   NOCOUNT   ON  
      DECLARE   @ObjectID   int  
      DECLARE   @TableScript   table(Iden   int   IDENTITY(1,   1),   ScriptLine   nvarchar(4000))  
      SET   @ObjectID   =   object_id(@TableName)  
      IF   @ObjectID   IS   NULL   OR   OBJECTPROPERTY(@ObjectID,   'IsTable')   =   0  
      BEGIN  
          RAISERROR('指定的对象不是表对象',   16,   1)  
          RETURN  
      END    
      --获取表的创建脚本  
      --插入表头  
      INSERT   INTO   @TableScript(ScriptLine)  
          SELECT   N'CREATE   TABLE   ['   +   USER_NAME(OBJECTPROPERTY(@ObjectID,   N'OwnerId'))   +   N'].['   +   object_name(@ObjectID)   +   N']('  
     
      --插入字段  
      INSERT   INTO   @TableScript(ScriptLine)  
          SELECT   N'     ['   +   a.Name   +   N']   ['   +   b.name   +   N']'   +    
                        CASE   WHEN   c.Object_id   IS   NOT   NULL   THEN   N'   IDENTITY('   +   CONVERT(nvarchar,   c.seed_value)   +   N',   '   +   CONVERT(nvarchar,   c.increment_value)   +   N')'    
                                  ELSE   ''   END   +  
                        CASE   WHEN   b.xusertype   IN   (167,   175,   231,   239)   THEN   N'('+CONVERT(nvarchar,   a.prec)     +   N')'  
                                  WHEN   b.xusertype   in   (106,   108)                       THEN   N'('+CONVERT(nvarchar,   a.xprec)   +   N',   '   +   CONVERT(nvarchar,   a.xscale)   +   N')'  
                                  ELSE   ''   END   +  
                        CASE   a.isnullable   WHEN   1   THEN   N''   ELSE   N'   NOT'   END   +   N'   NULL'   +  
                        CASE   WHEN   d.Name   IS   NOT   NULL   THEN   N'   DEFAULT   '   +   d.Definition   ELSE   N''   END   +  
                        N','    
              FROM   sys.syscolumns   a  
                  LEFT   JOIN   sys.systypes   b   ON   a.xusertype   =   b.xusertype  
                  LEFT   JOIN   sys.identity_columns   c   ON   c.Object_id   =   a.ID   AND   c.Column_ID   =   a.ColID  
                  LEFT   JOIN   sys.default_constraints   d   ON   d.Parent_Object_ID   =   a.ID   AND   d.Parent_column_ID   =   a.ColID  
          WHERE   a.[ID]   =   @ObjectID  
          ORDER   BY   a.ColOrder  
     
      --插入主键和索引  
      DECLARE   @IndexID   int,   @IndexScript   nvarchar(4000)  
      DECLARE   IndexCursor   CURSOR   FOR    
          SELECT   b.Index_ID,   N'     CONSTRAINT   ['   +   a.Name   +   N']   '   +    
                        CASE   a.Type   WHEN   'PK'   THEN   N'PRIMARY   KEY   '   WHEN   'UQ'   THEN   N'UNIQUE   '   END   +    
                        CASE   b.Type   WHEN   1         THEN   N'CLUSTERED'       WHEN   2           THEN   N'NONCLUSTERED   '   END   +   N'('  
              FROM   sys.key_constraints   a  
                  LEFT   JOIN   sys.indexes   b   ON   b.Object_ID   =   a.Parent_Object_ID   AND   b.index_id   =   a.unique_index_id  
          WHERE   a.Parent_Object_ID   =   @ObjectID  
      OPEN   IndexCursor  
      FETCH   NEXT   FROM   IndexCursor   INTO   @IndexID,   @IndexScript  
      WHILE   @@FETCH_STATUS   =   0  
      BEGIN  
          SELECT   @IndexScript   =   @IndexScript   +   N'['   +   INDEX_COL(object_name(@ObjectID),   2   ,   1)   +   N'],'  
              FROM   sys.index_columns    
          WHERE   Object_ID   =   @ObjectID  
              AND   Index_ID   =   2  
             
          SET   @IndexScript   =   LEFT(@IndexScript,   LEN(@IndexScript)   -1)   +   N'),'  
          INSERT   INTO   @TableScript(ScriptLine)   VALUES(@IndexScript)  
     
          FETCH   NEXT   FROM   IndexCursor   INTO   @IndexID,   @IndexScript  
      END  
      CLOSE   IndexCursor  
      DEALLOCATE   IndexCursor  
     
      --除去最后一个,号  
      UPDATE   @TableScript    
          SET   ScriptLine   =   LEFT(ScriptLine,   LEN(ScriptLine)   -   1)  
      WHERE   Iden   =   (SELECT   MAX(Iden)   FROM   @TableScript)  
      INSERT   INTO   @TableScript(ScriptLine)   VALUES(N')')  
      INSERT   INTO   @TableScript(ScriptLine)   VALUES(N'GO')  
      INSERT   INTO   @TableScript(ScriptLine)   VALUES(N'')  
    --获取表备注  
      DECLARE   @PropScript   nvarchar(4000)  
      INSERT   INTO   @TableScript(ScriptLine)  
          SELECT   N'EXEC   sys.sp_addextendedproperty   @name=N'''   +   a.Name   +   N''',   @value=N'''   +   CONVERT(nvarchar,   a.Value)   +   N'''   ,@level0type=N''SCHEMA'',   @level0name=N'''   +  
                        USER_NAME(OBJECTPROPERTY(a.major_Id,   N'OwnerId'))   +   N''',   @level1type=N''TABLE'',   @level1name=N'''   +   b.Name   +   N''''  
              FROM   sys.extended_properties   a  
                  LEFT   JOIN   sys.objects   b   ON   b.[Object_ID]   =   a.major_Id  
          WHERE   a.major_Id   =   @ObjectID  
              AND   Minor_ID   =   0  
      INSERT   INTO   @TableScript(ScriptLine)   VALUES('GO')  
     
      DECLARE   PropCursor   CURSOR   FOR    
          SELECT   N'EXEC   sys.sp_addextendedproperty   @name=N'''   +   a.Name   +   N''',   @value=N'''   +   CONVERT(nvarchar,   a.Value)   +   N'''   ,@level0type=N''SCHEMA'',   @level0name=N'''   +  
                        USER_NAME(OBJECTPROPERTY(a.major_Id,   N'OwnerId'))   +   N''',   @level1type=N''TABLE'',   @level1name=N'''   +   b.Name   +   N''''   +  
                        N',   @level2type=N''COLUMN'',   @level2name=N'''   +   c.[Name]   +   ''''  
              FROM   sys.extended_properties   a  
                  LEFT   JOIN   sys.objects   b   ON   b.[Object_ID]   =   a.major_Id  
                  LEFT   JOIN   sys.syscolumns   c   ON   c.[ID]   =   a.major_Id   AND   c.ColID   =   a.Minor_ID  
          WHERE   a.major_Id   =   @ObjectID  
              AND   Minor_ID   <>   0  
      OPEN   PropCursor  
      FETCH   NEXT   FROM   PropCursor   INTO   @PropScript  
      WHILE   @@FETCH_STATUS   =   0  
      BEGIN  
          INSERT   INTO   @TableScript(ScriptLine)   VALUES(@PropScript)  
          INSERT   INTO   @TableScript(ScriptLine)   VALUES(N'GO')  
     
          FETCH   NEXT   FROM   PropCursor   INTO   @PropScript  
      END  
      CLOSE   PropCursor  
      DEALLOCATE   PropCursor  
      INSERT   INTO   @TableScript(ScriptLine)   VALUES('')  
     
      --获取表外键  
      DECLARE   @ConstID   int,   @i   tinyint,   @keyCnt   tinyint,   @TempletSQL   nvarchar(400),   @SQLScript   nvarchar(500),  
                      @FColName   sysname,   @RColName   sysname,  
                      @ForeignLine   nvarchar(4000),   @ReferencesLine   nvarchar(4000),   @ReferencesAction   nvarchar(4000)  
      DECLARE   @ConstIDTable   table(ConstID   int)  
      SELECT  
          @FColName   =   '',  
          @RColName   =   '',  
          @TempletSQL   =    
              N'SELECT   @eFColName   =   ''[''   +   col_name(FkeyID,   Fkey%d)   +   '']'',   @eRColName   =   ''[''   +   col_name(RkeyID,   Rkey%d)   +   '']''   FROM   sys.sysreferences   WHERE   ConstID   =   @ConstID'  
     
      INSERT   INTO   @ConstIDTable  
          SELECT   ConstID   FROM   sys.sysreferences   WHERE   FKeyID   =   @ObjectID   OR   RKeyID   =   @ObjectID   ORDER   BY   FKeyID  
     
      WHILE   EXISTS(SELECT   *   FROM   @ConstIDTable)  
      BEGIN  
          SELECT   TOP   1   @ConstID   =   ConstID   FROM   @ConstIDTable  
          DELETE   FROM   @ConstIDTable   WHERE   ConstID   =   @ConstID  
     
          INSERT   INTO   @TableScript  
              SELECT   N'--ALTER   TABLE   [dbo].['   +   object_name(FKeyID)   +   ']   WITH   CHECK'  
                  FROM   sys.sysreferences  
              WHERE   ConstID   =   @ConstID  
          INSERT   INTO   @TableScript(ScriptLine)   VALUES('--     ADD'   +   CHAR(13)   +   CHAR(10))  
     
          SELECT   @ForeignLine   =   N'--         CONSTRAINT   ['   +   object_name(ConstID)   +   ']   FOREIGN   KEY(',    
                        @ReferencesLine   =   N'REFERENCES   [dbo].['   +   object_name(RKeyID)   +   ']   (',    
                        @ReferencesAction   =   CASE   b.Delete_Referential_Action   WHEN   0   THEN   N''    
                                                                                                                                  WHEN   1   THEN   N'ON   DELETE   Cascade'  
                                                                                                                                  WHEN   2   THEN   N'ON   DELETE   SET   NULL'  
                                                                                                                                  WHEN   3   THEN   N'ON   DELETE   SET   DEFAULT'  
                                                                END   +   '   '   +  
                                                                CASE   b.Delete_Referential_Action   WHEN   0   THEN   N''    
                                                                                                                                  WHEN   1   THEN   N'ON   UPDATE   Cascade'  
                                                                                                                                  WHEN   2   THEN   N'ON   UPDATE   SET   NULL'  
                                                                                                                                  WHEN   3   THEN   N'ON   UPDATE   SET   DEFAULT'  
                                                                END,  
                        @keyCnt   =   KeyCnt    
              FROM   sys.sysreferences   a  
                  LEFT   JOIN   sys.foreign_keys   b   ON   a.ConstID   =   b.Object_ID  
          WHERE   a.ConstID   =   @ConstID  
             
          --取字段  
          SET   @i   =   1  
          WHILE   @i   <=   @keyCnt  
          BEGIN  
              SET   @SQLScript   =   REPLACE(@TempletSQL,   '%d',   CONVERT(nvarchar,   @i))  
              EXEC   sp_executesql   @stmt   =   @SQLScript,   @params=   N'@eFColName   sysname   output,   @eRColName   sysname   output,   @ConstID   int',    
                                                    @eFColName   =   @FColName   output,   @eRColName   =   @RColName   output,   @ConstID   =   @ConstID  
              print   @SQLScript  
              SET   @ForeignLine   =   @ForeignLine   +   CASE   WHEN   @i   >   1   THEN   ',   '   ELSE   ''   END   +   @FColName  
              SET   @ReferencesLine   =   @ReferencesLine   +   CASE   WHEN   @i   >   1   THEN   ',   '   ELSE   ''   END   +   @RColName  
              SET   @i   =   @i   +   1  
          END  
     
          INSERT   INTO   @TableScript(ScriptLine)   VALUES(@ForeignLine   +   N')   '   +   @ReferencesLine   +   N')')  
          IF   @ReferencesAction   <>   ''  
              INSERT   INTO   @TableScript(ScriptLine)   VALUES(@ReferencesAction)  
          --INSERT   INTO   @TableScript(ScriptLine)   VALUES(N'GO')  
      END  
      --返回表的创建脚本  
      SELECT   ScriptLine   FROM   @TableScript  
      SET   NOCOUNT   OFF
 GO
 
--测试
USE LibrarySystem
EXEC sp_HelpTable 'BookInfoList'
 
对于查看视图,存储过程等系统有现成的.sp_helptext
 
 
 
---表结构
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[sp_table]
(@tableName varchar(200)
,@ColumnLike varchar(200)=NULL)          
as          
--/********************************************          
          
--根据表名得到表信息,包括字段说明          
            
--*********************************************/        
    
--DECLARE @tableName VARCHAR(200);
--DECLARE @ColumnLike VARCHAR(200);
  
--SET @tableName='purchase'
--SET @ColumnLike=NULL;
    
--如果表明不存在,就直接选出相似表  
if not exists( select 1 from sysobjects where id = object_id(@tableName)  and type = 'U')  
begin  
 select name from sysobjects where name like '%'+@tableName + '%'  and type = 'U'   
 return  
end     
    
--筛选相似列明  
if(@ColumnLike is null)    
 set @ColumnLike = ''    
declare @ColumnTable table(cName varchar(200))    
insert @ColumnTable(cName)    
select a.name from syscolumns a,sysobjects d    
where  a.id=d.id    
and d.name = @tableName and a.name like '%'+ @ColumnLike +'%'    
        
--查询表结构信息             
  SELECT               
  表名=case   when   a.colorder=1   then   d.name   else   ''   end,             
  表说明=case   when   a.colorder=1   then   isnull(f.value,'')   else   ''   end,             
  字段序号=a.colorder,             
  字段名=a.name,          
 字段说明=isnull(g.[value],''),     
  标识=case   when   COLUMNPROPERTY(   a.id,a.name,'IsIdentity')=1   then   '√'else   ''   end,             
  主键=case   when   exists(SELECT   1   FROM   sysobjects   where   xtype='PK'   and   parent_obj=a.id   and   name   in   (             
  SELECT   name   FROM   sysindexes   WHERE   indid   in(             
  SELECT   indid   FROM   sysindexkeys   WHERE   id   =   a.id   AND   colid=a.colid             
  )))   then   '√'   else   ''   end,             
  类型=b.name,             
  占用字节数=a.length,             
  长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'),             
  小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),             
  允许空=case   when   a.isnullable=1   then   '√'else   ''   end,             
  默认值=isnull(e.text,'')           
                 
  FROM   syscolumns   a             
  left   join   systypes   b   on   a.xusertype=b.xusertype             
  inner   join   sysobjects   d   on   a.id=d.id     and   d.xtype='U'   and     d.name<>'dtproperties'             
  left   join   syscomments   e   on   a.cdefault=e.id             
  left   join   sys.extended_properties   g   on   a.id=g.major_id   and   a.colid=g.minor_id                 
  left   join   sys.extended_properties   f   on   d.id=f.major_id   and   f.minor_id=0             
  --where   d.name='要查询的表'         --如果只查询指定表,加上此条件             
where d.name = @tableName      
  and exists(select 1 from @ColumnTable where cname = a.name)        
  order   by   a.id,a.colorder      
  GO
   
  ---查看函數,存儲過程代碼
  create procedure dusp_helptext 
@objname nvarchar(776) 
,@columnname sysname = NULL 
as 
   
set nocount on 
   
declare @dbname sysname 
,@objid int 
,@BlankSpaceAdded   int 
,@BasePos       int 
,@CurrentPos    int 
,@TextLength    int 
,@LineId        int 
,@AddOnLen      int 
,@LFCR          int --lengths of line feed carriage return 
,@DefinedLength int 
   
/* NOTE: Length of @SyscomText is 4000 to replace the length of 
** text column in syscomments. 
** lengths on @Line, #CommentText Text column and 
** value for @DefinedLength are all 255. These need to all have 
** the same values. 255 was selected in order for the max length 
** display using down level clients 
*/ 
,@SyscomText nvarchar(4000) 
,@Line          nvarchar(255) 
   
select @DefinedLength = 255 
select @BlankSpaceAdded = 0 /*Keeps track of blank spaces at end of lines. Note Len function ignores 
                             trailing blank spaces*/ 
CREATE TABLE #CommentText 
(LineId int 
 ,Text  nvarchar(255) collate database_default) 
   
/* 
**  Make sure the @objname is local to the current database. 
*/ 
select @dbname = parsename(@objname,3) 
if @dbname is null 
 select @dbname = db_name() 
else if @dbname <> db_name() 
        begin 
                raiserror(15250,-1,-1) 
                return (1) 
        end 
   
/* 
**  See if @objname exists. 
*/ 
select @objid = object_id(@objname) 
if (@objid is null
        begin 
  raiserror(15009,-1,-1,@objname,@dbname) 
  return (1) 
        end 
   
-- If second parameter was given. 
if ( @columnname is not null
    begin 
        -- Check if it is a table 
        if (select count(*) from sys.objects where object_id = @objid and type in ('S ','U ','TF'))=0 
            begin 
                raiserror(15218,-1,-1,@objname) 
                return(1) 
            end 
        -- check if it is a correct column name 
        if ((select 'count'=count(*) from sys.columns where name = @columnname and object_id = @objid) =0) 
            begin 
                raiserror(15645,-1,-1,@columnname) 
                return(1) 
            end 
    if (ColumnProperty(@objid, @columnname, 'IsComputed') = 0) 
  begin 
   raiserror(15646,-1,-1,@columnname) 
   return(1) 
  end 
   
        declare ms_crs_syscom  CURSOR LOCAL 
        FOR select text from syscomments where id = @objid and encrypted = 0 and number = 
                        (select column_id from sys.columns where name = @columnname and object_id = @objid) 
                        order by number,colid 
        FOR READ ONLY 
   
    end 
else if @objid < 0 -- Handle system-objects 
 begin 
  -- Check count of rows with text data 
  if (select count(*) from master.sys.syscomments where id = @objid and text is not null) = 0 
   begin 
    raiserror(15197,-1,-1,@objname) 
    return (1) 
   end 
      
  declare ms_crs_syscom CURSOR LOCAL FOR select text from master.sys.syscomments where id = @objid 
   ORDER BY number, colid FOR READ ONLY 
 end 
else 
    begin 
     
/* 
        **  Find out how many lines of text are coming back, 
        **  and return if there are none. 
        */ 
        if (select count(*) from syscomments c, sysobjects o where o.xtype not in ('S', 'U'
            and o.id = c.id and o.id = @objid) = 0 
                begin 
                        raiserror(15197,-1,-1,@objname) 
                        return (1) 
                end 
   
        if (select count(*) from syscomments where id = @objid and encrypted = 0) = 0 
                begin 
                        raiserror(15471,-1,-1,@objname) 
                        return (0) 
                end 
   
  declare ms_crs_syscom  CURSOR LOCAL 
  FOR select text from syscomments where id = @objid and encrypted = 0 
    ORDER BY number, colid 
  FOR READ ONLY 
   
    end 
   
/* 
**  else get the text. 
*/ 
select @LFCR = 2 
select @LineId = 1 
   
   
OPEN ms_crs_syscom 
   
FETCH NEXT from ms_crs_syscom into @SyscomText 
   
WHILE @@fetch_status >= 0 
begin 
   
    select  @BasePos    = 1 
  select  @CurrentPos = 1 
    select  @TextLength = LEN(@SyscomText) 
   
    WHILE @CurrentPos  != 0 
    begin 
        --Looking for end of line followed by carriage return 
        select @CurrentPos =   CHARINDEX(char(13)+char(10), @SyscomText, @BasePos) 
   
        --If carriage return found 
        IF @CurrentPos != 0 
        begin 
            /*If new value for @Lines length will be > then the 
            **set length then insert current contents of @line 
            **and proceed. 
            */ 
            while (isnull(LEN(@Line),0) + @BlankSpaceAdded + @CurrentPos-@BasePos + @LFCR) > @DefinedLength 
            begin 
                select @AddOnLen = @DefinedLength-(isnull(LEN(@Line),0) + @BlankSpaceAdded) 
                INSERT #CommentText VALUES 
                ( @LineId, 
                  isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText, @BasePos, @AddOnLen), N'')) 
                select @Line = NULL, @LineId = @LineId + 1, 
                       @BasePos = @BasePos + @AddOnLen, @BlankSpaceAdded = 0 
            end 
            select @Line    = isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText, @BasePos, @CurrentPos-@BasePos + @LFCR), N''
            select @BasePos = @CurrentPos+2 
            INSERT #CommentText VALUES( @LineId, @Line ) 
            select @LineId = @LineId + 1 
            select @Line = NULL 
        end 
        else 
        --else carriage return not found 
        begin 
            IF @BasePos <= @TextLength 
            begin 
                /*If new value for @Lines length will be > then the 
                **defined length 
                */ 
                while (isnull(LEN(@Line),0) + @BlankSpaceAdded + @TextLength-@BasePos+1 ) > @DefinedLength 
                begin 
                    select @AddOnLen = @DefinedLength - (isnull(LEN(@Line),0) + @BlankSpaceAdded) 
                    INSERT #CommentText VALUES 
                    ( @LineId, 
                      isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText, @BasePos, @AddOnLen), N'')) 
                    select @Line = NULL, @LineId = @LineId + 1, 
                        @BasePos = @BasePos + @AddOnLen, @BlankSpaceAdded = 0 
                end 
                select @Line = isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText, @BasePos, @TextLength-@BasePos+1 ), N''
                if LEN(@Line) < @DefinedLength and charindex(' ', @SyscomText, @TextLength+1 ) > 0 
                begin 
                    select @Line = @Line + ' ', @BlankSpaceAdded = 1 
                end 
            end 
        end 
    end 
   
 FETCH NEXT from ms_crs_syscom into @SyscomText 
end 
   
IF @Line is NOT NULL 
    INSERT #CommentText VALUES( @LineId, @Line ) 
   
select Text from #CommentText order by LineId 
   
CLOSE  ms_crs_syscom 
DEALLOCATE  ms_crs_syscom 
   
DROP TABLE  #CommentText 
   
return (0) -- sp_helptext

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
---2005附加數據庫
---ATTACH DATABASE TEMPLATE
exec sp_attach_db 'Asset5','D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Asset5.mdf','D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Asset5_log.ldf'
GO
---列出存儲過程
exec sp_stored_procedures
GO
--系統視圖
select * from sys.objects
 
---列出存儲過程
select * from sys.objects WHERE TYPE='P'
select [name] from sysobjects where xtype='P' order by [name]
GO
---列出所有表
select * from sys.objects WHERE TYPE='U' order by [name]
select [name] from sysobjects where xtype='U' order by [name]
 
GO
--列出視圖
select * from sys.objects WHERE TYPE='V' order by [name]
select [name] from sysobjects where xtype='V' order by [name]
 
GO
--
select * from sysobjects
GO
 
--列出所有表
select [name] from sysobjects where xtype='u' order by [name]
GO
/* select name from sysobjects where xtype='u'  ---
   C = CHECK 约束
   D = 默认值或 DEFAULT 约束
   F = FOREIGN KEY 约束
   L = 日志
   FN = 标量函数
   IF = 内嵌表函数
   P = 存储过程
   PK = PRIMARY KEY 约束(类型是 K)
   RF = 复制筛选存储过程
   S = 系统表
   TF = 表函数
   TR = 触发器
   U = 用户表
   UQ = UNIQUE 约束(类型是 K)
   V = 视图
   X = 扩展存储过程
   */
    
 --得到数据库存储过程列表:
select * from dbo.sysobjects where OBJECTPROPERTY(id, N'IsProcedure') = 1 order by name
--得到某个存储过程的参数信息:(SQL方法) proc_Insert_BookAdministratorListOutput
select * from syscolumns where ID in   
  (SELECT id FROM sysobjects as
   WHERE OBJECTPROPERTY(id, N'IsProcedure') = 1   
   and id = object_id(N'[dbo].[proc_Insert_BookAdministratorListOutput]'))
 
--得到数据库所有表:
select * from dbo.sysobjects where OBJECTPROPERTY(id, N'IsUserTable') = 1 order by name
---得到某个表中的字段信息:dbo.BookInfoList
select c.name as ColumnName, c.colorder as ColumnOrder, c.xtype as DataType, typ.name as  DataTypeName, c.Length, c.isnullable from dbo.syscolumns c inner join dbo.sysobjects t
on c.id = t.id
inner join dbo.systypes typ on typ.xtype = c.xtype
where OBJECTPROPERTY(t.id, N'IsUserTable') = 1
and t.name='BookInfoList' order by c.colorder;
 
 
select a.name,b.name,a.length,a.isnullable from syscolumns a,systypes b,sysobjects d where a.xtype=b.xusertype and a.id=d.id and d.xtype='U' and a.id =object_id('BookInfoList')
--得到存储过程内容
EXEC Sp_HelpText '[proc_Insert_BookAdministratorListOutput]'
 
--得到视图View定义:dbo.View_BookInfoList
--获取View, Procedure, Trigger, Function的源代码
EXEC Sp_HelpText 'View_BookInfoList'
--
EXEC Sp_HelpText 'BookInfoList'
 
SELECT b.name, a.name AS Expr1, a.id, a.xtype, a.typestat, a.xusertype, a.length, a.xprec, a.xscale, a.colid, a.xoffset, a.bitpos, a.reserved, a.colstat,
a.cdefault, a.domain, a.number, a.colorder, a.autoval, a.offset, a.collationid, a.language, a.status, a.type, a.usertype, a.printfmt, a.prec, a.scale,
a.iscomputed, a.isoutparam, a.isnullable, a.collation, a.tdscollation
FROM syscolumns AS a INNER JOIN
sysobjects AS b ON b.id = a.id
WHERE (b.xtype = 'U') AND (b.name <> 'dtproperties')
 
 
 
--得到数据库存储过程列表
select * from dbo.sysobjects where OBJECTPROPERTY(id, N'IsProcedure') = 1 order by name
--列出所有数据库
SELECT name FROM sys.sysdatabases order by name asc
    
--查詢數據庫中的表所占用空間
exec sp_spaceused '表名' --取得表占用空間 
exec sp_spaceused ''--數據庫所有空間 

  

posted @   ®Geovin Du Dream Park™  阅读(791)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示