MS SQLSERVER 中如何得到表的创建语句

MS SQLSERVER 只能得到存储过程的创建语句,方法如下:

sp_helptext procedureName

但是往往我们需要得到表的创建语句,比如说在数据库升级的时候判断某个表是否已经改变,或者已经有一个表存在,但不知道它的创建语句是什么,字段有没有约束,有没有主键,创建了哪些索引等等.下面我给出一个存储过程,供读者参考.

该存储过程可以得到你想得到的所有的表的创建语句,包括和表有关的索引的创建语句.

SQLSERVER2000 下的代码

  1 None.gif create   procedure  SP_GET_TABLE_INFO
  2 ExpandedBlockStart.gifContractedBlock.gif @ObjName   varchar ( 128 )        /**/ /* The table to generate sql script */
  3 None.gif as
  4 None.gif
  5 None.gif declare   @Script   varchar ( 255 )
  6 None.gif declare   @ColName   varchar ( 30 )
  7 None.gif declare   @ColID     TinyInt
  8 None.gif declare   @UserType   smallint
  9 None.gif declare   @TypeName  sysname
 10 None.gif declare   @Length     TinyInt
 11 None.gif declare   @Prec       TinyInt
 12 None.gif declare   @Scale      TinyInt
 13 None.gif declare   @Status     TinyInt
 14 None.gif declare   @cDefault   int
 15 None.gif declare   @DefaultID   TinyInt
 16 None.gif declare   @Const_Key   varchar ( 255 )
 17 None.gif declare   @IndID       SmallInt   
 18 None.gif declare   @IndStatus   Int
 19 None.gif declare   @Index_Key   varchar ( 255 )
 20 None.gif declare   @DBName      varchar ( 30 )
 21 None.gif declare   @strPri_Key   varchar  ( 255 )
 22 None.gif
 23 ExpandedBlockStart.gifContractedBlock.gif /**/ /*
 24InBlock.gif**  Check to see the the table exists and initialize @objid.
 25ExpandedBlockEnd.gif*/

 26 None.gif if   not   Exists ( Select  name  from  sysobjects  where  name  =   @ObjName )
 27 None.gif begin
 28 None.gif   select   @DBName   =   db_name ()
 29 None.gif     raiserror ( 15009 , - 1 , - 1 , @ObjName , @DBName )
 30 None.gif     return  ( 1 )
 31 None.gif end
 32 None.gif
 33 None.gif create   table  #spscript
 34 None.gif(
 35 None.gif    id      int   IDENTITY   not   null ,
 36 None.gif    Script  Varchar ( 255 NOT   NULL ,
 37 None.gif    LastLine  tinyint  
 38 None.gif)
 39 None.gif
 40 None.gif declare  Cursor_Column INSENSITIVE  CURSOR
 41 None.gif   for   Select  a.name,a.ColID,a.usertype,b.name,a.length,a.prec,a.scale,a.Status, a.cDefault,
 42 None.gif         case  a.cdefault  when   0   then   '   '   else  ( select  c. Text   from  syscomments c  where  a.cdefault  =  c.id)  end  const_key
 43 None.gif         from  syscolumns a, systypes b  where   object_name (a.id)  =   @ObjName
 44 None.gif         and  a.usertype  =  b.usertype  order   by  a.ColID
 45 None.gif
 46 None.gif set  nocount  on
 47 None.gif Select   @Script   =   ' Create table  '   +   @ObjName   +   ' ( '
 48 None.gif Insert   into  #spscript  values ( @Script , 0 )
 49 None.gif
 50 ExpandedBlockStart.gifContractedBlock.gif /**/ /* Get column information */
 51 None.gif open  Cursor_Column
 52 None.gif
 53 None.gif fetch   next   from  Cursor_Column  into   @ColName , @ColID , @UserType , @TypeName , @Length , @Prec , @Scale ,
 54 None.gif       @Status , @cDefault , @Const_Key
 55 None.gif
 56 None.gif Select   @Script   =   ''  
 57 None.gif while  ( @@FETCH_STATUS   <>   - 1 )
 58 None.gif begin
 59 None.gif   if  ( @@FETCH_STATUS   <>   - 2 )
 60 None.gif   begin
 61 None.gif     Select   @Script   =   @ColName   +   '   '   +   @TypeName
 62 None.gif     if   @UserType   in  ( 1 , 2 , 3 , 4 )
 63 None.gif       Select   @Script   =   @Script   +   ' ( '   +   Convert ( char ( 3 ), @Length +   ' '
 64 None.gif     else   if   @UserType   in  ( 24 )
 65 None.gif       Select   @Script   =   @Script   +   ' ( '   +   Convert ( char ( 3 ), @Prec +   ' , '
 66 None.gif                       +   Convert ( char ( 3 ), @Scale +   ' '
 67 None.gif     else
 68 None.gif       Select   @Script   =   @Script   +   '   '
 69 None.gif     if  (  @Status   &   0x80  )  >   0
 70 None.gif       Select   @Script   =   @Script   +   '  IDENTITY(1,1)  '
 71 None.gif
 72 None.gif     if  (  @Status   &   0x08  )  >   0
 73 None.gif       Select   @Script   =   @Script   +   '  NULL  '
 74 None.gif     else
 75 None.gif       Select   @Script   =   @Script   +   '  NOT NULL  '
 76 None.gif     if   @cDefault   >   0
 77 None.gif       Select   @Script   =   @Script   +   '  DEFAULT  '   +   @Const_Key
 78 None.gif   end
 79 None.gif   fetch   next   from  Cursor_Column  into   @ColName , @ColID , @UserType , @TypeName , @Length , @Prec , @Scale ,
 80 None.gif       @Status , @cDefault , @Const_Key
 81 None.gif   if   @@FETCH_STATUS   =   0
 82 None.gif   begin
 83 None.gif     Select   @Script   =   @Script   +   ' , '  
 84 None.gif     Insert   into  #spscript  values ( @Script , 0 )
 85 None.gif   end
 86 None.gif   else
 87 None.gif   begin
 88 None.gif     Insert   into  #spscript  values ( @Script , 1 )
 89 None.gif     Insert   into  #spscript  values ( ' ) ' , 0 )
 90 None.gif   end
 91 None.gif end
 92 None.gif Close  Cursor_Column
 93 None.gif Deallocate  Cursor_Column
 94 None.gif
 95 ExpandedBlockStart.gifContractedBlock.gif /**/ /* Get index information */
 96 None.gif Declare  Cursor_Index INSENSITIVE  CURSOR
 97 None.gif   for   Select  name,IndID,status  from  sysindexes  where   object_name (id) = @ObjName
 98 ExpandedBlockStart.gifContractedBlock.gif               and  IndID  >   0   and  IndID <> 255    order   by  IndID    /**/ /*增加了对InDid为255的判断*/
 99 None.gif Open  Cursor_Index
100 None.gif Fetch   Next   from  Cursor_Index  into   @ColName @IndID @IndStatus
101 None.gif while  ( @@FETCH_STATUS   <>   - 1 )
102 None.gif begin
103 None.gif   if   @@FETCH_STATUS   <>   - 2
104 None.gif   begin
105 None.gif
106 None.gif     declare   @i   TinyInt
107 None.gif     declare   @thiskey   varchar ( 50 )
108 ExpandedBlockStart.gifContractedBlock.gif     declare   @IndDesc   varchar ( 68 /**/ /* string to build up index desc in */
109 None.gif
110 None.gif     Select    @i   =   1
111 None.gif     while  ( @i   <=   16 )
112 None.gif     begin
113 None.gif       select   @thiskey   =   index_col ( @ObjName @IndID @i )
114 None.gif       if   @thiskey   is   null
115 None.gif         break
116 None.gif
117 None.gif       if   @i   =   1
118 None.gif         select   @Index_Key   =   index_col ( @ObjName @IndID @i )
119 None.gif       else
120 None.gif         select   @Index_Key   =   @Index_Key   +   ' '   +   index_col ( @ObjName @IndID @i )
121 None.gif       select   @i   =   @i   +   1
122 None.gif     end
123 None.gif     if  ( @IndStatus   &   0x02 >   0
124 None.gif       Select   @Script   =   ' Create unique  '
125 None.gif     else
126 None.gif       Select   @Script   =   ' Create  '
127 None.gif     if   @IndID   =   1
128 None.gif       select   @Script   =   @Script   +   '  clustered  '
129 None.gif
130 None.gif
131 None.gif     if  ( @IndStatus   &   0x800 >   0
132 None.gif      select   @strPri_Key   =   '  PRIMARY KEY ( '   +   @Index_Key   +   ' ) '
133 None.gif     else
134 None.gif      select   @strPri_Key   =   ''
135 None.gif     
136 None.gif     if   @IndID   >   1
137 None.gif       select   @Script   =   @Script   +   '  nonclustered  '
138 None.gif     Select   @Script   =   @Script   +   '  index  '   +   @ColName   +   '  ON  ' +   @ObjName
139 None.gif            +   ' ( '   +   @Index_Key   +   ' ) '
140 None.gif     Select   @IndDesc   =   ''  
141 ExpandedBlockStart.gifContractedBlock.gif     /**/ /*
142InBlock.gif **  See if the index is ignore_dupkey (0x01).
143ExpandedBlockEnd.gif    */

144 None.gif     if   @IndStatus   &   0x01   =   0x01
145 None.gif       Select   @IndDesc   =   @IndDesc   +   '  IGNORE_DUP_KEY '   +   ' , '
146 ExpandedBlockStart.gifContractedBlock.gif     /**/ /*
147InBlock.gif     **  See if the index is ignore_dup_row (0x04).
148ExpandedBlockEnd.gif    */

149 ExpandedBlockStart.gifContractedBlock.gif    /**/ /* if @IndStatus & 0x04 = 0x04 */
150 ExpandedBlockStart.gifContractedBlock.gif    /**/ /*   Select @IndDesc = @IndDesc + ' IGNORE_DUP_ROW' + ',' */   /**/ /* 2000 不在支持*/  
151 ExpandedBlockStart.gifContractedBlock.gif     /**/ /*
152InBlock.gif **  See if the index is allow_dup_row (0x40).
153ExpandedBlockEnd.gif    */

154 None.gif     if   @IndStatus   &   0x40   =   0x40
155 None.gif       Select   @IndDesc   =   @IndDesc   +   '  ALLOW_DUP_ROW '   +   ' , '
156 None.gif     if   @IndDesc   <>   ''
157 None.gif     begin
158 None.gif       Select   @IndDesc   =   SubString @IndDesc 1 DataLength ( @IndDesc -   1  )
159 None.gif       Select   @Script   =   @Script   +   '  WITH  '   +   @IndDesc
160 None.gif     end
161 ExpandedBlockStart.gifContractedBlock.gif     /**/ /*
162InBlock.gif **  Add the location of the data.
163ExpandedBlockEnd.gif    */

164 None.gif   end
165 None.gif   if  ( @strPri_Key   =   '' )
166 None.gif     Insert   into  #spscript  values ( @Script , 0 )
167 None.gif   else  
168 None.gif     update  #spscript  set  Script  =  Script  +   @strPri_Key   where  LastLine  =   1
169 None.gif  
170 None.gif   Fetch   Next   from  Cursor_Index  into   @ColName @IndID @IndStatus
171 None.gif end
172 None.gif Close  Cursor_Index
173 None.gif Deallocate  Cursor_Index
174 None.gif
175 None.gif Select  Script  from  #spscript
176 None.gif
177 None.gif set  nocount  off
178 None.gif
179 None.gif return  ( 0 )
180 None.gif
181 None.gif
182 None.gifSQLSERVER6.5下的代码
183 None.gif
184 None.gif create   procedure  SP_GET_TABLE_INFO
185 ExpandedBlockStart.gifContractedBlock.gif @ObjName   varchar ( 128 )        /**/ /* The table to generate sql script */
186 None.gif as
187 None.gif
188 None.gif declare   @Script   varchar ( 255 )
189 None.gif declare   @ColName   varchar ( 30 )
190 None.gif declare   @ColID     TinyInt
191 None.gif declare   @UserType   smallint
192 None.gif declare   @TypeName  sysname
193 None.gif declare   @Length     TinyInt
194 None.gif declare   @Prec       TinyInt
195 None.gif declare   @Scale      TinyInt
196 None.gif declare   @Status     TinyInt
197 None.gif declare   @cDefault   int
198 None.gif declare   @DefaultID   TinyInt
199 None.gif declare   @Const_Key   varchar ( 255 )
200 None.gif declare   @IndID       SmallInt
201 None.gif declare   @IndStatus   SmallInt
202 None.gif declare   @Index_Key   varchar ( 255 )
203 None.gif declare   @Segment     SmallInt
204 None.gif declare   @DBName      varchar ( 30 )
205 None.gif declare   @strPri_Key   varchar  ( 255 )
206 None.gif
207 ExpandedBlockStart.gifContractedBlock.gif /**/ /*
208InBlock.gif**  Check to see the the table exists and initialize @objid.
209ExpandedBlockEnd.gif*/

210 None.gif if   not   Exists ( Select  name  from  sysobjects  where  name  =   @ObjName )
211 None.gif begin
212 None.gif   select   @DBName   =   db_name ()
213 None.gif     raiserror ( 15009 , - 1 , - 1 , @ObjName , @DBName )
214 None.gif     return  ( 1 )
215 None.gif end
216 None.gif
217 None.gif create   table  #spscript
218 None.gif(
219 None.gif    id      int   IDENTITY   not   null ,
220 None.gif    Script  Varchar ( 255 NOT   NULL ,
221 None.gif    LastLine  tinyint  
222 None.gif)
223 None.gif
224 None.gif declare  Cursor_Column INSENSITIVE  CURSOR
225 None.gif   for   Select  a.name,a.ColID,a.usertype,b.name,a.length,a.prec,a.scale,a.Status, a.cDefault,
226 None.gif         case  a.cdefault  when   0   then   '   '   else  ( select   case  c. text   when  "( '   ' )"  then  "( '' )"  else  c. text   end  
227 None.gif         from  syscomments c  where  a.cdefault  =  c.id)  end  const_key
228 None.gif         from  syscolumns a, systypes b  where   object_name (a.id)  =   @ObjName
229 None.gif         and  a.usertype  =  b.usertype  order   by  a.ColID
230 None.gif
231 None.gif set  nocount  on
232 None.gif Select   @Script   =   ' Create table  '   +   @ObjName   +   ' ( '
233 None.gif Insert   into  #spscript  values ( @Script , 0 )
234 None.gif
235 ExpandedBlockStart.gifContractedBlock.gif /**/ /* Get column information */
236 None.gif open  Cursor_Column
237 None.gif
238 None.gif fetch   next   from  Cursor_Column  into   @ColName , @ColID , @UserType , @TypeName , @Length , @Prec , @Scale ,
239 None.gif       @Status , @cDefault , @Const_Key
240 None.gif
241 None.gif Select   @Script   =   ''  
242 None.gif while  ( @@FETCH_STATUS   <>   - 1 )
243 None.gif begin
244 None.gif   if  ( @@FETCH_STATUS   <>   - 2 )
245 None.gif   begin
246 None.gif     Select   @Script   =   @ColName   +   '   '   +   @TypeName
247 None.gif     if   @UserType   in  ( 1 , 2 , 3 , 4 )
248 None.gif       Select   @Script   =   @Script   +   ' ( '   +   Convert ( char ( 3 ), @Length +   ' '
249 None.gif     else   if   @UserType   in  ( 24 )
250 None.gif       Select   @Script   =   @Script   +   ' ( '   +   Convert ( char ( 3 ), @Prec +   ' , '
251 None.gif                       +   Convert ( char ( 3 ), @Scale +   ' '
252 None.gif     else
253 None.gif       Select   @Script   =   @Script   +   '   '
254 None.gif     if  (  @Status   &   0x80  )  >   0
255 None.gif       Select   @Script   =   @Script   +   '  IDENTITY(1,1)  '
256 None.gif
257 None.gif     if  (  @Status   &   0x08  )  >   0
258 None.gif       Select   @Script   =   @Script   +   '  NULL  '
259 None.gif     else
260 None.gif       Select   @Script   =   @Script   +   '  NOT NULL  '
261 None.gif     if   @cDefault   >   0
262 None.gif       Select   @Script   =   @Script   +   '  DEFAULT  '   +   @Const_Key
263 None.gif   end
264 None.gif   fetch   next   from  Cursor_Column  into   @ColName , @ColID , @UserType , @TypeName , @Length , @Prec , @Scale ,
265 None.gif       @Status , @cDefault , @Const_Key
266 None.gif   if   @@FETCH_STATUS   =   0
267 None.gif   begin
268 None.gif     Select   @Script   =   @Script   +   ' , '  
269 None.gif     Insert   into  #spscript  values ( @Script , 0 )
270 None.gif   end
271 None.gif   else
272 None.gif   begin
273 None.gif     Insert   into  #spscript  values ( @Script , 1 )
274 None.gif     Insert   into  #spscript  values ( ' ) ' , 0 )
275 None.gif   end
276 None.gif end
277 None.gif Close  Cursor_Column
278 None.gif Deallocate  Cursor_Column
279 None.gif
280 ExpandedBlockStart.gifContractedBlock.gif /**/ /* Get index information */
281 None.gif Declare  Cursor_Index INSENSITIVE  CURSOR
282 None.gif   for   Select  name,IndID,status,Segment  from  sysindexes  where   object_name (id) = @ObjName
283 None.gif               and  IndID  >   0   and  IndID <> 255   order   by  IndID
284 None.gif Open  Cursor_Index
285 None.gif Fetch   Next   from  Cursor_Index  into   @ColName @IndID @IndStatus @Segment
286 None.gif while  ( @@FETCH_STATUS   <>   - 1 )
287 None.gif begin
288 None.gif   if   @@FETCH_STATUS   <>   - 2
289 None.gif   begin
290 None.gif
291 None.gif     declare   @i   TinyInt
292 None.gif     declare   @thiskey   varchar ( 50 )
293 ExpandedBlockStart.gifContractedBlock.gif     declare   @IndDesc   varchar ( 68 /**/ /* string to build up index desc in */
294 None.gif
295 None.gif     Select    @i   =   1
296 None.gif     while  ( @i   <=   16 )
297 None.gif     begin
298 None.gif       select   @thiskey   =   index_col ( @ObjName @IndID @i )
299 None.gif       if   @thiskey   is   null
300 None.gif         break
301 None.gif
302 None.gif       if   @i   =   1
303 None.gif         select   @Index_Key   =   index_col ( @ObjName @IndID @i )
304 None.gif       else
305 None.gif         select   @Index_Key   =   @Index_Key   +   ' '   +   index_col ( @ObjName @IndID @i )
306 None.gif       select   @i   =   @i   +   1
307 None.gif     end
308 None.gif     if  ( @IndStatus   &   0x02 >   0
309 None.gif       Select   @Script   =   ' Create unique  '
310 None.gif     else
311 None.gif       Select   @Script   =   ' Create  '
312 None.gif     if   @IndID   =   1
313 None.gif       select   @Script   =   @Script   +   '  clustered  '
314 None.gif
315 None.gif
316 None.gif     if  ( @IndStatus   &   0x800 >   0
317 None.gif      select   @strPri_Key   =   '  PRIMARY KEY ( '   +   @Index_Key   +   ' ) '
318 None.gif     else
319 None.gif      select   @strPri_Key   =   ''
320 None.gif     
321 None.gif     if   @IndID   >   1
322 None.gif       select   @Script   =   @Script   +   '  nonclustered  '
323 None.gif     Select   @Script   =   @Script   +   '  index  '   +   @ColName   +   '  ON  ' +   @ObjName
324 None.gif            +   ' ( '   +   @Index_Key   +   ' ) '
325 None.gif     Select   @IndDesc   =   ''  
326 ExpandedBlockStart.gifContractedBlock.gif     /**/ /*
327InBlock.gif **  See if the index is ignore_dupkey (0x01).
328ExpandedBlockEnd.gif    */

329 None.gif     if   @IndStatus   &   0x01   =   0x01
330 None.gif       Select   @IndDesc   =   @IndDesc   +   '  IGNORE_DUP_KEY '   +   ' , '
331 ExpandedBlockStart.gifContractedBlock.gif     /**/ /*
332InBlock.gif     **  See if the index is ignore_dup_row (0x04).
333ExpandedBlockEnd.gif    */

334 None.gif     if   @IndStatus   &   0x04   =   0x04
335 None.gif       Select   @IndDesc   =   @IndDesc   +   '  IGNORE_DUP_ROW '   +   ' , '
336 ExpandedBlockStart.gifContractedBlock.gif     /**/ /*
337InBlock.gif **  See if the index is allow_dup_row (0x40).
338ExpandedBlockEnd.gif    */

339 None.gif     if   @IndStatus   &   0x40   =   0x40
340 None.gif       Select   @IndDesc   =   @IndDesc   +   '  ALLOW_DUP_ROW '   +   ' , '
341 None.gif     if   @IndDesc   <>   ''
342 None.gif     begin
343 None.gif       Select   @IndDesc   =   SubString @IndDesc 1 DataLength ( @IndDesc -   1  )
344 None.gif       Select   @Script   =   @Script   +   '  WITH  '   +   @IndDesc
345 None.gif     end
346 ExpandedBlockStart.gifContractedBlock.gif     /**/ /*
347InBlock.gif **  Add the location of the data.
348ExpandedBlockEnd.gif    */

349 None.gif     if   @Segment   <>   1
350 None.gif       select   @Script   =   @Script   +   '  ON  '   +  name
351 None.gif   from  syssegments
352 None.gif   where  segment  =   @Segment
353 None.gif   end
354 None.gif   if  ( @strPri_Key   =   '' )
355 None.gif     Insert   into  #spscript  values ( @Script , 0 )
356 None.gif   else  
357 None.gif     update  #spscript  set  Script  =  Script  +   @strPri_Key   where  LastLine  =   1
358 None.gif  
359 None.gif   Fetch   Next   from  Cursor_Index  into   @ColName @IndID @IndStatus @Segment
360 None.gif end
361 None.gif Close  Cursor_Index
362 None.gif Deallocate  Cursor_Index
363 None.gif
364 None.gif Select  Script  from  #spscript  order   by  id
365 None.gif
366 None.gif set  nocount  off
367 None.gif
368 None.gif return  ( 0 )
369 None.gif
370 None.gif
371 None.gif

转载于:https://www.cnblogs.com/rengy/archive/2005/10/12/253111.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值