转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> Sql Server >> 正文
直接从SQL语句问题贴子数据建表并生成建表语句的存储过程         

直接从SQL语句问题贴子数据建表并生成建表语句的存储过程

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2247 更新时间:2007/11/14 11:02:13

下面的存储过程,可帮你在回答SQL语句问题时,直接从贴子的样本数据建表并生成建表语句,省去大量的手工输入数据的工作。

/*Create Table from your web page data
* 2004-JAN-1, OpenVMS,V0.1
* 2004-JAN-2, V0.5, add tab & blank values logical
* 2004-JAN-3, V1.0, add SQL Statement generation
* 2004-JAN-4, V1.1, fix datatype like decimal(4,2) bug
* 2004-JAN-4, V1.2, fix field name bug
*
* Sample Call: in SQL Query Analyzer
exec dbo.create_table ''''##t2'''',''''varchar(20),datetime k'''',''''
ID                   AnDate            
99101                2002-11-24 00:00:00.000
99101                2003-11-15 00:00:00.000
99101                2003-11-29 00:00:00.000
99101                2003-12-20 00:00:00.000''''

注意:
1 如用临时表名,只能用全局临时表 ##,否则不可访问
2 如果没有列名,则需要在第一行数据手动加上列名
3 字段名称不允许含空格
4 至少一行数据,否则没有意义
5 字段值为空需要写上NULL,字段值中的任何符号作为值的一部分
6 没有对定义类型和值的类型匹配检查
7 可指定值中含有空格,方法为在该类型定义中的尾部加字母 k, 如 datatime k,
8 如过值中含有单引号,需要复写 '''' -》''''''''
*/

IF EXISTS (SELECT name
    FROM   sysobjects
    WHERE  name = N''''create_table''''
    AND    type = ''''P'''')
    DROP PROCEDURE create_table
go

create proc dbo.create_table
@table_name varchar(60),--- Table name
@datatype varchar(1000),--- separated by comma '''',''''
@str nvarchar(3000)     --- input string pasted from web page
AS
BEGIN
declare @dt table(id int identity(1,1),fld_name varchar(30),fld_type varchar(20),blank int)
declare @sqlt table(sql_statement varchar(8000))
declare @tmp varchar(1000),@num1 int,@num2 int,@sql nvarchar(4000)
declare @a nvarchar(3000),@i int,@j int,@k int,@m int,@x nvarchar(1000)

SET NOCOUNT ON
if object_id(@table_name) is not null
   begin
    set @a=''''TABLE
''''+@table_name+'''' exists,choose a new one!''''
    RAISERROR (@a,16,1)
    return
   end

--提取类型名
set @datatype=lower(replace(@datatype,'''' '''',''''''''))
set @tmp=@datatype
set @i=1
set @num1=0

while @i>0
begin
 select @i=charindex('''','''',@datatype)
        --check datatype like decimal(10,4)
 if @i>charindex(''''('''',@datatype) and @i<charindex('''')'''',@datatype)
           set @i=charindex('''')'''',@datatype)+1
 select @j=charindex(''''k'''',@datatype)
 set @m=0
 if (@j>1 and @j<@i) or (@i=0 and @j=len(@datatype)) set @m=-1
 if @i>1
 begin
    insert into @dt(fld_type,blank)
  values(left(@datatype,@i-1+@m),case when @m=-1 then 1 else 0 end)
    select @datatype=right(@datatype,len(@datatype)-@i)
 end
 if @i=0 and len(@datatype)>0
    insert into @dt(fld_type,blank) values(left(@datatype,len(@datatype)+@m),
   case when @m=-1 then 1 else 0 end)
 if @i=1 or len(@datatype)=0 
 begin
 RAISERROR (''''error data type,comma sign can not be a prefix or surfix'''',16,1)
 return
 end
 
 set @num1=@num1+1
end

--检查类型
if exists (select fld_type from @dt
   where (case when charindex(''''('''',fld_type)>0 then
               left(fld_type,charindex(''''('''',fld_type)-1)
               else fld_type end) not in (select name from systypes) or
          charindex(''''('''',fld_type)*charindex('''')'''',fld_type)=0 and
          charindex(''''('''',fld_type)+charindex('''')'''',fld_type)>0)
   begin
    RAISERROR (''''error data type.'''', 16, 1)
    return
   end

--提取字段和数据
set @a=replace(@str,char(9),'''' '''') --- TAB char
set @a=rtrim(ltrim(@a))
if charindex(char(13)+char(10),right(@a,len(@a)-1))=0 or len(@a)=0
   begin
    RAISERROR (''''input data error,check your data.'''', 16, 1)
    return
   end

if object_id(''''tempdb.dbo.#xx'''') is not null drop table #xx
select identity(int,1,1) ID,space(50) val into #xx where 1=2
set @k=0
set @num2=0
set @m=0
while len(@a)>0
begin
 set @i=1
 set @x=left(@a,1)

 if @x=char(10) begin
    if @m>@num2 and @num2>0 and charindex(''''k'''',@datatype)=0 begin
              RAISERROR (''''number of data is greater than the columns,you should add k in data type difinition.'''', 16, 1)
              return
    end 
    set @m=0
 end

 if @x not in ('''' '''',char(13),char(10))
 begin
          set @i=charindex('''' '''',@a)
          set @j=charindex(char(13)+char(10),@a)
   set @m=@m+1
   if @k<>-1 set @k=@k+1
   if @j>0 and (@j<@i or @j>@i and substring(@a,@i,@j-@i)=space(@j-@i)) begin
     set @i=@j
     if @k>@num2 and @k<>-1 set @num2=@k
     set @k=-1
   end
          if @i=0 set @i=(case when @j>0 then @j else len(@a)+1 end)

   select @j=max(ID) from #xx
   if @m=1 or @j<
=@num1 or (select blank from @dt where ID=@m-1) <> 1
      begin
        if @j<@num1 set @x=''''[''''+replace(rtrim(left(@a,@i-1)),'''']'''','''']]'''')+'''']''''
        else set @x=rtrim(left(replace(@a,'''''''''''''''',''''''''''''''''''''''''),@i-1)) 
               insert into #xx(val) values(@x)
      end
   else
     begin
       update #xx set val=val+'''' ''''+rtrim(left(@a,@i-1)) where
ID=@j
     

[1] [2]  下一页


[办公软件]在sybase中插入图片、PDF、文本文件  [办公软件]安装Sybase ASE
[办公软件]linux指令大全(完整篇)  [办公软件]Linux新手入门常用命令大全
[办公软件]在RedHat Linux 9里安装gaim0.80  [办公软件]浅谈Linux 下Java 1.5 汉字方块问题解决方法
[办公软件]Linux程序员必读:中文化与GB18030标准  [办公软件]linux指令大全
[办公软件]制作Linux启动盘的四种方法  [办公软件]Linux文件系统的反删除方法
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台