由Excel建表生成数据库真实表

  建表Excel的格式如下:其Excel的名名称最好为AllTableData

TableName
ColumnName
IndexKey
Type
Length
Scale
IsNull
Default
ColumnInfo
销售单主表
 
 
 
 
 
 
 
 
SO_OrderMaster
OrderNo
 
varchar
15
0
 
 
销售单号
SO_OrderMaster
OpenAddrID
 
varchar
5
0
 
 
开单地点编号
SO_OrderMaster
ProdAddrID
 
varchar
5
0
 
生产地点编号
 
利用 SQL Server 2000 Import Data…( Excel 的内容汇入数据库中 ) 功能将表的数据导入到 SQL 数据库,生成 AllTableData 表,然后利用如下的存储过程就可以生成数据库真实表 .
 
/******************************************************************************
由Excel建表生成数据库真实表
调用示例 :
         exec CreateTableForExcel 'SO_OrderMaster','select'
************************************************************/
CREATE PROCEDURE CreateTableForExcel
         @vTableName              varchar(100),
         @vEditType                varchar(15)
AS
set nocount on
 
declare @vSQLString varchar(4000)
,@nvColumnInfo varchar(8000)
,@iPos int
,@iColPos int
,@iCount int
,@iColCount int
,@vInTableName varchar(100)
,@vInColumnName varchar(100)
-- 保存每个表名,用于循环
create table #TableName
(
         TID            int identity(1,1),
         TableName varchar(100)
)
-- 保存某个表的字段名 , 用于循环
create table #ColumnName
(
         TID            int identity(1,1),
         ColumnName      varchar(100)
)
select @vInTableName=''
,@vInColumnName=''
,@iPos=1
,@iCount=0
,@iColPos=1
,@iColCount=0
 
/*
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OrderDetail]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[OrderDetail]
*/
if(exists(select top 1 0 from AllTableData where isnull(ColumnName,'')=''))
begin
         select * from AllTableData
         where isnull(ColumnName,'')=''
end
 
insert into #TableName(TableName)
select TableName
from AllTableData
where TableName like '%'+@vTableName+'%'
group by TableName
 
select @iCount=max(TID)
from #TableName
 
while (@iPos<=@iCount)
begin
         select @vInTableName=TableName from #TableName where TID=@iPos
         set @nvColumnInfo=''
         select @vSQLString='create table '+@vInTableName+''+char(13)+'('+char(13)
         select @vSQLString=@vSQLString+ColumnName+' '+Type
         +case when Type in ('datetime','int') then ',' 
         when Type in ('decimal','numeric') then '('+cast(Length as varchar)+','+cast(Scale as varchar)+'),'
         else '('+cast(Length as varchar)+'),'
         end+char(13)
         from AllTableData
         where TableName=@vInTableName
 
         set @vSQLString=left(@vSQLString,len(@vSQLString)-2)+char(13)+')'
 
         insert into #ColumnName(ColumnName)
         select ColumnName
         from AllTableData
         where TableName=@vInTableName
 
         select @iColCount=max(TID) from #ColumnName
          
         if(@vEditType='select')
         begin
                   print 'Create table '+@vInTableName+' EXECSQL:' +char(13)+@vSQLString+char(13)+char(13)
                   print 'Create table '+@vInTableName+'''s Column info EXECSQL:'+char(13)
         end
         else if(@vEditType='create')
         begin
                   exec(@vSQLString)
                   print @vInTableName+' 创建成功! '+char(13)
         end
        
         while(@iColPos<=@iColCount)
         begin
                   select @vInColumnName=ColumnName
                   from #ColumnName
                   where TID=@iColPos
 
                   select @nvColumnInfo='EXECUTE sp_addextendedproperty N''MS_Description'', N'''+ColumnInfo+''', N''user'', N''dbo'', N''table'', N'''+TableName+''', N''column'', N'''+ColumnName+''''+char(13)
                   from AllTableData
                   where TableName=@vInTableName
                   and ColumnName=@vInColumnName
                   select @iColPos=@iColPos+1
                  
                   if(@vEditType='select')
                   begin
                            print @nvColumnInfo
                   end
                   else if(@vEditType='create')
                   begin
                            exec(@nvColumnInfo)
                            print @vInTableName+' '+@vInColumnName+' 描述信息创建成功! '+char(13)
                   end
         end
         print char(13)
         set @iPos=@iPos+1
         delete from #ColumnName
end
 
drop table #TableName
 
set nocount off
GO

你可能感兴趣的:(SQL,Server)