---
title: "sql server 2005中 一个关系表中存储所有关系表及字段类型，自动创建所有表"
author: "iammonster"
date: "2007-10-13T11:11:00Z"
tags: []
slug: "sql-server-2005"
draft: false
lastmod: "2007-10-13T11:11:00Z"
id: 273296
url: "https://jiajun.iteye.com/blog/273296"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)--创建包含所有要建立表及字段的表 myT  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)use Northwind  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)go  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)if object\_id('myT') is not null  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)drop table myT  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)go  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)create table myT(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)TN VARCHAR(20),  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)TC VARCHAR(10),  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)CT VARCHAR(20),  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)DEF INT  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)INSERT INTO myT values(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)'abc','c1','int',1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)INSERT INTO myT values(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)'abc','c2','varchar(200)',0  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)INSERT INTO myT values(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)'def','c3','xml',1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)INSERT INTO myT values(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)'def','c4','varchar(100)',0  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)go  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)--select * from myT  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)--创建所有表  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)declare @total int  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)declare @count int  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)declare @sql varchar(1000)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)declare @name varchar(200)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)set @count=0  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)set @sql=''  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)select @total=count(distinct tn) from myT--计算表的个数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)while @count&lt;@total -- 循环遍历所有的表名  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)begin  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    with tmp1 as(select distinct tn from myT),  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)         tmp2 as(select tn,Row\_Number() over(order by tn) as rownum from tmp1)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    select @name=tn from tmp2  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        where rownum =@count+1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    -- 创建各个表  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            select @sql=@sql+' '+tc+' '+ct+' '+case when def=1 then 'not null'  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                                                    when def=0 then 'null'   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                                                end+','  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                from myT where tn=@name  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            set @sql ='create table '+@name+'('+@sql+')'  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            select @sql  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    set @count=@count+1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)end  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*//*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)--创建所有表  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)declare @total int  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)declare @count int  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)declare @distable varchar(50)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)declare @givename varchar(100)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)declare @sql varchar(1000)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)declare @name varchar(200)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)set @count=0  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)set @distable=''  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)--计算表的个数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)select @total=count(distinct tn) from myT  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)--select @total  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)while @count&lt;@total -- 循环遍历所有的表名  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)begin  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    with test as(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        select distinct tn from myT  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    ),  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    test2 as(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        select tn,Row\_Number() over(order by tn) as rownum from test  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    select @distable=tn from test2  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        where rownum =@count+1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    -- 创建各个表  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    set @givename=@distable  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    select @ext=count(1) from myT where tn=@givename  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    if(@ext=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        begin  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            print('Error')  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        end  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        begin  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            set @sql=''  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            set @name =''  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            set @name=@givename  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            select @sql=@sql+' '+tc+' '+ct+' '+case when def=1 then 'not null'  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                                                    when def=0 then 'null'   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                                                end+','  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                from myT where tn=@givename  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            set @sql ='create table '+@name+'('+@sql+')'  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            select @sql  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            --exec (@sql)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        end  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    set @count=@count+1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)end  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/